当前位置:网站首页>Detailed explanation of design ideas of webUI test framework
Detailed explanation of design ideas of webUI test framework
2022-07-29 20:32:00 【fluttering snow】
前言
自动化测试中,The most important thing is to build the framework,A good automated testing framework can greatly reduce maintenance costs,提高效率.这篇文章就简单介绍一下UIThe idea of building an automated testing framework.
基于POM+DDT+pytest+selenium+allure搭建的web自动化测试框架:https://github.com/tdx1997tdx/web_auto_testing
POM设计模式
POM,全程Page Object Model,is the testing industryUIMainstream design patterns for automation frameworks.The entire framework separates code and data,逻辑代码与测试代码分离,POM的结构如下:
- 基类:Mainly produces a series of functions that can be called in the page object.The existence of itself as a tool library.
- 页面对象类:逻辑代码部分.结合实际业务,Extract all pages that can be automated.The class contains the core elements of the page and the core process of the page.
- 测试用例类:测试代码部分.Used to stitch various page objects,Implement the final test flow.
- 测试数据类:It is used to extract all the data content that needs to be applied in the actual test process.
DDT数据驱动测试
数据驱动测试,Test cases are driven by external data sets,A different set of data to perform the same operation(a script),Test data and test operations are completely separated from the scripting design pattern,从数据文件读取输入数据,通过变量的参数化,将测试数据传入测试脚本,不同的数据文件对应不同的测试用例.
ddt优势如下:
代码复用率高,一次编写多条数据复用逻辑
异常排查效率高,测试执行隔离,数据间无影响
代码可维护性高,提高代码的易读性
项目实战讲解
项目结构介绍

- base_page:The base class mainly produces a series of functions that can be called in the page object.The existence of itself as a tool library.
- page_object:页面对象类,逻辑代码部分.结合实际业务,Extract all pages that can be automated.The class contains the core elements of the page and the core process of the page.
- test_case:Store the test case code implementation,Used to stitch various page objects,Implement the final test flow.
- data:存放测试数据,ddt实现的核心.
- config:Store general configuration
- report:存放allure测试报告
- log:存放日志
- utils:Store some general tool kits
base_page
POMmiddle class,Mainly encapsulate someselenium中常用的方法
base_page.py
from selenium.webdriver import Keys
from config.config import url as index_url
class BasePage:
# 构造函数
def __int__(self, driver):
self.driver = driver
# 访问url
def visit(self, url):
self.driver.get(url)
def visit_index(self):
self.driver.get(index_url)
# 元素定位
def locate(self, loc):
return self.driver.find_element(*loc)
# 输入文本
def input(self, loc, text):
self.locate(loc).send_keys(text)
# 单击
def click(self, loc):
self.locate(loc).click()
# refresh
def refresh_page(self):
self.driver.refresh()
# 按下回车键
def enter(self, loc):
self.locate(loc).send_keys(Keys.ENTER)
page_object
Stores page objects and the core business flow of the page
业务主页面:
index_page.py
from selenium.webdriver.common.by import By
from base_page.base_page import BasePage
class IndexPage(BasePage):
uri = "/"
# 核心元素
search_input = (By.XPATH, '//*[@id="layout"]/div/div[1]/div[2]/div/span/div[1]/span/span[1]/input')
def __init__(self, driver):
super().__int__(driver)
# core business flow
def search(self, txt):
self.visit_index()
self.input(self.search_input, txt)
self.enter(self.search_input)
测试用例中,We only relate to the search function,So abstract core elements and core business flows
- 核心元素:The framed part of the picture,That is, the search input box
- core business flow:
- 进入页面
- 定位输入框,并输入内容
- Click Enter to search for content
test_case
测试用例类,使用pytest框架搭建
from selenium import webdriver
from config.chrome_options import options
from page_object.login_page import LoginPage
from page_object.index_page import IndexPage
import allure
import pytest
from utils.json_tool import load_json
from utils.log_tool import logger
import time
@allure.feature('Search for module tests')
class TestSearch:
def setup_class(self):
self.driver = webdriver.Chrome(options=options())
lp = LoginPage(self.driver)
lp.login_with_cookie()
def teardown_class(self):
self.driver.quit()
@pytest.mark.parametrize("test_input,story,title", load_json("./data/search_data.json"))
def test_search(self, test_input, story, title):
""" 用例描述:搜索测试 """
# Dynamically add modules and headers
allure.dynamic.story(story)
allure.dynamic.title(title)
ip = IndexPage(self.driver)
ip.search(test_input["search_txt"])
logger.info(str(test_input) + "finish")
time.sleep(2)
@pytest.mark.parametrize和load_json组成ddt的核心,下面看看data中json文件的定义:
[
{
"data": {
"search_txt": "tdx"
},
"story": "用例1:输入tdx",
"title": "输入测试tdx"
},
{
"data": {
"search_txt": "mytdx"
},
"story": "用例2:输入mytdx",
"title": "输入测试mytdx"
},
{
"data": {
"search_txt": "三国"
},
"story": "用例3:Enter the Three Kingdoms",
"title": "Enter Test Three Kingdoms"
}
]
我们封装个load_jsonmethod to read data for data-driven testing
import json
# 读取json文件内容,Convert to a data-driven format
def load_json(path):
with open(path) as f:
data = json.load(f)
res = []
for i in data:
res.append((i["data"], i["story"], i["title"]))
return res
data
data中存放ddt的相关数据,Separate data and code
search_data.json
[
{
"data": {
"search_txt": "tdx"
},
"story": "用例1:输入tdx",
"title": "输入测试tdx"
},
{
"data": {
"search_txt": "mytdx"
},
"story": "用例2:输入mytdx",
"title": "输入测试mytdx"
},
{
"data": {
"search_txt": "三国"
},
"story": "用例3:Enter the Three Kingdoms",
"title": "Enter Test Three Kingdoms"
}
]
成果展示
自动化测试流程
测试报告

