Parallel Testing with Python and Selenium on Sauce Online Workshop Recap

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 + "@saucelabs.com/rest/v1/" + 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 + "@saucelabs.com/rest/v1/" + 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 selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
import time, unittest, sys, saucehelper, copy
import wd.parallel
and the test script:
class parallel(unittest.TestCase):
SAUCE_USERNAME = '{YOUR_USER}'
SAUCE_ACCESS_KEY = '{YOUR_KEY}'

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(
desired_capabilities=desired_capabilities,
command_executor="http://" + parallel.SAUCE_USERNAME + ":" + parallel.SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"

)

@wd.parallel.multiply
def test_parallel(self):

self.driver.get("http://www.yahoo.com/")
self.driver.find_element_by_link_text("Finance").click()
self.driver.find_element_by_id("txtQuotes").click()
self.driver.find_element_by_id("txtQuotes").clear()
self.driver.find_element_by_id("txtQuotes").send_keys("aapl")
self.driver.find_element_by_id("btnQuotes").click()

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

print self.driver.session_id

@wd.parallel.multiply
def tearDown(self):

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

if __name__ == '__main__':
unittest.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: https://github.com/OniOni/python-parallel-wd.git 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 selenium.webdriver.support.wait.WebDriverWait(driver, 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

Topics

SeleniumProgramming languages

Categories