Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created August 29, 2012 21:23
Show Gist options
  • Save tonetheman/3519164 to your computer and use it in GitHub Desktop.
Save tonetheman/3519164 to your computer and use it in GitHub Desktop.
testing a screenshot run
import urllib, urllib2, httplib, base64, json, time
class CBTAPI:
def setup_auth(self):
self.b64string = base64.encodestring("%s:%s" %
(self.username, self.password))
self.b64string = self.b64string.replace("\n","")
def add_auth(self, req):
req.add_header("Authorization", "Basic %s" % self.b64string)
def __init__(self, username, password):
self.username = username
self.password = password
self.setup_auth()
def list_screenshot_browsers(self):
SU="http://crossbrowsertesting.com/api/v2/screenshots/browsers?format=json"
req = urllib2.Request(SU)
self.add_auth(req)
u = urllib2.urlopen(req)
data = u.read()
u.close()
return data
def run_screenshot(self, url_to_run):
SU="http://crossbrowsertesting.com/api/v2/screenshots/run"
params = [ ("format", "json"),
("url", url_to_run),
("browsers", "Ubuntu11.10|Chromium14")]
req = urllib2.Request(SU, urllib.urlencode(params))
self.add_auth(req)
u = urllib2.urlopen(req)
data = u.read()
u.close()
return data
def run_screenshot2(self, url_to_run):
SU="http://crossbrowsertesting.com/api/v2/screenshots/run"
params = [ ("format", "json"),
("url", url_to_run),
("browsers", "Ubuntu11.10|Chromium14"),
("browsers", "Ubuntu11.10|FF13")]
print "these are my params", urllib.urlencode(params)
req = urllib2.Request(SU, urllib.urlencode(params))
opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1))
self.add_auth(req)
# u = urllib2.urlopen(req)
# data = u.read()
# u.close()
data = opener.open(req).read()
return data
def poll(self, url_to_poll):
req = urllib2.Request(url_to_poll)
self.add_auth(req)
u = urllib2.urlopen(req)
data = u.read()
u.close()
return data
def testing():
USERNAME = "tunneluser2"
PASSWORD = "???????"
cbt = CBTAPI(USERNAME, PASSWORD)
data = cbt.list_screenshot_browsers()
jdata = json.loads(data)
list_os = jdata["response"]["oss"]
for os in list_os:
name = os["name"]
api_name = os["api_name"]
if api_name == "Ubuntu11.10":
print os
print name, api_name
print "about to run a screenshot test on Ubuntu11.10 with Chromium14"
data = cbt.run_screenshot("http://yahoo.com")
data = json.loads(data)
print data
print "-------------------"
print "checking this response code", data["response"]["error"]
if data["response"]["error"] == 0:
test_id = data["response"]["test"]["id"]
version_id = data["response"]["test"]["version"]["id"]
poll_url = data["response"]["test"]["version"]["poll_status_api_url"]
print "screenshot has started for this test id and version id", test_id, version_id
print "poll url is", poll_url
poll_url = poll_url + "?format=json"
print "poll url with format is", poll_url
MAX_POLL_COUNT = 10
poll_count = 0
while True:
print "sleeping 5 seconds"
time.sleep(5)
if poll_count > MAX_POLL_COUNT:
print "gave up on the polling :("
break
poll_count = poll_count + 1
pdata = cbt.poll(poll_url)
pdata = json.loads(pdata)
if pdata["response"]["complete"]==1:
print "finished the screenshot test!"
print pdata
break
print pdata
def testing2():
USERNAME = "tunneluser2"
PASSWORD = "?????"
cbt = CBTAPI(USERNAME, PASSWORD)
# run a screenshot with 2 browsers no polling here
data = cbt.run_screenshot2("http://cnn.com")
print data
testing2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment