PO模式:Page Object,PO模式是自动化测试项目开发实践的最佳设计模式之一。
核心思想:通过对界面元素的封装减少冗余代码,同时在后期维护中,若元素位置发生变化,只需要调整页面封装的代码,提高测试用例的可维护性、可读性。
优点:
减少了冗余代码;
业务代码和测试代码被分开,降低耦合性;
维护成本低;
缺点:
结构复杂:基于流程做了模块化的拆分
例子:自动发送短信
方法:Appium+PO模式+Pytest框架数据参数化
base模块:前置代码和基本操作,base_driver.py对应打开driver,base_action.py对应元素定位、点击按钮和输入。
page模块:对应操作页面,考虑手指测试的过程需要用到多少个页面,就在page模块中创建多少个文件。page.py统一入口,有多少个页面,就写多少个函数,并创建对应的对象。
scripts模块:测试脚本。
pytest.ini:配置文件。
base_action.py:
from selenium.webdriver.support.wait import WebDriverWait
class BaseAction:
def __init__(self, driver):
self.driver = driver
def find_element(self, location, timeout=10, poll=1):
"""
:param location: 元素位置
:param timeout: 设置10秒
:param poll: 多少秒找一次
:return:
"""
location_by, location_value = location
wait = WebDriverWait(self.driver, timeout, poll)
return wait.until(lambda x: x.find_element(location_by, location_value))
def find_elements(self, location, timeout=10, poll=1):
location_by, location_value = location
wait = WebDri