Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created June 18, 2013 21:31
Show Gist options
  • Select an option

  • Save tonetheman/5809624 to your computer and use it in GitHub Desktop.

Select an option

Save tonetheman/5809624 to your computer and use it in GitHub Desktop.
CrossBrowserTesting.com sample for start/stopping and querying running live tests
import time, json
# we use python a lot
# to do api calls with api requests is a great way to go
# see here http://docs.python-requests.org/en/latest/
import requests
# put your own auth information here, login information
# for cbt
# username and password
AUTH_INFO = ("USERNAME-HERE", "PASSWORD-HERE")
APISERVER="http://crossbrowsertesting.com"
# wrapped up way of calling a HTTP GET
def gentest(testname, url, params=None):
# print testname
if params is None:
r = requests.get(url, auth = AUTH_INFO)
else:
r = requests.get(url, auth= AUTH_INFO, params = params)
# for debugging really
# save the data out to a file so you can see
outf = open(testname+".txt","w")
outf.write(r.text)
outf.close()
return (r.status_code, r.text)
# wrapped up way of calling a HTTP POST
def posttest(testname, url, params=None):
# print testname
if params is None:
r = requests.post(url, auth=AUTH_INFO)
else:
r = requests.post(url, auth=AUTH_INFO, params = params)
# for debugging really
# save the data out to a file so you can see
outf = open(testname+".txt","w")
outf.write(r.text)
outf.close()
return (r.status_code, r.text)
def start_test(myurl):
# start a test, HTTP POST
(status_code, data) = posttest("livetest0",
APISERVER + "/api/v3/livetests",
{ "url" : myurl,
"browser" : "Ubuntu11.10|FF9|800x600" })
# do some python magic to get the json
# object into a python object
o = json.loads(data)
live_test_id = o["live_test_id"]
# print some debugging so we will know what is going on
print "\tlive test id that we started", live_test_id
print "\tstate at time of start", o["state"]
return live_test_id
def get_info(live_test_id):
(staus_code, data) = gentest("getlivetest0",
APISERVER + "/api/v3/livetests/" +
str(live_test_id))
o = json.loads(data)
print "\tlive_test_id", o["live_test_id"]
print "\tstate", o["state"]
return o["state"]
def stop_test(live_test_id):
# stop a live test, HTTP DELETE
url = APISERVER + "/api/v3/livetests/" + str(live_test_id)
r = requests.delete(url, auth = AUTH_INFO)
return (r.status_code, r.text)
def nap(seconds):
print "sleeping for", seconds, "..."
time.sleep(seconds)
def test():
print "starting a test now"
live_test_id = start_test("http://yahoo.com")
state = get_info(live_test_id)
print "looking for a running state..."
while state!="running":
nap(5)
state = get_info(live_test_id)
nap(30)
print "stopping..."
stop_test(live_test_id)
state = get_info(live_test_id)
# looking for a stopped state
if state != "stopped":
nap(2)
state = get_info(live_test_id)
print "finished"
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment