Page History
Sauce Labs is a cloud platform for executing automated and manual mobile and web tests. Sauce Labs supports running automated tests with Selenium WebDriver (for web applications) and Appium (for native and mobile web applications). This topic will provide you with an example of how to get your Python tests up and running on Sauce.
Include Page | ||||
---|---|---|---|---|
|
Include Page | ||||
---|---|---|---|---|
|
Table of Contents |
---|
Prerequisites
Include Page | ||||
---|---|---|---|---|
|
Code Example
Include Page | ||||
---|---|---|---|---|
|
Tip | ||
---|---|---|
| ||
The explicit wait method tells the browser to wait a set amount of time (in seconds) for elements to appear on the page before giving up. Using explicit waits is one of our recommended best practices. |
Analyzing the Code
If you look at the code closely, you'll see that basics for setting up a test to run on sauce are very straightforward, and really only require two elements.
If you wanted to run Selenium locally, you might initiate a driver for the browser that you want to test on like so:
Code Block | ||||
---|---|---|---|---|
| ||||
driver = webdriver.Firefox() |
If you wanted to run on Sauce, you would instead use webdriver.Remote()
, and then pass it two paramaters: command_executor
, which points to the Sauce cloud and uses your Sauce Labs authentication to log in, and desired_capabilties
, which specifies the browsers and operating systems to run the tests against.
Code Block | ||||
---|---|---|---|---|
| ||||
# this is how you set up a test to run on Sauce Labs
desired_cap = {
'platform': "Mac OS X 10.9",
'browserName': "chrome",
'version': "31",
}
driver = webdriver.Remote(
command_executor='http://YOUR_SAUCE_USERNAME:YOUR_SAUCE_ACCESSKEY@ondemand.saucelabs.com:80/wd/hub',
desired_capabilities=desired_cap)
|
You can use the Platform Configurator to specify the desired capabilities for any browser/platform combination you want.
Running the Test
- Copy the example code and save it into a file called
first_test.py
.
Make sure your username and access key are included in the URL passed through to the command_executor. - Open a command line terminal and navigate to the directory where the file is located.
Execute the test:
Code Block python first_test.py
Check your dashboard and you will see that your test has just run on Sauce!
Running Local Tests
...
Running Tests in Parallel
...
See the topics under Running Tests in Parallel with Python for more information and examples of setting up popular Python testing frameworks to run tests in parallel on Sauce
Reporting on Test Results
"Wait," you might be asking, "My test says 'Complete' but what happens if it fails?"
Unfortunately, Sauce has no way to determine whether your test passed or failed automatically, since it is determined entirely by your business logic. You can, however, tell Sauce about the results of our tests automatically using the Sauce python client and adding these lines to your test.
Code Block | ||||
---|---|---|---|---|
| ||||
# this authenticates you
from sauceclient import SauceClient
sauce_client = SauceClient("YOUR_SAUCE_USERNAME", "YOUR_SAUCE_ACCESSKEY")
# this belongs in your test logic
sauce_client.jobs.update_job(driver.session_id, passed=True) |
You should also follow our recommended best practice of adding build numbers, tags, and other identifying information to your tests so you can easily find and manage them in your test results and archives pages.
...
|
You can clone this script from the saucelabs-training
repository on GitHub: https://github.com/saucelabs-training/demo-python
Info | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
There are examples using both
|
Testing with a Proxy
If you're trying to run this script from behind a VPN or a corporate proxy, you must use either IPSec or Sauce Connect Proxy. Once you've downloaded and installed the relevant software, add the following capability to the test script:
Code Block |
---|
'tunnelIdentifier': '<tunnel_id>', |
Running the Test
Navigate to the root project directory and use
pip
to install the latest Selenium library for use in the script:Code Block $ pip install -r requirements.txt
- Set your Sauce Labs Credentials as envrionment variables, indicated by the following lines in the script:
View Git file path on-boarding-modules/pytest-examples/test_module4_pytest.py lastline 9 repository-id 36 firstline 8 branch refs/remotes/origin/master Depending on which framework you're using, your commands may be different to run the tests. Use any of the following command based on the chosen framework:
pytest:
Code Block pytest on-boarding-modules/pytest-examples/test_module4_pytest.py
unittest:
Code Block python -m unittest on-boarding-modules/unittest-examples/test_module4_unittest.py