跳转到主要内容

Splinter插件,用于pytest测试框架

项目描述

Splinter 插件用于 pytest 运行器

Join the chat at https://gitter.im/pytest-dev/pytest-splinter https://img.shields.io/pypi/v/pytest-splinter.svg https://img.shields.io/pypi/pyversions/pytest-splinter.svg https://img.shields.io/coveralls/pytest-dev/pytest-splinter/master.svg https://github.com/pytest-dev/pytest-splinter/actions/workflows/tests.yml/badge.svg Documentation Status

安装 pytest-splinter

pip install pytest-splinter

功能

该插件提供了一组固定装置,用于使用 splinterpytest 中进行浏览器测试

固定装置

  • browser

    获取 splinter 的浏览器。固定装置是会话范围的,因此浏览器进程在每个测试会话中只启动一次,但浏览器的状态将是干净的(当前页面是空白,cookie 已清除)。

  • session_browser

    browser 相同,除了生存期。这个固定装置是会话范围的,所以它只会在整个测试会话结束时才被终止。如果你想通过减少测试隔离来加快测试套件,这将很有用。

  • browser_instance_getter

    创建浏览器实例的函数。只有当你需要在单个测试中同时拥有多个 Browser 实例时才需要这个固定装置。用法示例

@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')
  • splinter_selenium_implicit_wait

    传递给 Selenium WebDriver 的隐式等待超时。固定装置从命令行选项 splinter-implicit-wait 获取值(见下文)

  • splinter_wait_time

    显式等待超时(通过 wait_for_condition 等待显式条件)。固定装置从命令行选项 splinter-wait-time 获取值(见下文)

  • splinter_selenium_speed

    Selenium 的速度,如果不为 0,则在每个 Selenium 命令之间休眠。对于调试/演示很有用。固定装置从命令行选项 splinter-speed 获取值(见下文)

  • splinter_selenium_socket_timeout

    WebDriver 和浏览器之间通信的套接字超时。固定装置从命令行选项 splinter-socket-timeout 获取值(见下文)

  • 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

    要使用的 Splinter WebDriver 远程 URL(可选)。固定装置从命令行选项 splinter-remote-url 获取值(见下文)。如果选择了 'remote' WebDriver 名称,则将使用。

  • splinter_session_scoped_browser

    pytest-splinter 应该在每个测试会话中使用单个浏览器实例。固定装置从命令行选项 splinter-session-scoped-browser 获取值(见下文)

  • splinter_file_download_dir

    浏览器将在浏览期间自动下载文件的目录。例如,当您点击某些下载链接时。默认情况下是一个临时目录。目前仅支持 firefox 驱动程序自动下载文件。

  • splinter_download_file_types

    要自动下载的内容类型的逗号分隔列表。默认情况下是所有已知系统 MIME 类型(通过 mimetypes 标准库)。

  • splinter_browser_load_condition

    浏览器加载条件,Python 函数,它应该返回 True。如果函数返回 False,它将运行几次,直到达到以下超时。

  • splinter_browser_load_timeout

    浏览器加载条件超时(以秒为单位),在此超时后,将引发 WaitUntilTimeout 异常。

  • splinter_wait_time

    浏览器显式等待超时时间(秒),超过此超时将引发 WaitUntilTimeout 异常。

  • splinter_firefox_profile_preferences

    Firefox配置文件偏好设置,一个字典,用于传递给selenium webdriver的profile_preferences。

  • splinter_firefox_profile_directory

    使用selenium创建Firefox配置文件时用作模板的Firefox配置文件目录。默认情况下,它是pytest_splinter/profiles/firefox下的一个空目录。

  • 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")}

以下代码片段配置Chrome忽略证书错误并设置特定窗口大小。

import pytest
from selenium import webdriver

