Skip to content

Instantly share code, notes, and snippets.

@santiycr
Created January 20, 2012 01:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save santiycr/1644439 to your computer and use it in GitHub Desktop.
Save santiycr/1644439 to your computer and use it in GitHub Desktop.
Sauce REST API via Python
import httplib
import base64
try:
import json
except ImportError:
import simplejson as json
config = {"username": "your-sauce-username",
"access-key": "your-sauce-api-key"}
base64string = base64.encodestring('%s:%s' % (config['username'], config['access-key']))[:-1]
def set_test_status(jobid, passed=True):
body_content = json.dumps({"passed": passed})
connection = httplib.HTTPConnection("saucelabs.com")
connection.request('PUT', '/rest/v1/%s/jobs/%s' % (config['username'], jobid),
body_content,
headers={"Authorization": "Basic %s" % base64string})
result = connection.getresponse()
return result.status == 200
set_test_status("your-job-id", passed=True)
@lyndsysimon
Copy link

I'd like to use this code verbatim in a module I'm planning to release on PyPI - would you mind giving me explicit permission to do so, under a permissive license?

@dlai0001
Copy link

Can you add to this example how I can get a job ID given a selenium webdriver instance?

@marcesher
Copy link

@dlai0001, using webdriver: driver.session_id

@cindysongzi
Copy link

Can you add another saucerest_python_example.py for python 3?

@kimarneves
Copy link

For python 3 just replace line 11 with:
base64string = str(base64.b64encode(bytes('%s:%s' % (config['username'], config['access-key']),'utf-8')))[1:]

I also recommend you close the socket after line 19:
connection.close()

@dimit999
Copy link

@dlai0001, using webdriver: driver.session_id

hi, could u please tell how to send this session ID to test attribute (because i use ReportPortal and for connections SauceLabs and ReportPortal -> becuase i see that test run in ReportPortal happens earlier then driver is started and we receive this session ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment