airgun.browser

Tools to help getting selenium and widgetastic browser instance to run UI tests.

Module Contents

airgun.browser.docker
airgun.browser.sauceclient
airgun.browser.LOGGER
exception airgun.browser.DockerBrowserError

Bases: Exception

Indicates any issue with DockerBrowser.

airgun.browser._sauce_ondemand_url(saucelabs_user, saucelabs_key)

Get sauce ondemand URL for a given user and key.

Parameters:
  • saucelabs_user (str) – saucelabs username
  • saucelabs_key (str) – saucelabs access key
Returns:

string representing saucelabs ondemand URL

class airgun.browser.SeleniumBrowserFactory(provider=None, browser=None, test_name=None, session_cookie=None)

Bases: object

Factory which creates selenium browser of desired provider (selenium, docker or saucelabs). Creates all required capabilities, passes certificate checks and applies other workarounds. It is also capable of finalizing the browser when it’s not needed anymore (closes the browser, stops docker container, sends test results to saucelabs etc).

Usage:

# init factory
factory = SeleniumBrowserFactory(test_name=test_name)

# get factory browser
selenium_browser = factory.get_browser()

# navigate to desired url
# [...]

# perform post-init steps (e.g. skipping certificate error screen)
factory.post_init()

# perform your test steps
# [...]

# perform factory clean-up
factory.finalize(passed)
get_browser(self)

Returns selenium webdriver instance of selected provider and browser.

Returns:selenium webdriver instance
Raises:ValueError: If wrong provider or browser specified.
post_init(self)

Perform all required post-init tweaks and workarounds. Should be called _after_ proceeding to desired url.

Returns:None
finalize(self, passed=True)

Finalize browser - close browser window, report results to saucelabs or close docker container if needed.

Parameters:passed (bool) – Boolean value indicating whether test passed or not. Is only used for saucelabs provider.
Returns:None

Add the session cookie (if provided) to the webdriver

_get_selenium_browser(self)

Returns selenium webdriver instance of selected browser.

Note: should not be called directly, use get_browser() instead.

Raises:ValueError: If wrong browser specified.
_get_saucelabs_browser(self)

Returns saucelabs webdriver instance of selected browser.

Note: should not be called directly, use get_browser() instead.

Raises:ValueError: If wrong browser specified.
_get_docker_browser(self)

Returns webdriver running in docker container. Currently only firefox and chrome are supported.

Note: should not be called directly, use get_browser() instead.

_get_remote_browser(self)

Returns remote webdriver instance of selected browser.

Note: should not be called directly, use get_browser() instead.

_get_webdriver_capabilities(self)

Returns webdriver capabilities of selected browser.

Note: should not be called directly, use _get_remote_browser() or _get_saucelabs_browser() instead.

Raises:ValueError: If wrong browser specified.
_finalize_saucelabs_browser(self, passed)

SauceLabs has no way to determine whether test passed or failed automatically, so we explicitly ‘tell’ it.

Note: should not be called directly, use finalize() instead.

Parameters:passed (bool) – Bool value indicating whether test passed or not.
_finalize_docker_browser(self)

Stops docker container.

Note: should not be called directly, use finalize() instead.

class airgun.browser.DockerBrowser(name=None, image=None, capabilities=None)

Bases: object

Provide a browser instance running inside a docker container.

Usage:

# either as context manager
with DockerBrowser() as browser:
    # [...]

# or with manual :meth:`start` and :meth:`stop` calls.
docker_browser = DockerBrowser()
docker_browser.start()
# [...]
docker_browser.stop()
start(self)

Start all machinery needed to run a browser inside a docker container.

stop(self)

Quit the browser, remove docker container and close docker client.

_init_webdriver(self)

Init the selenium Remote webdriver.

_quit_webdriver(self)

Quit the selenium remote webdriver.

_init_client(self)

Init docker client.

Make sure that docker service to be published under the unix://var/run/docker.sock unix socket.

Use auto for version in order to allow docker client to automatically figure out the server version.

_close_client(self)

Close docker Client.

_create_container(self)

Create a docker container running a standalone-firefox or standalone-chrome selenium.

Make sure to have the image selenium/standalone-firefox or selenium/standalone-chrome already pulled, preferably in the same version as the selenium-module.

_remove_container(self)

Turn off and clean up container from system.

__enter__(self)

Setup docker browser when used as context manager.

__exit__(self, *exc)

Perform all cleanups when used as context manager.

_get_image_name(self, version)

Returns docker-image’s name and version (aka tag)

class airgun.browser.AirgunBrowserPlugin

Bases: widgetastic.browser.DefaultPlugin

Plug-in for AirgunBrowser which adds satellite-specific JavaScript to make sure page is loaded completely. Checks for absence of jQuery, AJAX, Angular requests, absence of spinner indicating loading progress and ensures document.readyState is “complete”.

ENSURE_PAGE_SAFE = function jqueryInactive() { return (typeof jQuery === "undefined") ? true : jQuery.active < 1 } function ajaxInactive() { return (typeof Ajax === "undefined") ? true : Ajax.activeRequestCount < 1 } function angularNoRequests() { if (typeof angular === "undefined") { return true } else if (typeof angular.element( document).injector() === "undefined") { injector = angular.injector(["ng"]); return injector.get("$http").pendingRequests.length < 1 } else { return angular.element(document).injector().get( "$http").pendingRequests.length < 1 } } function spinnerInvisible() { spinner = document.getElementById("vertical-spinner") return (spinner === null) ? true : spinner.style["display"] == "none" } function reactLoadingInvisible() { react = document.querySelector("#reactRoot .loading-state") return react === null } function anySpinnerInvisible() { spinners = Array.prototype.slice.call( document.querySelectorAll('.spinner') ).filter(function (item,index) { return item.offsetWidth > 0 || item.offsetHeight > 0 || item.getClientRects().length > 0; } ); return spinners.length === 0 } return { jquery: jqueryInactive(), ajax: ajaxInactive(), angular: angularNoRequests(), spinner: spinnerInvisible(), any_spinner: anySpinnerInvisible(), react: reactLoadingInvisible(), document: document.readyState == "complete", }
ensure_page_safe(self, timeout='30s')

Ensures page is fully loaded. Default timeout was 10s, this changes it to 30s.

before_click(self, element, locator=None)

Invoked before clicking on an element. Ensure page is fully loaded before clicking.

after_click(self, element, locator=None)

Invoked after clicking on an element. Ensure page is fully loaded before proceeding further.

class airgun.browser.AirgunBrowser(selenium, session, extra_objects=None)

Bases: widgetastic.browser.Browser

A wrapper around widgetastic.browser.Browser which injects airgun.session.Session and AirgunBrowserPlugin.

get_client_datetime(self)

Make Javascript call inside of browser session to get exact current date and time. In that way, we will be isolated from any issue that can happen due different environments where test automation code is executing and where browser session is opened. That should help us to have successful run for docker containers or separated virtual machines When calling .getMonth() you need to add +1 to display the correct month. Javascript count always starts at 0, so calling .getMonth() in May will return 4 and not 5.

Returns:Datetime object that contains data for current date and time on a client
get_downloads_list(self)

Open browser’s downloads screen and return a list of downloaded files.

Returns:list of strings representing file URIs
get_file_content(self, uri)

Get file content by its URI from browser’s downloads page.

Returns:bytearray representing file content
Raises:Exception – when error code instead of file content received
save_downloaded_file(self, file_uri=None, save_path=None)

Save local or remote browser’s automatically downloaded file to specified local path. Useful when you don’t know exact file name or path where file was downloaded or you’re using remote driver with no access to worker’s filesystem (e.g. saucelabs).

Usage example:

view.widget_which_triggers_file_download.click()
path = self.browser.save_downloaded_file()
with open(file_path, newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        # process file contents
Parameters:
  • optional file_uri (str) – URI of file. If not specified - browser’s latest downloaded file will be selected
  • optional save_path (str) – local path where the file should be saved. If not specified - temp_dir from airgun settings will be used in case of remote session or just path to saved file in case local one.