@pytest.fixture(scope='session')
def splinter_driver_kwargs():
    """Override Chrome WebDriver options"""
    chrome_options = webdriver.ChromeOptions()

    # List of Chromium Command Line Switches
    # https://peter.sh/experiments/chromium-command-line-switches/
    chrome_options.add_argument("--window-size=1440,1200")
    chrome_options.add_argument("--ignore-ssl-errors=yes")
    chrome_options.add_argument("--ignore-certificate-errors")

    return {"options": chrome_options}
  • splinter_window_size

    浏览器初始化时浏览器窗口的大小。形式为(<宽度>, <高度>)的元组。默认为(1366, 768)。

  • splinter_screenshot_dir

    pytest-splinter浏览器截图目录。此占位符从命令行选项(见下文)获取值。

  • splinter_make_screenshot_on_failure

    pytest-splinter是否应该在测试失败时捕获浏览器截图?此占位符从命令行选项(见下文)获取值。

  • splinter_screenshot_encoding

    测试失败时html截图的编码。默认为UTF-8。

  • splinter_screenshot_getter_html

    获取浏览器html截图的函数。默认情况下,它使用给定的路径和splinter_screenshot_encoding编码保存browser.html。

  • splinter_screenshot_getter_png

    获取浏览器图像(png)截图的函数。默认情况下,它使用给定路径调用browser.save_sceenshot。

  • splinter_driver_executable

    webdriver可执行文件的文件系统路径。此占位符从命令行选项(见下文)获取值。

  • splinter_browser_class

    用于浏览器实例的类。默认为pytest_splinter.plugin.Browser。

  • splinter_clean_cookies_urls

    要清除cookie的附加URL列表。默认情况下,pytest-splinter在测试准备期间仅清除上次访问的URL的cookie,因为无法通过webdriver协议一次性清除所有域的cookie,这是设计上的限制。如果知道需要清理cookie的URL列表(例如https://facebook.com),可以通过覆盖此占位符并将这些URL放入其中,pytest-splinter将访问每个URL并为每个域清理cookie。

  • splinter_headless

    以无头模式运行Chrome。默认为false。请参阅http://splinter.readthedocs.io/en/latest/drivers/chrome.html#using-headless-option-for-chrome

命令行选项

  • –splinter-implicit-wait

    Selenium webdriver隐式等待。秒数(默认:5)。

  • –splinter-speed

    Selenium webdriver命令之间的速度。秒数(默认:0)。

  • –splinter-socket-timeout

    Selenium webdriver与浏览器之间通信的套接字超时。秒数(默认:120)。

  • –splinter-webdriver

    要使用的Webdriver名称。(默认:firefox)选项

    • firefox

    • remote

    • chrome

    有关详细信息,请参阅Splinter和Selenium的文档。

  • –splinter-remote-url

    要使用的Webdriver远程URL。(默认:None)。仅当选定的webdriver名称为“remote”时使用。

    有关详细信息,请参阅Splinter和Selenium的文档。

  • –splinter-session-scoped-browser

    pytest-splinter应该为每个测试会话使用单个浏览器实例。选项为“true”或“false”(默认:“true”)。

  • –splinter-make-screenshot-on-failure

    pytest-splinter应该在测试失败时捕获浏览器截图。选项为“true”或“false”(默认:“true”)。

  • –splinter-screenshot-dir

    pytest-splinter浏览器截图目录。默认为当前目录。

  • –splinter-webdriver-executable

    webdriver可执行文件的文件系统路径。由chrome driver使用。默认为None,在这种情况下,shell PATH变量设置决定了可执行文件的位置。

  • –splinter-headless

    覆盖 splinter_headless 配置项。选项为 'true' 或 'false',默认值:'true'。 http://splinter.readthedocs.io/zh/latest/drivers/chrome.html#using-headless-option-for-chrome https://splinter.readthedocs.io/zh/latest/drivers/firefox.html#using-headless-option-for-firefox

浏览器配置项

如上所述,浏览器是一个由创建 splinter 的 Browser 对象生成的配置项,但有一些覆盖。

  • 访问

    通过具有配置项来增加了在每个浏览器访问上等待条件的可能性。

  • wait_for_condition

    方法复制了 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 配置项进行控制,其中返回 False 跳过。您还可以通过命令行参数禁用它。

pytest tests/functional --splinter-make-screenshot-on-failure=false

如果截图失败,pytest 将发出警告,可以使用 -rw 参数为 pytest 查看警告。

Python3 支持

支持 Python3,请确保您有 splinter 的最新版本,因为它最近才添加。

示例

test_your_test.py

def test_some_browser_stuff(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'

联系方式

如果您有任何问题、错误报告、建议等,请在此 GitHub 项目页面 上创建问题。

许可证

此软件根据 MIT 许可证 许可。

查看 许可证文件

© 2014 Anatoly Bubenkov,Paylogic International 及他人。

作者

Anatoly Bubenkov

原始想法和实现,新功能和改进

以下人员为 pytest-splinter 做出了贡献,按字母顺序排列

变更日志

3.3.2

  • 修复了 Firefox 的 mouse_over 补丁,使其与最新的 splinter (0.18.1) 兼容(mpasternak),

3.3.1

  • 修复了处理命令行选项 splinter_headless(mpasternak)

3.3.0

  • 支持无头 Firefox(mpasternak)

3.2.0

  • 传递 –splinter-headless 不带参数默认为 'true' #123(tony)

3.1.0

  • 移除用于重试的不必要的webdriver补丁,此行为现在已成为splinter的一部分。(jsfehler)

  • 将最低splinter版本提升到0.13.0(jsfehler)

3.0.0

  • 移除python2支持(bubenkoff)

2.1.0

  • 添加对Django和Flask Splinter浏览器的支持,这些浏览器没有driver属性 #146 (michelts)

2.0.1

  • 解决与pytest >= 4的兼容性问题

2.0.0

  • 将最低splinter版本提升到0.9.0(jsfehler)

  • 移除phantomjs支持。(jsfehler)

1.9.1

  • 修复在pytest-xdist激活时截图时出现的utf-8解码警告 #108 (jsfehler)

1.9.0

  • 使用getfixturevalue代替getfuncargvalue #97 (pelme)

  • 添加Chrome无头支持(miohtama)

1.8.6

  • 修复与xdist一起使用时无法截图的问题(youtux)

1.8.5

  • 修复xdist问题 #94 (bubenkoff)

1.8.3

  • 配置文件与geckodriver+远程webdriver不兼容 #90) (pelme)

1.8.2

  • 修复缺失的switch_to方法(某些selenium expected_conditions没有它将无法工作,参见 #93

1.8.1

  • 确保node的splinter_failure始终存在(bubenkoff, pelme)

  • 正确处理跳过的测试(bubenkoff, schtibe)

1.8.0

  • 限制prepare_browser的retry行为(bubenkoff)

  • 针对清理cookies(Edge浏览器)的解决方案(bubenkoff)

1.7.8

  • 使能够覆盖默认的–splinter-wait-time值(magnus-staberg)

1.7.7

  • 使能够覆盖默认的–splinter-webdriver(pelme)

  • 修复函数作用域的截图问题(pelme)

1.7.6

  • 支持pytest 3(bubenkoff)

  • 减少对splinter的visit的依赖(bubenkoff)

1.7.5

  • 为路径分隔符转义截图路径(bubenkoff)

1.7.4

  • 使用tmpdir_factory获取会话作用域的tmpdir(RonnyPfannschmidt, bubenkoff)

1.7.3

  • 修复Firefox打开缺少编解码器扩展时冻结的问题(olegpidsadnyi)

1.7.2

  • 修复pytest>=2.9.0时截图的问题(olegpidsadnyi)

1.7.1

  • 修复pytest警告(firebirdberlin)

  • 移除firefox firstrun脚本(aaugustin, bubenkoff)

1.7.0

  • 添加在浏览器清理过程中清理指定域的cookie的可能性,记录cookie清理(bubenkoff)

1.6.6

  • 使截图编码更加灵活(bubenkoff)

1.6.2

  • 将超时传递到恢复的连接(bubenkoff)

1.6.0

  • 添加html截图(bubenkoff, blueyed)

1.5.3

  • 远程webdriver修复(bubenkoff)

1.5.2

  • 尊重splinter_make_screenshot_on_failure(bubenkoff)

1.5.1

  • 使用原生的selenium套接字超时功能(pelme)

1.5.0

  • 支持pytest tmpdir_factory(bubenkoff)

  • 依赖于splinter 0.7.3,移除先前的status_code monkey patch(pelme)

  • 添加选项–splinter-wait-time以指定splinter显式等待超时(pelme)

1.4.6

  • 确保基本tempdir存在(bubenkoff)

1.4.0

  • 引入splinter_browser_class fixture(bubenkoff, ecesena)

1.3.8

  • 正确处理zope.testbrowser splinter驱动程序(bubenkoff)

1.3.7

  • splinter_selenium_implicit_wait作为wait_time传递给splinter Browser(lrowe)

1.3.6

  • 正确尊重webdriver可执行命令行选项(bubenkoff, bh)

1.3.5

  • 为phantomjs和chrome添加选项–splinter-webdriver-executable(sureshvv)

1.3.4

  • 使browser_instance_getter会话作用域,添加session_browser fixture(bubenkoff, sureshvv)

1.3.3

  • 使mouse_over与更多用例兼容(bubenkoff)

1.3.1

  • 正确处理测试运行期间的driver切换(bubenkoff)

  • 尊重splinter_session_scoped_browser fixture(bubenkoff)

1.2.10

  • 处理截图保存期间的异常(blueyed, bubenkoff)

  • 改进文档(blueyed)

1.2.9

  • 以懒方式恢复status_code(bubenkoff)

1.2.7

  • 修复自动下载pdf内容类型的问题(bubenkoff)

1.2.4

  • 修复如果pytest-xdist未安装,则测试运行失败的测试,因为它是一个完全可选的依赖项(bubenkoff, slafs)

1.2.3

  • 优化浏览器实例准备时的异常处理(bubenkoff)

  • 需要pytest(bubenkoff)

1.2.0

  • 测试失败时自动截图(bubenkoff)

  • 改进浏览器准备流程(bubenkoff)

  • 将布尔配置选项描述得更清楚(bubenkoff)

1.1.1

  • 每次测试运行时恢复浏览器参数,而不是浏览器启动时只恢复一次(bubenkoff)

1.1.0

  • 为单个测试添加了多个浏览器实例的选项(amakhnach, bubenkoff)

1.0.4

  • 修复了浏览器固定程序,默认支持splinter_browser_load_condition和splinter_browser_load_timeout。 (markon)

1.0.3

  • 修复了setup.py中的Unicode问题(bubenkoff, valberg)

1.0.2

  • wait_for_condition现在接收pytest_bdd.plugin.Browser对象,而不是selenium webdriver对象(bubenkoff)

1.0.1

  • 重构和清理(bubenkoff)

1.0.0

  • 首次公开发布

项目详情


发布历史 发布通知 | RSS订阅

下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

pytest-splinter-3.3.2.tar.gz (24.1 kB 查看散列)

上传时间 源代码

构建分发

pytest_splinter-3.3.2-py3-none-any.whl (18.3 kB 查看散列)

上传时间 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面