
web自动化测试POM设计模式
测试代码和页面特定代码之间的区分更清晰,提高了代码的可维护性 元素对象的管理集中,降低了维护难度 代码具有一定的复用性,且成本较低 页面层:封装元素定位和等待等基本方法 操作层:调用页面层方法,执行元素操作 业务层:规划每个业务的元素定位,为操作层提供支持
发布日期:2021-05-13 21:48:54
浏览次数:12
分类:精选文章
本文共 4394 字,大约阅读时间需要 14 分钟。
Page Object Models
Page对象模型在测试自动化领域越来越流行,它的出现为了改善测试维护性并减少代码重复。Page对象是一个面向对象的类,用于与页面的UI交互。使用Page对象的好处是,当页面的UI发生变化时,只需要修改Page对象中的代码即可,无需修改测试代码本身。
Page Object Benefits
使用Page对象具有以下优点:
Page Object Structure
Page对象的设计分为三个层级:
File夹名称中的备注信息仅供参考,请参阅下方图片。
common/basePage.py
以下是Page对象的一个示例类:
import loggingfrom . import pathPage as PGfrom selenium.webdriver.remote.webdriver import WebDriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECclass BasePage: def __init__(self, driver): self.driver = driver def page_waits(self, located, time_out, poll_frequency): """获取元素等待""" WebDriverWait(self.driver, time_out, poll_frequency).until(EC.visibility_of_element_located(located)) def page_element_exists(self, located, image): """判断页面元素是否存在""" try: WebDriverWait(self.driver, 20, 1).until(EC.presence_of_element_located(located)) except Exception as e: self.page_screenshot(image) logging.error(located) raise e return True def page_element(self, located, image): """获取元素""" try: ele = self.driver.find_element(*located) except Exception as e: self.page_screenshot(image) logging.error(ele) raise e return ele def page_click(self, located, image): """点击元素""" self.page_waits(located, 20, 1) ele_click = self.page_element(located, image) try: ele_click.click() except Exception as e: self.page_screenshot(image) logging.error(located) raise e def page_clear(self, location, image): """清空输入框""" self.page_waits(location, 20, 1) ele_clear = self.page_element(location, image) try: ele_clear.clear() except Exception as e: self.page_screenshot(image) logging.error(location) raise e def page_send_keys(self, location, text, image): """输入文本信息""" self.page_waits(location, 20, 1) ele_send_keys = self.page_element(location, image) try: ele_send_keys.send_keys(text) except Exception as e: self.page_screenshot(image) logging.error(location) raise e def page_text(self, location, image): """获取文本信息""" self.page_waits(location, 20, 1) ele_text = self.page_element(location, image) try: text_info = ele_text.text except Exception as e: self.page_screenshot(image) logging.error(text_info) raise e return text_info
elementHandle/baiduHandle.py
以下是对百度搜索页面的处理类:
from .basePage import BasePagefrom .elementLocalization import BaiDuSearchclass HandleBaiDu: def __init__(self, driver): self.driver = driver self.bd = BaiDuSearch() self.bp = BasePage(self.driver) def baidu_search(self, input_info): """执行百度搜索""" self.bp.page_send_keys(self.bd.search_input, input_info, '百度搜索输入框') self.bp.page_click(self.bd.baidu_click, '鼠标点击百度一下') def assert_search_result(self): """断言搜索结果""" return self.bp.page_element_is_exits(self.bd.assert_result, '断言搜索结果')
elementLocalization/baiduLoc.py
以下是百度搜索页面的元素定位配置:
from selenium.webdriver.common.by import Byclass BaiDuSearch: search_input = (By.XPATH, '//*[@id="kw"]') baidu_click = (By.XPATH, '//*[@id="su"]') assert_result = (By.XPATH, '//*[@class="res-gap-right16"]')
testCases/conftest.py
以下是测试环境配置:
import pytestimport timefrom selenium import webdriver@pytest.fixture(scope='session', name='pre')def pre_init(): """初始化测试环境""" driver = webdriver.Chrome() driver.get('https://www.baidu.com/') driver.maximize_window() time.sleep(2) yield driver time.sleep(2) driver.quit()
testCases/test_baiduSearch.py
以下是百度搜索测试用例:
import pytestfrom .elementHandle import HandleBaiDuclass TestBaiduSearch: def test_baidu_search(self, pre): """百度搜索测试用例""" HandleBaiDu(pre).baidu_search('baidu') result = HandleBaiDu(pre).assert_search_result() print(result)
以上代码仅供参考,可根据实际需求进行调整和扩展。Pytest框架用于测试编写,你也可以创建入口文件并运行用例生成测试报告。
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年05月04日 06时57分53秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
python初学者容易犯的错误
2019-03-09
Qt之QImage无法获取图片尺寸(宽和高)
2019-03-09
推荐几篇近期必看的视觉综述,含GAN、Transformer、人脸超分辨、遥感等
2019-03-09
Java-类加载过程
2019-03-09
BUU-MISC-认真你就输了
2019-03-09
BUU-MISC-caesar
2019-03-09
【专题2:电子工程师 之 上位机】 之 【36.事件重载】
2019-03-09
【专题3:电子工程师 之 上位机】 之 【46.QT音频接口】
2019-03-09
一文学会JVM常见参数设置+调优经验(JDK1.8)
2019-03-09
一文理解设计模式--命令模式(Command)
2019-03-09
VTK:可视化之RandomProbe
2019-03-09
block多队列分析 - 2. block多队列的初始化
2019-03-09
Java时间
2019-03-09
不编译只打包system或者vendor image命令
2019-03-09
MySQL
2021-05-12
The wxWindows Library Licence (WXwindows)
2021-05-12
leetcode——第203题——虚拟头结点
2021-05-12
【编程】C语言入门:1到 100 的所有整数中出现多少个数字9
2021-05-12