跳至主要内容

pytest自动化库插件

项目描述

pytest 插件用于 splinter.

pytest-splinter4 是基于 pytest-splinter 的分支,增加了新功能和修复,以支持更新版本的 pytestpytest-xdistsplinter >= 0.17.0selenium >= 4.0

PyPI PyPI - Python Version GitHub Build status https://codecov.io/gh/jsfehler/pytest-splinter4/branch/master/graph/badge.svg?token=C1vfu8YgWn Documentation Status

安装

python -m pip install pytest-splinter4

功能

合理的默认值

驱动程序可执行路径参数

当使用chrome、firefox或edge时,executable_path 驱动程序参数的默认值设置为在当前工作目录中搜索 chrome/gecko/edgedriver。

浏览器固定值

以下固定值提供 splinter.Browser() 的实例

  • browser

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

  • session_browser

    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 参数的支持。它们仅在基于 Selenium 的驱动程序使用时使用。

  • splinter_selenium_implicit_wait

    传递给 Selenium WebDriver 的隐式等待超时。测试用例从命令行选项 splinter-implicit-wait 获取值(见下文)

  • splinter_selenium_speed

    Selenium 的速度,如果不为 0,则在每次 Selenium 命令之间将暂停。这对于调试/演示很有用。测试用例从命令行选项 splinter-speed 获取值(见下文)

  • splinter_selenium_socket_timeout

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

Splinter 测试用例

以下测试用例提供对 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 名称是‘remote’时使用。

  • splinter_remote_name

    运行远程 WebDriver 时要使用的浏览器名称。

    这仅在选定的 WebDriver 名称是‘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

    驱动程序日志目录。默认为‘logs’。

  • splinter_screenshot_dir

    浏览器截图目录。默认为‘logs/{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。如果您知道需要清理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

仅Firefox

  • splinter_firefox_profile_preferences

    Firefox配置文件首选项,这是一个字典,用于传递给selenium webdriver的profile_preferences

  • splinter_firefox_profile_directory

    用于作为selenium创建的Firefox配置文件模板的Firefox配置文件目录。默认情况下,它位于pytest_splinter/profiles/firefox中的空目录

命令行选项

  • –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-remote-name

    运行远程 WebDriver 时要使用的浏览器名称。

  • –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-headless

    覆盖splinter_headless固定装置。选项为“true”或“false”,默认:“true”。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

浏览器固定装置

如上所述,browser固定装置是Splinter的Browser对象实例,但有一些覆盖。

  • visit

    通过具有固定装置,增加了在每个浏览器访问时等待条件的可能性。

  • wait_for_condition

    复制selenium的wait_for_condition方法,不同之处在于条件是Python,因此您可以在那里做任何您想做的事情,而不仅仅是通过browser.evaluate_script执行javascript。

在测试失败时自动截图

当测试失败时,了解失败的原因非常重要。当在无法调试(使用–pdb)的持续集成服务器上运行测试时,这变得很困难。为了简化问题,浏览器固定装置有特殊的行为,当测试失败时,它会在一个与jenkins插件兼容的命名约定文件夹中捕获截图。浏览器页面的html内容也存储起来,这可以用于调试html源。

创建截图与pytest-xdist插件完全兼容,并将自动将截图从工作节点通过通信通道自动传输。

如果测试(使用browser固定装置)失败,您应该可以在以下路径获得截图文件

<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 将发出警告,可以使用 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'

联系方式

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

许可证

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

查看 许可证文件

项目详情


下载文件

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

源分布

pytest-splinter4-0.4.0.tar.gz (22.0 kB 查看哈希值)

上传时间

构建分布

pytest_splinter4-0.4.0-py3-none-any.whl (17.0 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

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