關於selenium測試框架首先想到的就是PO模型,簡單說下PO模型html
PO模型的概念和理解:web
PO就是一個設計思想,將代碼以頁面爲單位進行組織,針對這個頁面上的全部信息、相關操做都放到一個類中,從而使具體的測試用例變成了簡單的調用和驗證操做。框架
優勢:進行了拆分和分層測試
缺點:對於複雜的業務page層變了,case也須要去改動ui
PO模型的目錄結構:url
其中,base_page是login_page、search_page的基礎。test_login調用login_page,login_page調用base_page,同理test_search。spa
PO代碼示例:設計
base_page.pycode
1 from selenium.webdriver.support.wait import WebDriverWait 2 3 ''' 4 這個類主要是完成全部頁面的一些公共方法的封裝 5 ''' 6 class Action(object): 7 #初始化 8 def __init__(self,se_driver): 9 self.driver = se_driver 10 11 #定義open方法 12 def open(self,url): 13 self.driver.get(url) 14 self.driver.maximize_window() 15 16 #重寫元素定位的方法 17 def find_element(self,*loc): 18 try: 19 WebDriverWait(self.driver,20).until(lambda driver:driver.find_element(*loc).is_displayed()) 20 return self.driver.find_element(*loc) 21 except Exception as e: 22 print("未找到%s"%(self,loc)) 23 24 #定義script方法,用於執行js腳本 25 def script(self,src): 26 self.driver.execute_script(src) 27 28 #重寫send_keys方法 29 def send_keys(self,loc,value,clear_first=True,clik_first=True): 30 try: 31 if clik_first: 32 self.find_element(*loc).click() 33 if clear_first: 34 self.find_element(*loc).clear() 35 self.find_element(*loc).send_keys(value) 36 except AttributeError: 37 print("未找到%s"%(self,loc))
login_page.pyhtm
1 from selenium.webdriver.common.by import By 2 from seleniumframework.PO import base_page 3 import time 4 5 class LoginPage(base_page.Action): 6 link_loc = (By.LINK_TEXT,"登陸") 7 name_loc = (By.ID,"TANGRAM__PSP_8__userName") 8 password_loc = (By.ID,"TANGRAM__PSP_8__password") 9 submit_loc = (By.ID,"TANGRAM__PSP_8__submit") 10 11 username_top = (By.LINK_TEXT,"hanxiaobei") 12 13 14 def click_link(self): 15 self.find_element(*self.link_loc).click() 16 time.sleep(3) #等待3秒,等待登陸彈窗加載完成 17 18 def run_case(self,value1,value2): 19 self.find_element(*self.name_loc).send_keys(value1) 20 self.find_element(*self.password_loc).send_keys(value2) 21 time.sleep(20) #手動輸入驗證碼 22 self.find_element(*self.submit_loc).click() 23 time.sleep(5) #等待5秒,登陸後的頁面加載完成 24 25 def get_username(self): 26 return self.find_element(*self.username_top).text
test_login.py
1 import unittest 2 from selenium import webdriver 3 from seleniumframework.PO.login_page import LoginPage 4 import time 5 6 class TestBaiduLogin(unittest.TestCase): 7 """UI自動化登陸""" 8 def setUp(self): 9 self.url = "http://www.baidu.com" 10 self.driver = webdriver.Firefox() 11 self.driver.implicitly_wait(20) 12 # self.verificationErrors = [] 13 14 def tearDown(self): 15 time.sleep(5) 16 self.driver.quit() 17 # self.assertEqual([],self.verificationErrors) 18 19 def test_login(self): 20 """百度登陸""" 21 sp = LoginPage(self.driver) 22 sp.open(self.url) 23 sp.click_link() 24 sp.run_case("hanxiaobei","xxxxxxx") 25 self.assertEqual(sp.get_username(),"hanxiaobei",msg="驗證失敗!")
main.py 運行的主入口
1 import unittest 2 import HTMLTestRunner 3 4 #相對路徑 5 testcase_path = ".\\testcase" 6 report_path = ".\\report\\report.html" 7 def creat_suite(): 8 uit = unittest.TestSuite() 9 discover = unittest.defaultTestLoader.discover(testcase_path,pattern="test_*.py") 10 for test_suite in discover: 11 # print(test_suite) 12 for test_case in test_suite: 13 uit.addTest(test_case) 14 return uit 15 16 suite = creat_suite() 17 fp = open(report_path,"wb") 18 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="測試結果",description="測試搜索結果") 19 runner.run(suite) 20 fp.close()
解決報告命名的問題:
1 now = time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time())) 2 print(now)
3 report_path = ".\\report\\"+now+"report.html"
測試報告截圖:
search_page.py
1 from selenium.webdriver.common.by import By 2 from seleniumframework.PO import base_page 3 4 #繼承base後既能夠調用base的方法也可本身添加新的方法 5 class SearchPage(base_page.Action): 6 7 #經過id進行定位元素 8 search_loc = (By.ID,"kw") 9 10 def run_case(self,value): 11 #第一種利用原生的send_keys方法 12 self.find_element(*self.search_loc).send_keys(value) 13 14 #第二種利用二次封裝的send_keys方法 15 # self.send_keys(self.search_loc,value)
test_search.py
1 import unittest 2 from selenium import webdriver 3 from seleniumframework.PO.search_page import SearchPage 4 import time 5 6 class TestBaiduSearch(unittest.TestCase): 7 """UI自動化搜索""" 8 def setUp(self): 9 self.url = "http://www.baidu.com" 10 self.driver = webdriver.Firefox() 11 self.driver.implicitly_wait(20) 12 self.verificationErrors = [] 13 14 def tearDown(self): 15 time.sleep(5) 16 self.driver.quit() 17 self.assertEqual([],self.verificationErrors) 18 19 def test_search(self): 20 """搜索測試關鍵字""" 21 sp = SearchPage(self.driver) 22 sp.open(self.url) 23 sp.run_case("測試")