# User Guide¶

## Quick Start¶

The pytest-selenium plugin provides a method scoped selenium fixture for your tests. This means that any test with selenium as an argument will cause a browser instance to be invoked. The browser may run locally or remotely depending on your configuration, and may even run headless.

Here’s a simple example test that opens a website using Selenium:

def test_example(selenium):
selenium.get('http://www.example.com')


To run the above test you will need to specify the browser instance to be invoked. For example, to run it using Firefox installed in a default location:



## Specifying a Browser¶

To indicate the browser you want to run your tests against you will need to specify a driver and optional capabilties.

### Firefox¶

To run your automated tests with Firefox version 47 or earlier, simply specify Firefox as your driver:

$py.test --driver Firefox  For Firefox version 48 onwards, you will need to download GeckoDriver and selenium 3.0 or later. If the driver executable is not available on your path, you can use the --driver-path option to indicate where it can be found: $ py.test --driver Firefox --driver-path /path/to/geckodriver


#### Configuration¶

The current implementation of the Firefox driver does not allow you to specify the binary path, preferences, profile path, or extensions via capabilities. There are therefore additional command line options for each of these. Check --help for further details.

### Chrome¶

To use Chrome, you will need to download ChromeDriver and specify Chrome for the --driver command line option. If the driver executable is not available on your path, you can use the --driver-path option to indicate where it can be found:



### Safari¶

To use Safari, you will need to have at least Safari 10 running on OS X El Capitan or later, and selenium 3.0 or later. Once you have these prerequisites, simply specify Safari for the --driver command line option.

$py.test --driver Safari  ### Selenium Server/Grid¶ To run your automated tests against a Selenium server or a Selenium Grid you must have a server running and know the host and port of the server. By default Selenium will listen on host 127.0.0.1 and port 4444. This is also the default when running tests against a remote driver. To run your automated tests, simply specify Remote as your driver. Browser selection is determined using capabilities. Check the desired capabilities documentation for details of accepted values. There are also a number of browser specific capabilities that can be set. Be sure to also check the documentation for your chosen driver, as the accepted capabilities may differ: $ py.test --driver Remote --capability browserName firefox


Note that if your server is not running locally or is running on an alternate port you will need to specify the --host and --port command line options:

$py.test --driver Remote --host selenium.hostname --port 5555 --capability browserName firefox  ### Sauce Labs¶ To run your automated tests using Sauce Labs, you must provide a valid username and API key. This can be done either by using a .saucelabs configuration file in the working directory or your home directory, or by setting the SAUCELABS_USERNAME and SAUCELABS_API_KEY environment variables. #### Configuration¶ Below is an example .saucelabs configuration file: [credentials] username = username key = secret  #### Running tests¶ To run your automated tests, simply specify SauceLabs as your driver: $ py.test --driver SauceLabs --capability browserName Firefox


See the supported platforms to help you with your configuration. Additional capabilities can be set using the --capability command line arguments. See the test configuration documentation for full details of what can be configured.

### BrowserStack¶

To run your automated tests using BrowserStack, you must provide a valid username and access key. This can be done either by using a .browserstack configuration file in the working directory or your home directory, or by setting the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.

#### Configuration¶

Below is an example .browserstack configuration file:

[credentials]
key = secret


#### Running tests¶

To run your automated tests, simply specify BrowserStack as your driver:

$py.test --driver BrowserStack --capability browserName Firefox  See the capabilities documentation for additional configuration that can be set using --capability command line arguments. ### TestingBot¶ To run your automated tests using TestingBot, you must provide a valid key and secret. This can be done either by using a .testingbot configuration file in the working directory or your home directory, or by setting the TESTINGBOT_KEY and TESTINGBOT_SECRET environment variables. #### Configuration¶ Below is an example .testingbot configuration file: [credentials] key = key secret = secret  #### Running tests¶ To run your automated tests, simply specify TestingBot as your driver: $ py.test --driver TestingBot --capability browserName firefox --capability version 39 --capability platform WIN8


See the list of available browsers to help you with your configuration. Additional capabilities can be set using the --capability command line arguments. See the test options for full details of what can be configured.

### CrossBrowserTesting¶

To run your automated tests using CrossBrowserTesting, you must provide a valid username and auth key. This can be done either by using a .crossbrowsertesting configuration file in the working directory or your home directory, or by setting the CROSSBROWSERTESTING_USERNAME and CROSSBROWSERTESTING_AUTH_KEY environment variables.

#### Configuration¶

Below is an example .crossbrowsertesting configuration file:

[credentials]
key = secret


#### Running tests¶

To run your automated tests, simply specify CrossBrowserTesting as your driver:



### Capabilities Files¶

To specify capabilities, you can provide a JSON file on the command line using the pytest-variables plugin. For example if you had a capabilties.json containing your capabilities, you would need to include --variables capabilities.json on your command line.

The following is an example of a variables file including capabilities:

{ "capabilities": {
"browserName": "Firefox",
"platform": "MAC" }
}


### Capabilities Fixtures¶

The session_capabilities fixture includes capabilities that apply to the entire test session (including any command line or file based capabilities). Any changes to these capabilities will apply to every test. These capabilities are also reported at the top of the HTML report.

import pytest
@pytest.fixture(scope='session')
def session_capabilities(session_capabilities):
session_capabilities['tags'] = ['tag1', 'tag2', 'tag3']
return session_capabilities


The capabilities fixture contains all of the session capabilities, plus any capabilities specified by the capabilities marker. Any changes to these capabilities will apply only to the tests covered by scope of the fixture override.

import pytest
@pytest.fixture
def capabilities(capabilities):
capabilities['public'] = 'private'
return capabilities


### Capabilities Marker¶

You can add or change capabilities using the capabilities marker:

import pytest
@pytest.mark.capabilities(foo='bar')
def test_capabilities(selenium):
selenium.get('http://www.example.com')


## Common Selenium Setup¶

If you have common setup that you want to apply to your tests, such as setting the implicit timeout or window size, you can override the selenium fixture:

import pytest
@pytest.fixture
def selenium(selenium):
selenium.implicitly_wait(10)
selenium.maximize_window()
return selenium


## HTML Report¶

A custom HTML report is generated when the --html command line option is given. By default this will include additional debug information for failures.

### Debug Types¶

The following debug information is gathered by default when a test fails:

• URL - The current URL open in the browser.
• HTML - The HTML source of the page open in the browser.
• LOG - All logs available. Note that this will vary depending on the browser and server in use. See logging for more details.
• SCREENSHOT - A screenshot of the page open in the browser.

### Capturing Debug¶

To change when debug is captured you can either set selenium_capture_debug in a configuration file, or set the SELENIUM_CAPTURE_DEBUG environment variable. Valid options are: never, failure (the default), and always. Note that always capturing debug will dramatically increase the size of the HTML report.

### Excluding Debug¶

You may need to exclude certain types of debug from the report. For example, log files can contain sensitive information that you may not want to publish. To exclude a type of debug from the report, you can either set selenium_exclude_debug in a configuration file, or set the SELENIUM_EXCLUDE_DEBUG environment variable to a list of the Debug Types to exclude.

For example, to exclude HTML, logs, and screenshots from the report, you could set SELENIUM_EXCLUDE_DEBUG to html:logs:screenshot.