页面对象模型:Page object models
Page Object是一种设计模式,已在测试自动化中流行,以增强测试维护并减少代码重复。页面对象是面向对象的类,测试只要需要与该页面的UI进行交互,便会使用该页面对象类的方法。好处是,如果页面的UI更改了,则测试本身不需要更改,只需更改页面对象中的代码即可。
页面对象设计模式优点:
- 测试代码和页面特定代码之间有清晰的区分, 提高可维护性
- 集中管理元素对象
- 代码可复用性强,成本低
模式层级设计分三层:
- 页面层:主要封装一些定位方法、等待方法…
- 操作层:调用页面层的方法,操作元素
- 业务层:规划好每个业务的元素定位,服务于操作层
下图中针对文件夹名称作了备注信息,仅供参考:
common/basePage.py
import logging
from . import pathPage as PG
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class BasePage:
def __init__(self, driver: WebDriver):
self.driver = driver
def page_waits(self, located, time_out, poll_frequency):
"""
获取元素等待
:param located: 元素
:param time_out: 超时
:param poll_frequency: 轮询时间
:return:
"""
WebDriverWait(self.driver, time_out, poll_frequency).until(EC.visibility_of_element_located(located))
def page_element_is_exits(self, located, image):
"""
判断页面元素是否存在,存在就返回True
:param located: 元素
:param image: 图片
:return:
"""
try:
WebDriverWait(self.driver, 20, 1).until(EC.presence_of_element_located(located))
except Exception as