airgun.browser¶
Tools to help getting selenium and widgetastic browser instance to run UI tests.
Module Contents¶
Classes¶
Factory which creates selenium browser of desired provider (selenium, |
|
Plug-in for |
|
A wrapper around |
Attributes¶
- airgun.browser.LOGGER¶
- class airgun.browser.SeleniumBrowserFactory(provider=None, browser=None, test_name=None, session_cookie=None, hostname=None)¶
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()¶
Returns selenium webdriver instance of selected
providerandbrowser.- Returns
selenium webdriver instance
- Raises
ValueError: If wrong
providerorbrowserspecified.
- post_init()¶
Perform all required post-init tweaks and workarounds. Should be called _after_ proceeding to desired url.
- Returns
None
- finalize(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
saucelabsprovider.- Returns
None
- _set_session_cookie()¶
Add the session cookie (if provided) to the webdriver
- _get_selenium_browser()¶
Returns selenium webdriver instance of selected
browser.Note: should not be called directly, use
get_browser()instead.- Raises
ValueError: If wrong
browserspecified.
- _get_remote_browser()¶
Returns remote webdriver instance of selected
browser.Note: should not be called directly, use
get_browser()instead.
- class airgun.browser.AirgunBrowserPlugin(*args, **kwargs)¶
Plug-in for
AirgunBrowserwhich 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 ensuresdocument.readyStateis “complete”.- ENSURE_PAGE_SAFE = Multiline-String¶
Show Value
1 function jqueryInactive() { 2 return (typeof jQuery === "undefined") ? true : jQuery.active < 1 3 } 4 function ajaxInactive() { 5 return (typeof Ajax === "undefined") ? true : 6 Ajax.activeRequestCount < 1 7 } 8 function angularNoRequests() { 9 if (typeof angular === "undefined") { 10 return true 11 } else if (typeof angular.element( 12 document).injector() === "undefined") { 13 injector = angular.injector(["ng"]); 14 return injector.get("$http").pendingRequests.length < 1 15 } else { 16 return angular.element(document).injector().get( 17 "$http").pendingRequests.length < 1 18 } 19 } 20 function spinnerInvisible() { 21 spinner = document.getElementById("vertical-spinner") 22 return (spinner === null) ? true : spinner.style["display"] == "none" 23 } 24 function reactLoadingInvisible() { 25 react = document.querySelector("#reactRoot .loading-state") 26 return react === null 27 } 28 function anySpinnerInvisible() { 29 spinners = Array.prototype.slice.call( 30 document.querySelectorAll('.spinner') 31 ).filter(function (item,index) { 32 return item.offsetWidth > 0 || item.offsetHeight > 0 33 || item.getClientRects().length > 0; 34 } 35 ); 36 return spinners.length === 0 37 } 38 return { 39 jquery: jqueryInactive(), 40 ajax: ajaxInactive(), 41 angular: angularNoRequests(), 42 spinner: spinnerInvisible(), 43 any_spinner: anySpinnerInvisible(), 44 react: reactLoadingInvisible(), 45 document: document.readyState == "complete", 46 }
- property ignore_ensure_page_safe_timeout¶
- ensure_page_safe(timeout='30s')¶
Ensures page is fully loaded. Default timeout was 10s, this changes it to 30s. If self.ignore_ensure_page_safe_timeout is True, the function doesn’t raise an exception and continues as if the page was safe instead. This can be used to bypass some bugs, e.g. https://bugzilla.redhat.com/show_bug.cgi?id=2106022
- before_click(element, locator=None)¶
Invoked before clicking on an element. Ensure page is fully loaded before clicking.
- after_click(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)¶
A wrapper around
widgetastic.browser.Browserwhich injectsairgun.session.SessionandAirgunBrowserPlugin.- get_client_datetime()¶
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()¶
Open browser’s downloads screen and return a list of downloaded files.
- Returns
list of strings representing file URIs
- get_file_content(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(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
file_uri (str optional) – URI of file. If not specified - browser’s latest downloaded file will be selected
save_path (str optional) – local path where the file should be saved. If not specified -
temp_dirfrom airgun settings will be used in case of remote session or just path to saved file in case local one.
- check_alert(locator)¶
- get_alert(squash=False)¶
Returns the current alert/PF4 alert object.
- Parameters
squash (bool optional) – Whether or not to squash errors during alert handling. Default False
- Raises
selenium.common.exceptions.NoAlertPresentException –
- handle_alert(cancel=False, wait=30.0, squash=False, prompt=None, check_present=False)¶
Extend the behaviour of widgetstatic.browser.handle_alert to handle PF4 alerts
- ignore_ensure_page_safe_timeout()¶