Package: pytest-splinter
Splinter plugin for the pytest runnerInstall pytest-splinterpip install pytest-splinterFeaturesThe plugin provides a set of fixtures to usesplinterfor browser testing withpytestFixturesbrowserGet the splinter’s Browser. Fixture is underneath session scoped, so browser process is started
once per test session, but the state of the browser will be clean (current page isblank, cookies clean).session_browserThe same asbrowserexcept the lifetime. This fixture is session-scoped so will only be finalized at the
end of the whole test session. Useful if you want to speedup your test suite paying with reduced test isolation.browser_instance_getterFunction to create an instance of the browser. This fixture is required only if you need to have
multiple instances of the Browser in a single test at the same time. Example of usage:@pytest.fixturedefadmin_browser(request,browser_instance_getter):"""Admin browser fixture."""# browser_instance_getter function receives parent fixture -- our admin_browserreturnbrowser_instance_getter(request,admin_browser)deftest_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_waitImplicit wait timeout to be passed to Selenium webdriver.
Fixture gets the value from the command-line option splinter-implicit-wait (see below)splinter_wait_timeExplicit wait timeout (for waiting for explicit condition viawait_for_condition).
Fixture gets the value from the command-line option splinter-wait-time (see below)splinter_selenium_speedSpeed for Selenium, if not 0 then it will sleep between each selenium command.
Useful for debugging/demonstration.
Fixture gets the value from the command-line option splinter-speed (see below)splinter_selenium_socket_timeoutSocket timeout for communication between the webdriver and the browser.
Fixture gets the value from the command-line option splinter-socket-timeout (see below)splinter_webdriverSplinter’s webdriver name to use. Fixture gets the value from the command-line option
splinter-webdriver (see below). To make pytest-splinter always use certain webdriver, override a fixture
in yourconftest.pyfile:importpytest@pytest.fixture(scope='session')defsplinter_webdriver():"""Override splinter webdriver name."""return'chrome'splinter_remote_urlSplinter’s webdriver remote url to use (optional). Fixture gets the value from the command-line option
splinter-remote-url (see below). Will be used only if selected webdriver name is ‘remote’.splinter_session_scoped_browserpytest-splinter should use single browser instance per test session.
Fixture gets the value from the command-line option splinter-session-scoped-browser (see below)splinter_file_download_dirDirectory, to which browser will automatically download the files it
will experience during browsing. For example when you click on some download link.
By default it’s a temporary directory. Automatic downloading of files is only supported for firefox driver
at the moment.splinter_download_file_typesComma-separated list of content types to automatically download.
By default it’s the all known system mime types (via mimetypes standard library).splinter_browser_load_conditionBrowser load condition, python function which should return True.
If function returns False, it will be run several times, until timeout below reached.splinter_browser_load_timeoutBrowser load condition timeout in seconds, after this timeout the exception
WaitUntilTimeout will be raised.splinter_wait_timeBrowser explicit wait timeout in seconds, after this timeout the exception
WaitUntilTimeout will be raised.splinter_firefox_profile_preferencesFirefox profile preferences, a dictionary which is passed to selenium
webdriver’s profile_preferencessplinter_firefox_profile_directoryFirefox profile directory to use as template for firefox profile created by selenium.
By default, it’s an empty directly inside pytest_splinter/profiles/firefoxsplinter_driver_kwargsWebdriver keyword arguments, a dictionary which is passed to selenium
webdriver’s constructor (after applying firefox preferences)importpytestfrompathlibimportPath@pytest.fixturedefsplinter_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 sizeimportpytestfromseleniumimportwebdriver@pytest.fixture(scope='session')defsplinter_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_sizeSize of the browser window on browser initialization. Tuple in form (<width>, <height>). Default is (1366, 768)splinter_screenshot_dirpytest-splinter browser screenshot directory.
This fixture gets the value from the command-line optionsplinter-screenshot-dir(see below).splinter_make_screenshot_on_failureShould pytest-splinter take browser screenshots on test failure?
This fixture gets the value from the command-line optionsplinter-make-screenshot-on-failure(see below).splinter_screenshot_encodingEncoding of thehtmlscreenshoton test failure. UTF-8 by default.splinter_screenshot_getter_htmlFunction to get browser html screenshot. By default, it savesbrowser.htmlwith given path andsplinter_screenshot_encodingencoding.splinter_screenshot_getter_pngFunction to get browser image (png) screenshot. By default, it callsbrowser.save_sceenshotwith given path.splinter_driver_executableFilesystem path of the webdriver executable.
This fixture gets the value from the command-line optionsplinter-webdriver-executable(see below).splinter_browser_classClass to use for browser instance.
Defaults topytest_splinter.plugin.Browser.splinter_clean_cookies_urlsList of additional urls to clean cookies on. By default, during the preparation of the browser for the test,
pytest-splinter only cleans cookies for the last visited url from previous test, as it’s not possible to clean
all cookies from all domains at once via webdriver protocol, by design. This limitation can be worked around if
you know the list of urls, the domains for which you need to clean cookies (for examplehttps://facebook.com).
If so, you can override this fixture and put those urls there, and pytest-splinter will visit each of them and will
clean the cookies for each domain.splinter_headlessRun Chrome in headless mode. Defaults to false.http://splinter.readthedocs.io/en/latest/drivers/chrome.html#using-headless-option-for-chromeCommand-line options–splinter-implicit-waitSelenium webdriver implicit wait. Seconds (default: 5).–splinter-speedselenium webdriver speed (from command to command). Seconds (default: 0).–splinter-socket-timeoutSelenium webdriver socket timeout for for communication between the webdriver and the browser.
Seconds (default: 120).–splinter-webdriverWebdriver name to use. (default: firefox). Options:firefoxremotechromeFor more details refer to the documentation for splinter and selenium.–splinter-remote-urlWebdriver remote url to use. (default: None). Will be used only if selected webdriver name is ‘remote’.For more details refer to the documentation for splinter and selenium.–splinter-session-scoped-browserpytest-splinter should use a single browser instance per test session.
Choices are ‘true’ or ‘false’ (default: ‘true’).–splinter-make-screenshot-on-failurepytest-splinter should take browser screenshots on test failure.
Choices are ‘true’ or ‘false’ (default: ‘true’).–splinter-screenshot-dirpytest-splinter browser screenshot directory. Defaults to the current
directory.–splinter-webdriver-executableFilesystem path of the webdriver executable. Used by chrome driver.
Defaults to the None in which case the shell PATH variable setting determines the location of the executable.–splinter-headlessOverridesplinter_headlessfixture. Choices are ‘true’ or ‘false’, default: ‘true’.http://splinter.readthedocs.io/en/latest/drivers/chrome.html#using-headless-option-for-chromehttps://splinter.readthedocs.io/en/latest/drivers/firefox.html#using-headless-option-for-firefoxBrowser fixtureAs mentioned above, browser is a fixture made by creating splinter’s Browser object, but with some overrides.visitAdded possibility to wait for condition on each browser visit by having a fixture.wait_for_conditionMethod copying selenium’s wait_for_condition, with difference that condition is in python,
so there you can do whatever you want, and not only execute javascript via browser.evaluate_script.Automatic screenshots on test failureWhen 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 withpytest-xdist pluginand 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.htmlThesplinter-screenshot-dirfor 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 thesplinter_make_screenshot_on_failurefixture, where returnFalseskips it. You can also disable it via a command line argument:pytest tests/functional --splinter-make-screenshot-on-failure=falseIn case taking a screenshot fails, a pytest warning will be issued, which
can be viewed using the-rwargument forpytest.Python3 supportPython3 is supported, check if you have recent version of splinter as it was added recently.Exampletest_your_test.py:deftest_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' buttonbutton=browser.find_by_name('btnK')# Interact with elementsbutton.click()assertbrowser.is_text_present('splinter.cobrateam.info'),"splinter.cobrateam.info wasn't found... We need to"' improve our SEO techniques'ContactIf you have questions, bug reports, suggestions, etc. please create an issue on
theGitHub project page.LicenseThis software is licensed under theMIT licenseSeeLicense file© 2014 Anatoly Bubenkov, Paylogic International and others.AuthorsAnatoly Bubenkovoriginal idea and implementation, new features and improvementsThese people have contributed topytest-splinter, in alphabetical order:Alessio BogonAndreas PelmeAndrey MakhnachAymeric AugustinDaniel HahlerHugo van KemenadeIonel Cristian MărieșJoshua FehlerLaurence RoweMarco BucciniMichał PasternakMichel SabchukMikko OhtamaaOleg PidsadnyiPeter LauriSuresh VTomáš EhrlichTony NarlockChangelog3.3.2Fix mouse_over patch for Firefox, so it works with recent splinter (0.18.1) (mpasternak),3.3.1Fix handling of command-line optionsplinter_headless(mpasternak)3.3.0Support headless firefox (mpasternak)3.2.0Passing–splinter-headlesswithout arguments defaults to ‘true’#123(tony)3.1.0Remove unnecessary webdriver patch for retries, this behaviour is now part of splinter. (jsfehler)Bump minimum splinter version to 0.13.0 (jsfehler)3.0.0Removed python2 support (bubenkoff)2.1.0Add support for Django and Flask Splinter browsers, that don’t have a driver
attribute#146(michelts)2.0.1Address compatibility with pytest >= 42.0.0Bump minimum splinter version to 0.9.0 (jsfehler)Remove phantomjs support. (jsfehler)1.9.1Fix utf-8 decode warnings when taking screenshots with pytest-xdist active#108(jsfehler)1.9.0Use getfixturevalue instead of getfuncargvalue#97(pelme)Added Chrome headless support (miohtama)1.8.6Fix screenshots not being taken when used with xdist (youtux)1.8.5Fixed issue with xdist#94(bubenkoff)1.8.3Profile does not work with geckodriver+remote webdriver#90) (pelme)1.8.2Fixed missingswitch_tomethod (some seleniumexpected_conditionsare broken without
it, see#93)1.8.1Ensure node’ssplinter_failurealways exists (bubenkoff, pelme)Correctly handle skipped tests (bubenkoff, schtibe)1.8.0Limit retry behavior forprepare_browser(bubenkoff)Workaround for cleaning cookies (Edge browser) (bubenkoff)1.7.8Make it possible to override the default value for –splinter-wait-time (magnus-staberg)1.7.7Make it possible to override the default–splinter-webdriver(pelme)Fix screenshots for function scoped fixtues (pelme)1.7.6Support pytest 3 (bubenkoff)Less opionated override of splinter’s visit (bubenkoff)1.7.5escape screenshot paths for path separators (bubenkoff)1.7.4use tmpdir_factory to get session scoped tmpdir (RonnyPfannschmidt, bubenkoff)1.7.3fixed Firefox freezing when opening a missing codec extension (olegpidsadnyi)1.7.2fixed taking a screenshot with pytest>=2.9.0 (olegpidsadnyi)1.7.1pytest warnings fixed (firebirdberlin)remove firefox firstrun script (aaugustin, bubenkoff)1.7.0add possibility to clean cookies on given domains during the browser cleanup, document cookies cleanup (bubenkoff)1.6.6screenshot encoding made flexible (bubenkoff)1.6.2pass timeout to restored connection (bubenkoff)1.6.0added html screenshot (bubenkoff, blueyed)1.5.3remote webdriver fixes (bubenkoff)1.5.2respect splinter_make_screenshot_on_failure (bubenkoff)1.5.1use native selenium socket timeout feature (pelme)1.5.0pytest tmpdir_factory support (bubenkoff)depend on splinter 0.7.3, remove the previous status_code monkey patch (pelme)add option–splinter-wait-timeto specify splinter explicit wait timeout (pelme)1.4.6ensure base tempdir exists (bubenkoff)1.4.0introduce splinter_browser_class fixture (bubenkoff, ecesena)1.3.8correctly handle zope.testbrowser splinter driver (bubenkoff)1.3.7passsplinter_selenium_implicit_waitaswait_timeto splinter Browser (lrowe)1.3.6properly respect webdriver executable command line option (bubenkoff, bh)1.3.5add option –splinter-webdriver-executable for phantomjs and chrome (sureshvv)1.3.4makebrowser_instance_gettersession scoped, addsession_browserfixture (bubenkoff, sureshvv)1.3.3makemouse_overcomparible with more use-cases (bubenkoff)1.3.1properly handle driver switch during the test run (bubenkoff)respect splinter_session_scoped_browser fixture (bubenkoff)1.2.10handle exceptions during screenshot saving (blueyed, bubenkoff)documentation improvements (blueyed)1.2.9status_code is back in a lazy way (bubenkoff)1.2.7Fix automatic download of pdf content type (bubenkoff)1.2.4fix failing the test run if pytest-xdist is not installed, as it’s completely optional dependency (bubenkoff, slafs)1.2.3improve exception handing when preparing the browser instance (bubenkoff)require pytest (bubenkoff)1.2.0automatic screenshot capture on test failure (bubenkoff)improvements to the browser preparation procedure (bubenkoff)boolean config options made more clear (bubenkoff)1.1.1restore browser parameters on each test run instead of once for browser start (bubenkoff)1.1.0added possibility to have multiple browser instances for single test (amakhnach, bubenkoff)1.0.4Fixed browser fixture to support splinter_browser_load_condition and splinter_browser_load_timeout by default. (markon)1.0.3unicode fixes to setup.py (bubenkoff, valberg)1.0.2wait_for_condition now receives pytest_bdd.plugin.Browser object, not selenium webdriver one (bubenkoff)1.0.1Refactoring and cleanup (bubenkoff)1.0.0Initial public release