python+selenium2自動化---關鍵字驅動+unittest結合實現自動化


簡單理解:

關鍵字驅動就是將頁面元素的定位、操作等相關代碼封裝成方法(關鍵字),編寫用例的時候直接調用對應方法(關鍵字),而不用關心頁面元素的相關操作

unittest框架組織和執行測試用例

示例代碼如下:

頁面元素操作的關鍵字:

from selenium import webdriver class BasePage(): def __init__(self,name,url): self.driver = self.open_brower(name,url) def open_brower(self,name,url): if name == 'chrome': driver = webdriver.Chrome() if name=='ie': driver = webdriver.Ie() driver.get(url) driver.maximize_window() return driver #在輸入框輸入內容
    def input_text(self,locater_type,locater,text): if locater_type == 'id': self.driver.find_element_by_id(locater).send_keys(text) if locater_type == 'xpath': self.driver.find_element_by_xpath(locater).send_keys(text) if locater_type == 'name': self.driver.find_element_by_name(locater).send_keys(text) #點擊按鈕
    def click_ele(self, locater_type, locater): if locater_type == 'id': self.driver.find_element_by_id(locater).click() if locater_type == 'xpath': self.driver.find_element_by_xpath(locater).click() if locater_type == 'name': self.driver.find_element_by_name(locater).click()

測試用例代碼

import unittest import time from ddt import ddt,data,unpack from MainPage import BasePage @ddt class TestLogin1(unittest.TestCase): def setUp(self): print('開始測試') def tearDown(self): print('結束測試') @data(('id','userAccount','id','password','xpath','//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/div/button','xxxxxxx','123456'), ('id','userAccount','id','password','xpath','//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/div/button','aaaaaaaa','123456')) @unpack def test_manager_login(self,locater_type,locator,locator_type1,locator1,locator_type2,locator2,text,text1): driver = BasePage('chrome','http://xxxxxxxxx.com/login') driver.input_text(locater_type,locator,text) driver.input_text(locator_type1, locator1, text1) driver.click_ele(locator_type2,locator2) time.sleep(2) driver.driver.quit() if __name__ == '__main__': unittest.main()

執行結果:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM