Parallel Testing with Python and Selenium on Sauce Online Workshop Recap

Posted Nov 13th, 2013

Last week, we hosted an intro to parallel testing on Sauce with Python and Selenium. You can find the slides here. We used the following scripts in the workshop: Sauce helper:
This library is not supported by Sauce Labs. It merely servers as an
example on send data to the Sauce Labs API.

import requests
import json
import time

# Can be used to time different selenium commands
def timeit(f):

def timed(*args, **kw):

ts = time.time()
result = f(*args, **kw)
te = time.time()

print 'func:%r args:[%r, %r] took: %2.4f sec' % \
(f.__name__, args, kw, te-ts)
return result

return timed
# Set the status of a test to pass or fail status can be passed or failed.
def reportStatus(status, session_id, user_id, access_key):
hdrs = {"Content-Type": "text/json"}
payload = {"passed" : status}
r = requests.put("http://" + user_id + ":" + access_key + "" + user_id + "/jobs/" + session_id, headers=hdrs, data=json.dumps(payload))

def reportCustom(custom_data, session_id, user_id, access_key):
hdrs = {"Content-Type": "text/json"}
payload = {"custom-data" : custom_data}
r = requests.put("http://" + user_id + ":" + access_key + "" + user_id + "/jobs/" + session_id, headers=hdrs, data=json.dumps(payload))

# from selenium.webdriver.firefox.webdriver import WebDriver
# from selenium.webdriver.common.action_chains import ActionChains
from import expected_conditions as EC
from import WebDriverWait
from import By
from selenium import webdriver
import time, unittest, sys, saucehelper, copy
import wd.parallel
and the test script:
class parallel(unittest.TestCase):

def setUp(self):
# self.wd = WebDriver()
# self.wd.implicitly_wait(60)

desired_capabilities = []

browser = copy.copy(webdriver.DesiredCapabilities.CHROME)
browser['platform'] = 'Linux'
browser['name'] = 'Python on Sauce 1/2'
browser['tags'] = "Parallel"
browser['build'] = "9999"
desired_capabilities += [browser]

browser = copy.copy(webdriver.DesiredCapabilities.CHROME)
browser['platform'] = 'Windows XP'
browser['name'] = 'Python on Sauce 2/2'
browser['tags'] = "Parallel"
browser['build'] = "9999"
desired_capabilities += [browser]

self.drivers = wd.parallel.Remote(
command_executor="http://" + parallel.SAUCE_USERNAME + ":" + parallel.SAUCE_ACCESS_KEY + ""


def test_parallel(self):


wait = WebDriverWait(self.driver, 30)
condition = EC.text_to_be_present_in_element((By.TAG_NAME, "html"), "Apple Inc. (AAPL)")

print self.driver.session_id

def tearDown(self):

status = sys.exc_info() == (None, None, None)
saucehelper.reportStatus(status, self.driver.session_id, parallel.SAUCE_USERNAME, parallel.SAUCE_ACCESS_KEY)

if __name__ == '__main__':
Additionally, here are some of the commonly asked questions and answers. Q: How many tests can be run in parallel? A: This is determined by the number of VM’s allocated to an account. Q: Where can I download the python-parallel-wd module? A: Q: What recommendations would you make on choosing which tests should be run in parallel? A: We recommend running all UI tests in parallel across multiple platforms. If the application is publicly facing application owners do not have control over what browsers and devices will be accessing the application. Q: With, timeout, poll_frequency=0.5, ignored_exceptions=None) how long will it wait? A: until(method, message='') Calls the method provided with the driver as an argument until the return value is not False Python Example wait = WebDriverWait(self.driver, 30) condition = EC.text_to_be_present_in_element((By.TAG_NAME, "html"), "Finance") wait.until(condition) Stay tuned for more workshops, and let us know if there are topics you want us to cover!

Written by

Bill McGee


SeleniumProgramming languages