关于selenium测试框架首先想到的就是PO模型,简单说下PO模型
PO模型的概念和理解:
PO就是一个设计思想,将代码以页面为单位进行组织,针对这个页面上的所有信息、相关操作都放到一个类中,从而使具体的测试用例变成了简单的调用和验证操作。
优点:进行了拆分和分层
缺点:对于复杂的业务page层变了,case也需要去改动
PO模型的目录结构:
其中,base_page是login_page、search_page的基础。test_login调用login_page,login_page调用base_page,同理test_search。
PO代码示例:
base_page.py
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.py
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("测试")