使用selenium的可视化测试网站的pytest插件

pytest-needle的Python项目详细描述


Pytest针

Build StatusCoverage StatusPyPI versionPython versionLicenseStatusRequirements StatusDocumentation StatusMaintainability

pytest针是needle的pytest实现。

它与针头非常相似,并且有很多相同的功能, 但它使用pytest-selenium来处理webdriver 并将needle实现为fixture,而不是让测试用例继承自needle的基本测试类。

安装

通过PIP安装:

pip install pytest-needle

从源安装:

cd /path/to/source/pytest-needle
python setup.py install 

示例

针pytest实现示例:

"""test_example.py"""fromselenium.webdriver.common.byimportByimportpytest@pytest.mark.elementdeftest_example_element(needle):"""Example for comparing individual elements    :param NeedleDriver needle: NeedleDriver instance    :return:    """# Navigate to web pageneedle.driver.get('https://www.google.com')# Take an element screen diffneedle.assert_screenshot('search_field',(By.ID,'tsf'))

为所有后续测试运行创建基线:

pytest --driver Chrome --needle-save-baseline test_example.py

在我们有基线之后,要运行测试,请使用:

pytest --driver Chrome test_example.py

选择网络驱动程序

要控制使用哪个浏览器,请使用pytest selenium中的--driver <BROWSER>。例如,将浏览器更改为Firefox:

pytest --driver Firefox test_example.py

设置视口的大小

您可以使用针固定装置上的set_viewport_size()设置浏览器视区的大小

deftest_example_viewport(needle):# Navigate to web pageneedle.set_viewport_size(width=1024,height=768)# Rest of the test ...

您还可以使用命令行参数--needle-viewport-size

pytest --driver Chrome --needle-viewport-size "1024 x 768" test_example.py

不包括区域

有时网页上的区域可能包含动态内容并导致错误的否定,或者更糟的是说服测试人员提出 可接受更改的阈值。相反,您可以选择隐藏这些区域,以避免 测试失败:

"""test_example.py"""fromselenium.webdriver.common.byimportByimportpytest@pytest.mark.maskdeftest_example_page_with_mask(needle):"""Example for comparing page with a mask    :param NeedleDriver needle: NeedleDriver instance    :return:    """# Navigate to web pageneedle.driver.get('https://www.google.com')# Take a entire page screen diff, ignore the doodle bannerneedle.assert_screenshot('search_page',threshold=60,exclude=[(By.ID,'hplogo'),(By.ID,'prm')])

在谷歌主页的情况下,涂鸦横幅经常变化,因此要想在视觉上回归日常需要 每次更新横幅时生成新基线。掩蔽只允许忽略横幅,而其余的 可以计算页面的。

发动机

默认情况下,指针使用pil引擎(needle.engines.pil_engine.Engine)进行屏幕截图。您也可以使用Perceptualdiff或ImageMagick代替PIL。

Perceptualdiff示例:

pytest --driver Chrome --needle-engine perceptualdiff test_example.py

ImageMagick示例:

pytest --driver Chrome --needle-engine imagemagick test_example.py

除了比pil快得多之外,perceptualdiff和imagemagick还可以在测试失败时生成diff png文件,突出显示基线图像和新屏幕截图之间的差异。

注意,要使用perceptualdiff引擎,首先需要downloadperceptualdiff二进制文件并将其放置在路径中。

要使用imagemagick引擎,您需要在计算机上安装一个软件包(例如sudo apt get install imagemagick on ubuntu或brew install imagemagick on osx)。

文件清理

每次运行测试时,针将在磁盘上创建新的屏幕截图图像,以便与基线屏幕截图进行比较。 然后由你决定是删除它们还是存档它们。要从成功的测试中删除屏幕截图,请使用:

pytest --driver Chrome --needle-cleanup-on-success test_example.py

任何不成功的测试都将保留在文件系统上。

文件输出

要指定基线图像路径,请使用:

pytest --driver Chrome --needle-baseline-dir /path/to/baseline/images

默认路径为./screenshots/baseline

要指定输出图像路径,请使用:

pytest --driver Chrome --needle-output-dir /path/to/output/images

默认路径是./screenshots

生成HTML报告

要生成HTML报告,请使用:

pytest --driver Chrome --html=report.html --self-contained-html

特别感谢

browserstack_logo

Special thanks to BrowserStack for providing automated browser testing, at no charge, for this project and other open source projects like this. With over 1000+ device, browser and os versions combinations to choose from and integrations with Travis CI this project could not be successful without the hard work of the BrowserStack team and their continued support of the open source community.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java的单元测试测试用例库。util。列出实现   java通过超类进行序列化/反序列化   java Android获取设备语言ISO字符串   java如何打开广告的行动。移动   Eclipse IDE for(嵌入式C/C++)开发者202012:经典的深色主题深黑色背景和菜单中的文本   java使用不同的内容动态创建TableView(JavaFX)   java JAXB将多个同名节点解组   java ClassDefNotFoundException,即使类由类加载器加载(服务器上安装了多个应用程序)   java有没有办法关闭在后端生成的MqttClient线程?   html如何在网站上的java小程序中包含图像?   java无法访问已分配给超类引用的子类实例变量   java在TableViewer中双击打开对话框   列出如何创建ListNode。JAVA   java如何从文本中输出的数组中放入随机图像