边栏推荐
猜你喜欢

MATLAB程序设计与应用 2. 第2章 MATLAB数据及其运算 2.1 MATLAB数值数据 && 2.2 MATLAB矩阵的表示 && 2.3 变量及其操作
![[数学]考研数学公式定理大总结](/img/7b/4eee16f4dcaa3d9ca76ccdbc2e1166.png)
[数学]考研数学公式定理大总结

MySQL 中的反斜杠 \\,怎么能这么坑?

接口测试工具之Postman详解

【AutoSAR 七 工具链简介】

DJI MID 360

What should I do if the Win11 network is unstable?The solution to frequent disconnection of wifi connection in Win11

【AutoSAR 十二 模式管理】

HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界

Answer these 3 interview questions correctly, and the salary will go up by 20K
随机推荐
【体系结构 四 存储结构】
cv2 imread()函数[通俗易懂]
[数学]线性代数复习总结
峰会(暑假每日一题 8)
【win10系统安装deepin双系统重启进不了win系统解决办法】
Custom Components -behaviors
pytorch构建YOLOV7网络结构
关于高考选志愿
C # CLI (common language infrastructure)
easyExce模板填充生成Excel的实际操作,多sheet页处理
本科毕业六年,疫情期间备战一个月,四面阿里巴巴定级P7
Chapter 01 Installation and use of MySQL under Linux [1. MySQL Architecture] [MySQL Advanced]
ESP8266-Arduino编程实例-I2C设备地址扫描
PromptBERT: Improving BERT Sentence Embeddings with Prompts
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
“加价”都换不来安全保障,雷克萨斯LM之殇,到底是谁的错?
不堆概念、换个角度聊多线程并发编程
【AutoSAR 九 C/S原理架构】
敏捷组织 | 企业克服数字化浪潮冲击的路径
OpenCV - 图像二值化处理 腐蚀膨胀 边缘检测 轮廓识别