PO(Page Object)设计模式是自动化测试项目最佳的设计模式之一,下边是我个人对PO设计模式粗浅的一点理解
一、流水账:
不知道设计模式时,写自动化测试用例,是采用“业务操作-断言-业务操作2-断言2...”这种流程编写代码的
完全按照手工测试的流程,执行操作,判断预期结果的方式写的代码,看起来就像是流水账
#!/usr/bin/env python # _*_ coding:utf-8 _*_ from selenium import webdriver from selenium.webdriver.remote import webelement import unittest,time,HTMLTestRunner_cn firefox_profile = r'C:\Users\86168\AppData\Roaming\Mozilla\Firefox\Profiles\i7nn3wax.default' pf = webdriver.FirefoxProfile(firefox_profile) driver = webdriver.Firefox(pf) driver.get(u'http://www.baidu.com') bdhome = driver.title if bdhome == u'百度一下,你就知道': print 'PASS' else: print 'FAIL' inputer = driver.find_element_by_id(u'kw') inputer.clear() inputer.send_keys(u'python学习') time.sleep(3) bdsug = driver.title if bdhome == u'python学习_百度搜索': print 'PASS' else: print 'FAIL' driver.quit()
二、稍作修改,将业务操作和预期结果的判断(断言)分离,加入unittest框架,加入HTMLTestRunner,就有一点PO的雏形了
#!/usr/bin/env python # _*_ coding:utf-8 _*_from selenium import webdriverimport unittest,time,HTMLTestRunner_cnfirefox_profile = r'C:\Users\86168\AppData\Roaming\Mozilla\Firefox\Profiles\i7nn3wax.default'pf = webdriver.FirefoxProfile(firefox_profile)driver = webdriver.Firefox(pf)driver.get(u'http://www.baidu.com')bdhome = driver.titleinputer = driver.find_element_by_id(u'kw')inputer.clear()inputer.send_keys(u'python学习')time.sleep(3)bdsug = driver.titledriver.quit()class MyTestCase(unittest.TestCase): def test_1(self): ''' 搜索关键字:python :return: None ''' self.assertEqual(bdsug,u'python学习路线_百度搜索') def test_2(self): ''' 打开百度后 :return: None ''' self.assertEqual(bdhome,u'百度一下,你就知道')if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(MyTestCase('test_1')) suite.addTest(MyTestCase('test_2')) with open('line.html','a') as f: runner = HTMLTestRunner_cn.HTMLTestRunner(stream=f,verbosity=2,title='线性模型') runner.run(suite)
三、分离业务代码和测试代码,封装业务代码,改造测试代码,这就是一个完整的PO设计模式的测试脚本了
业务代码:
# coding=utf-8# !/usr/bin/env python# 业务代码from selenium import webdriverimport timefirefox_profile = r'C:\Users\86168\AppData\Roaming\Mozilla\Firefox\Profiles\i7nn3wax.default'pf = webdriver.FirefoxProfile(firefox_profile)driver = webdriver.Firefox(pf)def home_page(): driver.get(u'http://www.baidu.com') bdhome = driver.title inputer = driver.find_element_by_id(u'kw') inputer.clear() inputer.send_keys(u'python学习') time.sleep(3) bdsug = driver.title driver.quit() return bdhome, bdsug
测试代码:
#!/usr/bin/env python# _*_ coding:utf-8 _*_#测试代码import unittestfrom ..Page.TestPage import home_pagebdhome,bdsug = home_page()class MyTestCase(unittest.TestCase): def test_1(self): ''' 搜索关键字:python :return: None ''' self.assertEqual(bdsug,u'python学习路线_百度搜索') def test_2(self): ''' 打开百度后 :return: None ''' self.assertEqual(bdhome,u'百度一下,你就知道')
启动程序:
启动程序要注意,引入包的时候不能使用相对路径,最好使用添加环境变量的方法引入包
#!/usr/bin/env python # _*_ coding:utf-8 _*_#启动程序import unittest,os,sysBaseDir = os.path.dirname(os.path.dirname(__file__))sys.path.append(BaseDir)from pyPOmodule.Case.TestPageCase import MyTestCasefrom pyPOmodule.HTMLTestRunner_cn import HTMLTestRunnerif __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(MyTestCase('test_1')) suite.addTest(MyTestCase('test_2')) with open('line.html','a') as f: runner = HTMLTestRunner(stream=f,verbosity=2,title='线性模型') runner.run(suite)
四、提取基类,使用类再次封装业务代码,将不同模块的业务封装为不同的类
未完持续...