🔥 交流讨论:欢迎加入我们一起学习!
🔥 资源分享:耗时200+小时精选的「软件测试」资料包
🔥 教程推荐:火遍全网的《软件测试》教程
📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
一、设计思路
框架采用python3+yaml+po+unittest+HTMLTestRunner等技术编写的UI自动化测试框架
1.使用Page Object模式将页面定位和业务操作分开,分离测试对象(元素对象)和测试脚本(用例脚本),一个页面建一个对象类,提高用例的可维护性;
2.使用yaml管理页面空间元素和测试用例数据。例如一个页面元素发生变化时,不需要去修改代码,只需要在对应的页面元素yaml文件中修改即可。
3.以每个页面作为模块管理,随时组装,互不影响。
二、测试框架分层
1.框架层:基础组件封装,支撑整个框架的流程执行及功能扩展
2.用例层:根据用例场景构造test测试方法
3.业务层:封装页面对象类,一个页面建立一个类,业务层基础基础层page类
4.基础层:二次开发selenium的元素定位已经操作
三、编写用例方法
例如我需要新增百度搜索测试场景用例:
1.在testyaml目录下新增一个页面对象yaml文件,利用封装的yamlload方法将yaml文件中的dom元素读取出来,提供给封装页面对象类调用并定元素操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
2.在testdata目录下新增一个yaml文件提供给用例测试数据
1 2 3 4 5 6 7 8 9 |
|
3.在page_obj目录编写百度搜索场景的页面对象类封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
4.在testcase目录编写测试用例文件,采用ddt数据驱动读取yaml测试数据源文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
5.通过多线程将驱动传给执行方法,并行开启多个浏览器执行测试
1 2 3 4 5 6 7 8 9 10 11 12 |
|
6.操作元素封装
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
7.浏览器驱动封装
from typing import Union, Type from selenium.webdriver import * from selenium.webdriver.opera.options import Options as OperaOptions from selenium.webdriver.edge.options import Options as EdgeOptions from setting import *BROWSERS = (Chrome, Ie, Firefox, Edge, Opera, PhantomJS) OPTIONS = (ChromeOptions, IeOptions, FirefoxOptions, EdgeOptions, OperaOptions)class BROWSER:CHROME_DRIVER_PATH = CHROME_DRIVER_PATH# 使用32位ie驱动解决文本输入缓慢的问题IE_DRIVER_PATH = IE_DRIVER_PATHFIREFOX_DRIVER_PATH = FIREFOX_DRIVER_PATHEDGE_DRIVER_PATH = EDGE_DRIVER_PATHOPERA_DRIVER_PATH = OPERA_DRIVER_PATHHEADLESS = HEADLESS# IMPLICITLY WAITING TIME METHODIMPLICITLY_WAIT_TIME = 10# PAGE SOURCE LOAD TIME METHODPAGE_LOAD_TIME = 10# the script should wait during an# execute_async_script call before throwing an errorSCRIPT_TIMEOUT = 0# SET WINDOW SIZE METHODWINDOWS_SIZE = (1024, 900)def __init__(self, browser_type: Type[Union[Chrome, Ie, Firefox, Edge, Opera, PhantomJS, Remote]] = Chrome,option_type: Type[Union[FirefoxOptions, ChromeOptions, IeOptions, EdgeOptions, OperaOptions]] = ChromeOptions,driver_path: str = CHROME_DRIVER_PATH):if not issubclass(browser_type, BROWSERS):raise TypeErrorif not issubclass(option_type, OPTIONS):raise TypeErrorif not isinstance(driver_path, str):raise TypeErrorself._driver = browser_typeself._option = option_typeself._path = driver_path@propertydef browser(self):"""subclass should implement this methodreturn the instance of WebDriver:return:"""return@propertydef _options(self):"""subclass should implement this methodreturn instance of the Option Type like ChromeOptions:return:"""returnclass CHROME(BROWSER):METHOD_MARK = TrueIMPLICITLY_WAIT_TIME = 30PAGE_LOAD_TIME = 20SCRIPT_TIMEOUT = 10# HEADLESS MODEL OPTIONHEADLESS = FalseOPTION_MARK = True# EXPERIMENTAL OPTIONEXPERIMENTAL = {# 'mobileEmulation': {'deviceName': 'iPhone 6'}, #以移动格式现实启动页面做h5时候会用到'excludeSwitches': ['enable-automation'] #浏览器启动时左上角提示正在运行自动化}# WINDOW SIZE OPTIONWINDOW_SIZE = ''# WINDOW_SIZE = '--window-size=1920,1080'# START MAXIMIZED OPTIONSTART_MAXIMIZED = '--start-maximized'@propertydef _options(self):"""chrome浏览器特有的操作属性:return:"""if self.OPTION_MARK:chrome_option = self._option()chrome_option.headless = self.HEADLESSif self.WINDOW_SIZE:chrome_option.add_argument(self.WINDOW_SIZE)if self.START_MAXIMIZED:# chrome_option.add_argument(self.START_MAXIMIZED)chrome_option.add_argument('--headless')chrome_option.add_argument('--disable-gpu')for k, v in self.EXPERIMENTAL.items():chrome_option.add_experimental_option(k, v)return chrome_optionreturn None@propertydef browser(self):"""启动chrome浏览器并进行初始配置:return:"""if self._options:chrome = self._driver(self._path, options=self._options)else:chrome = self._driver(self._path)if self.METHOD_MARK:chrome.implicitly_wait(self.IMPLICITLY_WAIT_TIME)chrome.set_page_load_timeout(self.PAGE_LOAD_TIME)chrome.set_script_timeout(self.SCRIPT_TIMEOUT)return chromeclass IE(BROWSER):IMPLICITLY_WAIT_TIME = 20PAGE_LOAD_TIME = 10SCRIPT_TIMEOUT = 10WINDOWS_SIZE = (1600, 1024)CLEAN_SESSION = TrueATTACH_TIMEOUT = 10000MARK = Truedef __init__(self):super(IE, self).__init__(browser_type=Ie,option_type=IeOptions,driver_path=super().IE_DRIVER_PATH)@propertydef _options(self):"""ie浏览器特有的操作属性:return:"""ie_option = self._option()ie_option.browser_attach_timeout = self.ATTACH_TIMEOUTie_option.ensure_clean_session = self.CLEAN_SESSIONreturn ie_option@propertydef browser(self):"""启动ie浏览器并进行初始配置:return:"""if self.MARK:ie = self._driver(self._path, options=self._options)else:ie = self._driver(self._path)ie.implicitly_wait(self.IMPLICITLY_WAIT_TIME)ie.set_page_load_timeout(self.PAGE_LOAD_TIME)ie.set_script_timeout(self.SCRIPT_TIMEOUT)ie.set_window_size(*self.WINDOWS_SIZE)return ieclass FIREFOX(BROWSER):def __init__(self):super(FIREFOX, self).__init__(browser_type=Firefox,option_type=FirefoxOptions,driver_path=super().FIREFOX_DRIVER_PATH)@propertydef _options(self):returndef browser(self):return self._driver(self._path)class OPERA(BROWSER):def __init__(self):super(OPERA, self).__init__(browser_type=Opera,option_type=OperaOptions,driver_path=super().OPERA_DRIVER_PATH)@propertydef _options(self):returndef browser(self):return self._driver(self._path)
最后我邀请你进入我们的【软件测试学习交流群:785128166】, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路
作为一个软件测试的过来人,我想尽自己最大的努力,帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源,现在免费分享给大家,有需要的小伙伴可以关注【公众号:程序员二黑】自提!