GitHub - pytest-dev/pytest-splinter: pytest splinter and selenium integration for anyone interested in browser interaction in tests (original) (raw)

Splinter plugin for the pytest runner

Join the chat at https://gitter.im/pytest-dev/pytest-splinter Documentation Status

Install pytest-splinter

pip install pytest-splinter

Features

The plugin provides a set of fixtures to use splinterfor browser testing with pytest

Fixtures

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

import pytest

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

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

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 thejenkins 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 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:

/my.dotted.name.test.package/test_name-browser.png /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:

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.

License

This software is licensed under the MIT license

See License file

© 2014 Anatoly Bubenkov, Paylogic International and others.