当前位置:网站首页>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"
}
]
成果展示
自动化测试流程
测试报告

边栏推荐
猜你喜欢

LeetCode 1047 Remove all adjacent duplicates in a string

本科毕业六年,疫情期间备战一个月,四面阿里巴巴定级P7

easyExce模板填充生成Excel的实际操作,多sheet页处理

悲伤的摇滚乐

H264码流RTP封装方式详解

2022暑假 动态规划总结

First-line big factory software test interview questions and answer analysis, the strongest version of 2022...

etcd实现大规模服务治理应用实战

并发编程学习笔记 之 常用并发容器的概念及使用方法

敏捷组织 | 企业克服数字化浪潮冲击的路径
随机推荐
TDengine 助力西门子轻量级数字化解决方案
无知大V秀智商!台积电南京厂扩产28nm将击垮大陆晶圆代工业?无稽之谈!
Embedded Development: Embedded Fundamentals - Software Error Classification
我用两行代码实现了一个数据库!
面试突击:为什么 TCP 需要 3 次握手?
cv2 imread()函数[通俗易懂]
yarn的安装和使用(yarn安装mysql)
Chengdu | Changed to software testing, from zero income to over 10,000 monthly salary, a new turning point in life...
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
无代码开发平台角色设置入门教程
swagger @ApiModel @ApiModelProperty注解属性说明「建议收藏」
Test push | Ali Fliggy, Baidu, 58 (recruitment), Zhihu, Huanxin Network, Baiguoyuan, Ali (Lazada), Shenzhicheng, Yuanrong Qixing are recruiting
数字孪生万物可视 | 联接现实世界与数字空间
PromptBERT: Improving BERT Sentence Embeddings with Prompts
cpolar应用实例之助力航运客户远程办公
经验分享|编写简单易用的在线产品手册小妙招
GNOME将在Secure Boot被禁用时向用户发出警告 并准备提供安全帮助
UE4选不中半透明物体(半透明显示快捷键是啥)
低代码搭建高效管理房屋检测系统案例分析
如何把thinkphp5的项目迁移到阿里云函数计算来应对流量洪峰?