Skip to content

Instantly share code, notes, and snippets.

Last active April 3, 2020 13:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save datakurre/9589707 to your computer and use it in GitHub Desktop.
Save datakurre/9589707 to your computer and use it in GitHub Desktop.
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 = '{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:'<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
${LOGIN_FAIL_MSG} Incorrect username or password.
*** Test Cases ***
Incorrect username or password
[Tags] Login
Go to
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
Close all browsers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment