pytest-dev / pytest-splinter

pytest splinter and selenium integration for anyone interested in browser interaction in tests
MIT License
253 stars 51 forks source link

Splinter plugin for the pytest runner

.. image:: https://badges.gitter.im/pytest-dev/pytest-splinter.svg :alt: Join the chat at https://gitter.im/pytest-dev/pytest-splinter :target: https://gitter.im/pytest-dev/pytest-splinter?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge

.. image:: https://img.shields.io/pypi/v/pytest-splinter.svg :target: https://pypi.python.org/pypi/pytest-splinter .. image:: https://img.shields.io/pypi/pyversions/pytest-splinter.svg :target: https://pypi.python.org/pypi/pytest-splinter .. image:: https://img.shields.io/coveralls/pytest-dev/pytest-splinter/master.svg :target: https://coveralls.io/r/pytest-dev/pytest-splinter .. image:: https://github.com/pytest-dev/pytest-splinter/actions/workflows/tests.yml/badge.svg :target: https://github.com/pytest-dev/pytest-splinter/actions .. image:: https://readthedocs.org/projects/pytest-splinter/badge/?version=latest :target: https://readthedocs.org/projects/pytest-splinter/?badge=latest :alt: Documentation Status

Install pytest-splinter

::

pip install pytest-splinter

Features

The plugin provides a set of fixtures to use splinter <https://splinter.readthedocs.io> for browser testing with pytest <http://pytest.org>

Fixtures

.. code-block:: python

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

.. code-block:: python

import pytest

@pytest.fixture(scope='session')
def splinter_webdriver():
    """Override splinter webdriver name."""
    return 'chrome'

.. code-block:: python

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

The snippet below configures Chrome to ignore certificate errors and sets a specific window size

.. code-block:: python

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}

Command-line options

Browser fixture

As mentioned above, browser is a fixture made by creating splinter's Browser object, but with some overrides.

Automatic screenshots on test failure

When your functional test fails, it's important to know the reason. This becomes hard when tests are being run on the continuous integration server, where you cannot debug (using --pdb). To simplify things, a special behaviour of the browser fixture is available, which takes a screenshot on test failure and puts it in a folder with the a naming convention compatible to the jenkins plugin <https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Attachments+Plugin>_. The html content of the browser page is also stored, this can be useful for debugging the html source.

Creating screenshots is fully compatible with pytest-xdist plugin <https://pypi.python.org/pypi/pytest-xdist>_ and will transfer the screenshots from the worker nodes through the communication channel automatically.

If a test (using the browser fixture) fails, you should get a screenshot files in the following path:

::

<splinter-screenshot-dir>/my.dotted.name.test.package/test_name-browser.png
<splinter-screenshot-dir>/my.dotted.name.test.package/test_name-browser.html

The splinter-screenshot-dir for storing the screenshot is generated by a fixture and can be provided through a command line argument, as described above at the configuration options section.

Taking screenshots on test failure is enabled by default. It can be controlled through the splinter_make_screenshot_on_failure fixture, where return False skips it. You can also disable it via a command line argument:

::

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

In case taking a screenshot fails, a pytest warning will be issued, which can be viewed using the -rw argument for pytest.

Python3 support

Python3 is supported, check if you have recent version of splinter as it was added recently.

Example

test_your_test.py:

.. code-block:: python

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'

Contact

If you have questions, bug reports, suggestions, etc. please create an issue on the GitHub project page <http://github.com/paylogic/pytest-splinter>_.

License

This software is licensed under the MIT license <http://en.wikipedia.org/wiki/MIT_License>_

See License file <https://github.com/paylogic/pytest-splinter/blob/master/LICENSE.txt>_

© 2014 Anatoly Bubenkov, Paylogic International and others.