webdriver_test_tools.testcase.webdriver module

Base test case classes.

class webdriver_test_tools.testcase.webdriver.WebDriverTestCase(methodName='runTest')[source]

Base class for web driver test cases.

This defines the common setUp() and tearDown() tasks as well as WebDriver-related assertion methods and webdriver_test_tools framework-related decorator methods. It does not initialize self.driver so will not work on its own. Tests should be written with this as their parent class. Browser-specific implementations of test cases will be generated when running tests.

Instances of this class will have the following variables:

Variables

Tests that implement this class override the following variables:

Variables
  • WebDriverTestCase.SITE_URL – Go to this URL during setUp(). Tests that implement WebDriverTestCase must set this accordingly.

  • WebDriverTestCase.SKIP_BROWSERS – (Optional) List of browser names to skip test generation for. This can be useful if a test case class requires functionality that is not implemented in a certain driver, or if its tests are meant for specific browsers. Valid browser names are declared in the Browsers class.

  • WebDriverTestCase.SKIP_MOBILE – (Optional) By default, tests will be generated for all enabled browsers, including mobile. If SKIP_MOBILE is set to True, don’t generate tests for mobile browsers. This can be helpful if the layout changes between desktop and mobile viewports would alter the test procedures.

  • WebDriverTestCase.DEFAULT_ASSERTION_TIMEOUT – (Optional) Default number of seconds for WebDriverTestCase assertion methods to wait for expected conditions to occur before test fails. Defaults to the value of DEFAULT_ASSERTION_TIMEOUT set in the test project’s WebDriverConfig class

Browser-specific implementations of this class need to override the following:

Function WebDriverTestCase.driver_init

Function that returns a Selenium WebDriver object for the browser

Variables
  • WebDriverTestCase.DRIVER_NAME – Name of the browser. This is mostly used in the docstrings of generated test classes to indicate what browser the tests are being run in

  • WebDriverTestCase.SHORT_NAME – Short name for the driver used for command line args, skipping, etc. Should be all lowercase with no spaces

  • WebDriverTestCase.CAPABILITIES – The DesiredCapabilities dictionary for the browser. Used for initializing BrowserStack remote driver

The following attributes are used for running tests on BrowserStack:

Variables

The following attributes are used for running tests in a headless browser:

Variables

WebDriverTestCase.ENABLE_HEADLESS – (Default = False) If set to True, browser implementations with headless browser support will configure their drivers to run tests in a headless browser

driver = None
SITE_URL = None
SKIP_BROWSERS = []
SKIP_MOBILE = None
DEFAULT_ASSERTION_TIMEOUT = None
DRIVER_NAME = None
SHORT_NAME = None
ENABLE_BS = False
COMMAND_EXECUTOR = None
CAPABILITIES = None
ENABLE_HEADLESS = False
bs_driver_init()[source]

Initialize driver for BrowserStack

Returns

webdriver.Remote object with the command_executor and desired_capabilities parameters set to self.COMMAND_EXECUTOR and self.CAPABILITIES respectively.

driver_init()[source]

Returns an initialized WebDriver object. Browser test case classes must implement this.

setUp()[source]

Initialize driver and call self.driver.get(self.SITE_URL)

If self.ENABLE_BS is False, self.driver gets the returned results of self.driver_init(). If self.ENABLE_BS is True, self.driver gets the returned results of self.bs_driver_init()

Also checks if self.DEFAULT_ASSERTION_TIMEOUT is set and defaults to self.WebDriverConfig.DEFAULT_ASSERTION_TIMEOUT if it’s unspecified

tearDown()[source]

Calls self.driver.quit()

assertExists(element_locator, msg=None, wait_timeout=None)[source]

Fail if element doesn’t exist

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertNotExists(element_locator, msg=None, wait_timeout=None)[source]

Fail if element exists

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertInView(element_locator, msg=None, wait_timeout=None)[source]

Fail if element isn’t scrolled into view

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertNotInView(element_locator, msg=None, wait_timeout=None)[source]

Fail if element is scrolled into view

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertVisible(element_locator, msg=None, wait_timeout=None)[source]

Fail if element isn’t visible

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertInvisible(element_locator, msg=None, wait_timeout=None)[source]

Fail if element is visible

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertEnabled(element_locator, msg=None, wait_timeout=None)[source]

Fail if element is disabled

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertDisabled(element_locator, msg=None, wait_timeout=None)[source]

Fail if element is enabled

Parameters
  • element_locator – webdriver locator tuple in the format (by.<attr>, <locator string>)

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertUrlChange(expected_url, msg=None, wait_timeout=None)[source]

Fail if the URL doesn’t match the expected URL.

Assertion uses webdriver_test_tools.test.url_change_test() using the specified wait_timeout before determining that expected_url does not match the current URL.

Parameters
  • expected_url – The expected URL

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

assertBaseUrlChange(expected_url, ignore_trailing_slash=True, msg=None, wait_timeout=None)[source]

Fail if the URL (ignoring query strings) doesn’t match the expected URL.

Assertion uses webdriver_test_tools.test.url_change_test() using the specified wait_timeout before determining that expected_url does not match the current URL.

Parameters
  • expected_url – The expected URL

  • ignore_trailing_slash – (Default = True) If True, ignore trailing ‘/’ in the expected url and current base URL when comparing

  • msg – (Optional) if specified, used as the error message on failure

  • wait_timeout – (Default = self.DEFAULT_ASSERTION_TIMEOUT) Number of seconds to wait for expected conditions to occur before test fails

static skipBrowsers(*browsers)[source]

Conditionally skip a test method for certain browsers

Usage Example:

@WebDriverTestCase.skipBrowsers(Browsers.SAFARI, Browsers.IE)
test_method(self):
    ...
static skipMobile()[source]

Conditionally skip a test method for mobile browsers

Usage Example:

@WebDriverTestCase.skipMobile()
test_method(self):
    ...
static mobileOnly()[source]

Conditionally skip a test method for non-mobile browsers

Usage Example:

@WebDriverTestCase.mobileOnly()
test_method(self):
    ...
takeScreenshot(print_filename=False)[source]

Save a screenshot using self.WebDriverConfig.new_screenshot_file

Parameters

print_filename – (Default = False) If True, print the path to the new file to standard out

Returns

Path to the new screenshot file

static screenshotOnFail()[source]

Decorator for test methods that takes a screenshot if an assertion fails. See WebDriverConfig.new_screenshot_file for details on filename and output directory

Usage Example:

@WebDriverTestCase.screenshotOnFail()
test_method(self):
    ...
    self.assertTrue(condition)
    ...

Note

Currently, this method does not take a screenshot for assertions that fail within a subTest. Since subTests are designed to continue test execution if an assertion fails, they don’t raise exceptions outside of their context.

is_mobile()[source]

Check whether the test case is running in a mobile browser

Returns

True if running in a mobile browser (or emulated one), False if running in a desktop

class webdriver_test_tools.testcase.webdriver.WebDriverMobileTestCase(methodName='runTest')[source]

Bases: webdriver_test_tools.testcase.webdriver.WebDriverTestCase

Base class for mobile web driver test cases

If a test subclasses WebDriverMobileTestCase instead of WebDriverTestCase, tests will only be generated for mobile browsers

SKIP_MOBILE = False
is_mobile()[source]

Check whether the test case is running in a mobile browser

Returns

True if running in a mobile browser (or emulated one), False if running in a desktop