用于分裂自动化库的 Pytest 插件
项目描述
用于splinter的pytest插件。
pytest-splinter4 是 pytest-splinter 的一个分支,具有 添加的功能和修复以支持更新版本的 pytest、pytest-xdist、 splinter >= 0.17.0和selenium >= 4.0。
安装
python -m pip install pytest-splinter4
特征
合理的默认值
驱动程序可执行路径参数
使用 chrome、firefox 或 edge 时,executable_path驱动程序参数的默认值设置为也在当前工作目录中搜索 chrome/gecko/edgedriver。
浏览器装置
以下夹具提供了splinter.Browser()的实例
- 浏览器
splinter 浏览器的新实例。Fixture 是会话范围的,因此每个测试会话都会启动一次浏览器进程,但浏览器的状态将是干净的(当前页面是空白的,cookie 是干净的)。
- session_browser
除了生命周期外,与浏览器相同。此夹具是会话范围的,因此只会在整个测试会话结束时完成。如果您想通过减少测试隔离来加速您的测试套件支付,这很有用。
- browser_instance_getter
创建浏览器实例的函数。仅当您需要在单个测试中同时拥有多个浏览器实例时,才需要此夹具。例子:
@pytest.fixture def admin_browser(request, browser_instance_getter): """Admin browser fixture.""" # browser_instance_getter function receives parent fixture -- our admin_browser return browser_instance_getter(request, admin_browser) def test_2_browsers(browser, admin_browser): """Test using 2 browsers at the same time.""" browser.visit('http://google.com') admin_browser.visit('http://admin.example.com')
硒夹具
以下夹具提供对 selenium 参数的支持。它们仅在使用基于 Selenium 的驱动程序时使用。
- splinter_selenium_implicit_wait
隐式等待超时传递给 Selenium webdriver。Fixture 从命令行选项 splinter-implicit-wait 获取值(见下文)
- splinter_selenium_speed
Selenium 的速度,如果不是 0,那么它将在每个 selenium 命令之间休眠。对调试/演示很有用。Fixture 从命令行选项 splinter-speed 获取值(见下文)
- splinter_selenium_socket_timeout
webdriver 和浏览器之间通信的套接字超时。Fixture 从命令行选项 splinter-socket-timeout 获取值(见下文)
碎片夹具
以下fixture 提供对splinter 参数的支持。
- splinter_wait_time
显式等待超时(通过wait_for_condition等待显式条件)。默认值来自命令行选项 splinter-wait-time (见下文)
- splinter_webdriver
要使用的 Splinter 的 webdriver 名称。默认值来自命令行选项 splinter-webdriver(见下文)。
要使 pytest-splinter 始终使用某些 webdriver,请覆盖conftest.py文件中的固定装置。例子:
import pytest @pytest.fixture(scope='session') def splinter_webdriver(): """Override splinter webdriver name.""" return 'chrome'
- splinter_remote_url
要使用的 Webdriver 远程 URL。默认值来自命令行选项 splinter-remote-url(见下文)。
仅当所选的WebDriver名称为“远程”时,才会使用此情况。
- splinter_remote_name
运行远程 Webdriver 时使用的浏览器名称。
This will be used only if the selected webdriver name is 'remote'.
- splinter_session_scoped_browser
每个测试会话使用一个浏览器实例。默认值来自命令行选项 splinter-session-scoped-browser (见下文)
- splinter_file_download_dir
目录,浏览器会自动将浏览过程中遇到的文件下载到该目录。例如,当您单击某个下载链接时。默认情况下,它是一个临时目录。目前只有firefox驱动支持自动下载文件。
- splinter_download_file_types
要自动下载的内容类型的逗号分隔列表。默认情况下,它是所有已知的系统 mime 类型(通过 mimetypes 标准库)。
- splinter_browser_load_condition
浏览器加载条件,一个应该返回 True 的 python 函数。如果函数返回 False,它将运行多次,直到达到超时。
- splinter_browser_load_timeout
浏览器加载条件超时(以秒为单位),超时后将引发异常 WaitUntilTimeout。
- splinter_wait_time
浏览器显式等待超时(以秒为单位),在此超时后将引发异常 WaitUntilTimeout。
- splinter_driver_kwargs
Webdriver 关键字参数,传递给 selenium webdriver 构造函数的字典(在应用 firefox 首选项后)
import pytest from pathlib import Path @pytest.fixture def splinter_driver_kwargs(): """ Webdriver kwargs for Firefox. https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver """ return {"service_log_path": Path("/log/directory/geckodriver.log")}
- splinter_window_size
浏览器初始化时浏览器窗口的大小。表单中的元组 (<width>, <height>)。默认为 (1366, 768)
- splinter_logs_dir
驱动程序日志目录。默认为“日志”。
- splinter_screenshot_dir
浏览器截图目录。默认为“日志/{test_function_name}”。
此夹具从命令行选项 splinter-screenshot-dir获取值(见下文)。
- splinter_make_screenshot_on_failure
pytest-splinter 是否应该在测试失败时截取浏览器截图?此夹具从命令行选项 splinter-make-screenshot-on-failure获取值(见下文)。
- splinter_screenshot_encoding
测试失败时对html 屏幕截图的编码。默认为 UTF-8。
- splinter_browser_class
用于浏览器实例的类。默认为pytest_splinter.plugin.Browser。
- splinter_clean_cookies_urls
用于清除 cookie 的其他 URL 列表。默认情况下,在为测试准备浏览器期间,pytest-splinter 仅清除上次测试中最后访问的 url 的 cookie,因为按照设计,不可能通过 webdriver 协议一次清除所有域中的所有 cookie。如果您知道 url 列表、需要清除 cookie 的域(例如https://facebook.com),则可以解决此限制。如果是这样,您可以覆盖此固定装置并将这些 url 放在那里,pytest-splinter 将访问它们中的每一个并清除每个域的 cookie。
- splinter_headless
以无头模式运行 Chrome。默认为假。http://splinter.readthedocs.io/en/latest/drivers/chrome.html#using-headless-option-for-chrome
仅限火狐
- splinter_firefox_profile_preferences
Firefox 配置文件首选项,传递给 selenium webdriver 的 profile_preferences 的字典
- splinter_firefox_profile_directory
Firefox 配置文件目录用作 selenium 创建的 Firefox 配置文件的模板。默认情况下,它直接在 pytest_splinter/profiles/firefox 中为空
命令行选项
- –分裂隐式等待
Selenium webdriver 隐式等待。秒(默认值:5)。
- – 分裂速度
selenium webdriver 速度(从命令到命令)。秒(默认值:0)。
- --splinter-socket-timeout
Selenium webdriver 套接字超时,用于 webdriver 和浏览器之间的通信。秒(默认值:120)。
- --splinter-webdriver
要使用的 Webdriver 名称。(默认:火狐)。选项:
火狐
偏僻的
铬合金
有关更多详细信息,请参阅 splinter 和 selenium 的文档。
- --splinter-remote-url
要使用的 Webdriver 远程 URL。(默认值:无)。仅在选定的WebDriver名称为“远程”时才使用。
有关更多详细信息,请参阅 splinter 和 selenium 的文档。
- –分裂远程名称
运行远程 Webdriver 时使用的浏览器名称。
- --splinter-session-scoped-browser
pytest-splinter 应该在每个测试会话中使用一个浏览器实例。选项是“真”或“假”(默认值:“真”)。
- –splinter-make-screenshot-on-failure
pytest-splinter 应该在测试失败时获取浏览器屏幕截图。选项是“真”或“假”(默认值:“真”)。
- –splinter-screenshot-dir
pytest-splinter 浏览器截图目录。默认为当前目录。
- – 无头分裂
覆盖splinter_headless夹具。选项为“真”或“假”,默认值:“真”。 http://splinter.readthedocs.io/en/latest/drivers/chrome.html#using-headless-option-for-chrome https://splinter.readthedocs.io/en/latest/drivers/firefox.html#using -headless-option-for-firefox
浏览器夹具
如上所述,浏览器夹具是 splinter 的 Browser 对象的一个实例,但有一些覆盖。
- 访问
通过固定装置增加了等待每次浏览器访问条件的可能性。
- 等待条件
复制 selenium 的 wait_for_condition 的方法,不同的是条件在 python 中,所以你可以做任何你想做的事情,而不仅仅是通过 browser.evaluate_script 执行 javascript。
测试失败自动截图
当测试失败时,了解原因很重要。当测试在您无法调试的持续集成服务器上运行时(使用 –pdb),这变得很困难。为简化起见,可以使用浏览器固定装置的特殊行为,它会在测试失败时截取屏幕截图,并将其放入具有与jenkins 插件兼容的命名约定的文件夹中 。浏览器页面的 html 内容也被存储,这对于调试 html 源代码很有用。
创建屏幕截图与pytest-xdist 插件完全兼容,并将自动通过通信通道从工作节点传输屏幕截图。
如果测试(使用浏览器夹具)失败,您应该在以下路径中获取屏幕截图文件:
<splinter-screenshot-dir>/my.dotted.name.test.package/test_name-browser.png <splinter-screenshot-dir>/my.dotted.name.test.package/test_name-browser.html
用于存储屏幕截图的splinter-screenshot-dir由夹具生成,并且可以通过命令行参数提供,如上文配置选项部分所述。
默认情况下启用对测试失败进行截图。它可以通过splinter_make_screenshot_on_failure夹具进行控制,其中 return False 跳过它。您也可以通过命令行参数禁用它:
pytest tests/functional --splinter-make-screenshot-on-failure=false
如果截屏失败,将发出 pytest 警告,可以使用pytest的-rw参数查看。
例子
def test_using_a_browser(browser):
"""Test using real browser."""
url = "http://www.google.com"
browser.visit(url)
browser.fill('q', 'splinter - python acceptance testing for web applications')
# Find and click the 'search' button
button = browser.find_by_name('btnK')
# Interact with elements
button.click()
assert browser.is_text_present('splinter.cobrateam.info'), "splinter.cobrateam.info wasn't found... We need to"
' improve our SEO techniques'
接触
执照
该软件在MIT 许可下获得许可
请参阅许可证文件
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
内置分布
pytest_splinter4-0.3.0 -py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 675167c2cc793264c1f7b1c4ddfc9460c69553077f54ca8c20a088bf3f7ba891 |
|
| MD5 | eb3ee36ec37692e2b84d3aac6a9e0b4d |
|
| 布莱克2-256 | 7f64a79abe27191291d64882cf6672565ce048d22d27c62537e2a95cd7e42ec0 |