Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Robot Framework Selenium test suite with Sauce Labs support
import re
import requests
import simplejson as json
from robot.api import logger
from robot.libraries.BuiltIn import BuiltIn
USERNAME_ACCESS_KEY = re.compile('^(http|https):\/\/([^:]+):([^@]+)@')
def report_sauce_status(name, status, tags=[], remote_url=''):
# Parse username and access_key from the remote_url
assert USERNAME_ACCESS_KEY.match(remote_url), 'Incomplete remote_url.'
username, access_key = USERNAME_ACCESS_KEY.findall(remote_url)[0][1:]
# Get selenium session id from the keyword library
selenium = BuiltIn().get_library_instance('Selenium2Library')
job_id = selenium._current_browser().session_id
# Prepare payload and headers
token = (':'.join([username, access_key])).encode('base64').strip()
payload = {'name': name,
'passed': status == 'PASS',
'tags': tags}
headers = {'Authorization': 'Basic {0}'.format(token)}
# Put test status to Sauce Labs
url = 'https://saucelabs.com/rest/v1/{0}/jobs/{1}'.format(username, job_id)
response = requests.put(url, data=json.dumps(payload), headers=headers)
assert response.status_code == 200, response.text
# Log video url from the response
video_url = json.loads(response.text).get('video_url')
if video_url:
logger.info('<a href="{0}">video.flv</a>'.format(video_url), html=True)
*** Settings ***
Library Selenium2Library
Library SauceLabs
Test Setup Open test browser
Test Teardown Close test browser
*** Variables ***
${BROWSER} firefox
${REMOTE_URL}
${DESIRED_CAPABILITIES}
${LOGIN_FAIL_MSG} Incorrect username or password.
*** Test Cases ***
Incorrect username or password
[Tags] Login
Go to https://saucelabs.com/login
Page should contain element id=username
Page should contain element id=password
Input text id=username anonymous
Input text id=password secret
Click button id=submit
Page should contain ${LOGIN_FAIL_MSG}
*** Keywords ***
Open test browser
Open browser about: ${BROWSER}
... remote_url=${REMOTE_URL}
... desired_capabilities=${DESIRED_CAPABILITIES}
Close test browser
Run keyword if '${REMOTE_URL}' != ''
... Report Sauce status
... ${SUITE_NAME} | ${TEST_NAME}
... ${TEST_STATUS} ${TEST_TAGS} ${REMOTE_URL}
Close all browsers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment