1、简介
SeleniumBase 的pytest 插件允许您从 CLI(命令行界面)自定义测试运行,它添加了用于设置/启用浏览器类型、仪表板模式、演示模式、无头模式、移动模式、多线程模式、记录器模式、重用的选项-会话模式、代理配置、用户代理配置、浏览器扩展、html 报告模式等。
2、常用运行命令
2.1、高亮模式运行:
- pytest test_专家管理.py --demo
- pytest my_first_test.py --demo --demo-sleep=1.2
2.2、无UI界面的运行
- pytest test_专家管理.py --headless
2.3、测试浏览器兼容性
- pytest test_专家管理.py【不添加,默认chrome】
- pytest test_专家管理.py --browser=chrome
- pytest test_专家管理.py --browser=firefox
- pytest test_专家管理.py --browser=edge
- pytest test_专家管理.py --browser=opera
- pytest test_专家管理.py --browser=ie
2.4、所有的登陆共用一个浏览器
- pytest test_专家管理.py --reuse-session
- pytest test_suite.py --reuse-session --crumbs 1
2.5、图形用户界面测试运行器
- python gui_test_runner.py
- gui_test_runner.py 代码示例
"""
GUI TEST RUNNER
Run by Typing: "python gui_test_runner.py"
(Use Python 3 - There are GUI issues when using Python 2)
"""
import os
from tkinter import Tk, Frame, Button, Label
class App:
def __init__(self, master, test):
self.test = test
frame = Frame(master)
frame.pack()
self.label = Label(root, width=40).pack()
self.title = Label(frame, text="", fg="black").pack()
self.title1 = Label(frame, text=("Run a Test in Chrome:"), fg="blue", ).pack()
self.run1 = Button(frame, command=self.run_1, text=(f"pytest {self.test} --browser=chrome"), fg="green", ).pack()
self.title2 = Label(frame, text=("Run a Test in Firefox:"), fg="blue", ).pack()
self.run2 = Button(frame, command=self.run_2, text=(f"pytest {self.test} --browser=firefox"), fg="green", ).pack()
self.title8 = Label(frame, text="Run a Test with ie:", fg="blue", ).pack()
self.run8 = Button(frame, command=self.run_8, text=(f"pytest {self.test} --browser=ie"), fg="green", ).pack()
self.title5 = Label(frame,text="Run a Test in edge :",fg="blue",).pack()
self.run5 = Button(frame,command=self.run_5,text=(f"pytest {self.test} --browser=edge"),fg="green",).pack()
self.title3 = Label(frame,text="Run a Test with Demo:",fg="blue",).pack()
self.run3 = Button(frame,command=self.run_3,text=(f"pytest {self.test} --demo --reuse-session"),fg="green",).pack()
self.title4 = Label(frame,text="Run a Test with 无GUI :",fg="blue",).pack()
self.run4 = Button(frame,command=self.run_4,text=(f"pytest {self.test} --headless"),fg="green",).pack()
self.title6 = Label(frame,text="Run a Test Suite with a Test Report:",fg="blue",).pack()
self.run6 = Button(frame,command=self.run_6,text=(f"pytest {self.test} --html=report.html --reuse-session"),fg="green",).pack()
self.end_title = Label(frame, text="", fg="black").pack()
self.quit = Button(frame, text="QUIT", command=frame.quit).pack()
def run_1(self):
os.system(f"pytest -s {self.test} --browser=chrome")
def run_2(self):
os.system(f"pytest {self.test} --browser=firefox")
def run_3(self):
os.system(f"pytest {self.test} --demo --reuse-session")
def run_4(self):
os.system(f"pytest {self.test} --headless")
def run_5(self):
os.system(f"pytest {self.test} --browser=edge")
def run_6(self):
os.system(f"pytest {self.test} --html=report.html --reuse-session")
def run_8(self):
os.system(f"pytest {self.test} --browser=ie")
if __name__ == "__main__":
# 请输入你要测试的测试用例
test = "test_远程连接列表.py"
# 此处不用管
root = Tk()
root.title("运行测试用例GUI界面")
root.minsize(500, 500)
app = App(root, test)
root.mainloop()
2.6、在手机的浏览器上运行
- pytest test_专家管理.py --mobile
- 这里有很多参数可以配置,具体参考产品文档
- pytest test_swag_labs.py --mobile --metrics=“411,731,3”
- 运行指定CSS宽度、CSS高度和像素比率的移动测试
- pytest test_swag_labs.py --mobile --agent=“Mozilla/5.0 (Linux; Android 9; Pixel 3 XL)”
- 运行指定用户代理的移动测试
- 指定代理网站:https://developers.whatismybrowser.com/useragents/explore/
设备名称 开发规模 比率 实际尺寸 宽x高支柱
iPhone 5S 320x568 2 640x1136 0,56
iPhone 6 375x667 2 750x1334 0,56
iPhone6 Plus 414x736 3 1242x2208 0,56
iPhone6s 375x667 2 750x1334 0,56
iPhone6s Plus 414x736 3 1242x2208 0,56
iPhone7 375x667 2 750x1334 0,56
iPhone7 Plus 414x736 3 1242x2208 0,56
iPhone8 375x667 2 750x1334 0,56
iPhone8 Plus 414x736 3 1242x2208 0,56
iPhoneSE 320x568 2 640x1136 0,56
iPhoneX 375x812 3 1125x2436 0,46
iPhoneX - 无刘海 375x734 3 1125x2202 0,51
安卓一 320x569 1,5 480x853,5 0,56
宏达一号 M8 360x640 3 1080x1920 0,56
HTC 一号 M9 360x640 3 1080x1920 0,56
LG G2 360x640 3 1080x1920 0,56
摩托G 360x640 2 720x1280 0,56
摩托X 360x640 2 720x1280 0,56
摩托 X 2 360x640 3 1080x1920 0,56
连结 5 360x640 3 1080x1920 0,56
银河 S5 360x640 3 1080x1920 0,56
银河 S6 360x640 4 1440x2560 0,56
银河 S7 360x640 4 1440x2560 0,56
银河 S7 边缘 360x640 4 1440x2560 0,56
银河 S8 360x740 4 1440x2960 0,49
银河 S8+ 360x740 4 1440x2960 0,49
Xperia Z1 360x640 2 720x1280 0,56
Xperia Z2/Z3 360x640 3 1080x1920 0,56
Xperia Z3 Compact 360x640 4 1440x2560 0,56
连结 4 384x640 3 1152x1920 0,6
谷歌像素 411x731 2,6 1068,6x1900,6 0,56
谷歌像素 XL 411x731 3,5 1438,5x2558,5 0,56
Nexus 5X 411x731 2,6 1068,6x1900,6 0,56
连结 6 411x731 3,5 1438,5x2558,5 0,56
Nexus 6P 411x731 3,5 1438,5x2558,5 0,56
LG G3 480x853 3 1440x2559 0,56
三星注 4 480x853 3 1440x2559 0,56
Xperia C4 360x592 3 1080x1776 0,61
Xperia Z Ultra 360x592 3 1080x1776 0,61
2.7、使用[n]个线程运行测试
- pytest test_suite.py -n=4
- -n=4 --dist=loadscope
分布式执行时候,通过module、class分类测试用例方法 - -n=4 --dist=loadfile
分布式执行时候,通过 py 文件分类测试用例 - 组合:pytest -n=4 --reuse-session --headless -v --dashboard --html=report.html
2.8、失败重试机制
- pytest test_suite.py --reruns=2
- pytest my_first_test.py --reruns=1 --reruns-delay=1
2.9、查看测试报告
- pytest test_suite.py --dashboard --html=report.html
- pytest --dashboard --rs --headless
-r 测试报告、-s 详细信息
2.10、覆盖默认的settings.py文件
- pytest test_suite.pysettings-file=custom_settings.py
2.11、提取py文件中所有元素的 selector,并将测试用例替换为变量
sbase extract-objects my_first_test.py
将 myfirst_test.py 文件的 selector提取到 page_objects.py中
sbase inject-objects my_first_test.py -c
将page_objects.py中的元素变量替换到 myfirst_test.py
sbase revert-objects test_001.py -c
将文件恢复到为替换元素变量之前,-c并保留注释
2.12、在远程Selenium网格上运行测试
- pytest test_suite.py --server=ip --port=4444
2.13、pytest.ini常用配置
-v # 详细模式。打印每个测试运行的全名。
-q # 安静模式。运行测试时,在控制台输出中打印较少的详细信息。
-x # 第一个故障后,停止运行测试
--html=report.html # 在测试完成后创建详细的pytest html报告。
--collect-only | --co # 显示将运行哪些测试。
-n=NUM # 使用那么多线程执行测试。(加快测试运行速度!)
-s #打印报表。(在pytest.ini存在的情况下,默认情况下应处于启用状态。)
--junit-xml=report.xml # 在测试完成后创建JUnitXML报告。
--pdb # 如果测试失败,暂停运行并进入调试模式。(不要与CI一起使用!)
-m=MARKER # 使用指定的pytest标记运行测试。
2、命令行工具 - 不常用的
2.1、创建脚手架
- sbase mkdir ui_tests
创建用于运行SBase脚本的新文件夹,新文件夹包含默认配置文件,帮助新用户开始的示例测试,以及用于设置自定义测试框架。
2.2、使用样板代码创建新的SBase测试文件。
- sbase mkfile new_test.py
2.3、转换文件语言,转成中文,打印到界面
- sbase translate test_demo001.py --zh -p
重用浏览器会话,但删除测试之间的cookie ↩︎