Skip to content
63 changes: 57 additions & 6 deletions testdroid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ class Testdroid:
# polling interval when awaiting for test run completion
polling_interval_mins = 10

""" Constructor, defaults against cloud.testdroid.com
""" Constructor, defaults against cloud.bitbar.com
"""
def __init__(self, **kwargs):
self.api_key = kwargs.get('apikey')
self.username = kwargs.get('username')
self.password = kwargs.get('password')
self.cloud_url = kwargs.get('url') or "https://cloud.testdroid.com"
self.cloud_url = kwargs.get('url') or "https://cloud.bitbar.com"
self.download_buffer_size = kwargs.get('download_buffer_size') or 65536

def set_apikey(self, apikey):
Expand Down Expand Up @@ -184,7 +184,6 @@ def get_token(self):
def _build_headers(self):
if self.api_key:
apikey = {'Authorization' : 'Basic %s' % base64.b64encode((self.api_key+":").encode(encoding='utf_8')).decode(), 'Accept' : 'application/json' }
#print(apikey)
return apikey
else:
return { 'Authorization': 'Bearer %s' % self.get_token(), 'Accept': 'application/json' }
Expand Down Expand Up @@ -285,11 +284,23 @@ def get_me(self):
def get_device_groups(self, limit=0):
return self.get("me/device-groups", payload = {'limit': limit})

""" Returns list of frameworks
"""
def get_frameworks(self, limit=0):
return self.get("me/available-frameworks", payload = {'limit': limit})

""" Returns list of devices
"""
def get_devices(self, limit=0):
return self.get(path = "devices", payload = {'limit': limit})


""" Print input files
"""
def print_input_files(self, limit=0):
for input_file in self.get_input_files(limit)['data']:
print("id:{} name:{} size:{} type:{}".format(input_file['id'],input_file['name'],input_file['size'],input_file['inputType']))

""" Print device groups
"""
def print_device_groups(self, limit=0):
Expand All @@ -306,8 +317,18 @@ def print_available_free_android_devices(self, limit=0):
for device in self.get_devices(limit)['data']:
if device['creditsPrice'] == 0 and device['locked'] == False and device['osType'] == "ANDROID":
print(device['displayName'])

print("")

""" Print available frameworks
"""
def print_available_frameworks(self, os_type=None, limit=0):
print("")
print("Available frameworks")
print("------------------------------")
for framework in self.get_frameworks(limit)['data']:
print("id: {}\tosType:{}\tname:{}".format(framework['id'], framework['osType'], framework['name']))
print("")


""" Print available free iOS devices
"""
Expand Down Expand Up @@ -431,6 +452,28 @@ def set_project_framework(self, project_id, frameworkId):
}
self.post(path, payload={"frameworkId": frameworkId})


""" Start a test run using test run config
e.g '{"frameworkId":12252,
"osType": "ANDROID",
"projectId":1234,
"files":[{"id":9876}, {"id":5432}]
"testRunParameters":[{"key":"xyz", "value":"abc"}],
"deviceGroupId":6854
}'
client.start_test_run_using_config(json.dumps({"frameworkId":123213}))
"""
def start_test_run_using_config(self, test_run_config={}):
if type(test_run_config) == str:
payload = json.loads(test_run_config)
else:
payload = test_run_config

me = self.get_me()
path = "users/%s/runs" % (me['id'])
test_run = self.post(path=path, payload=test_run_config, headers={'Content-type': 'application/json', 'Accept': 'application/json'})
return test_run

""" Start a test run on a device group
"""
def start_test_run(self, project_id, device_group_id=None, device_model_ids=None, name=None, additional_params={}):
Expand Down Expand Up @@ -565,6 +608,11 @@ def get_device_run_files(self, project_id, test_run_id, device_session_id, tags=
else:
return self.get("me/projects/%s/runs/%s/device-sessions/%s/output-file-set/files?tag[]=%s" % (project_id, test_run_id, device_session_id, tags))

""" Get list of input files
"""
def get_input_files(self, limit=0):
return self.get("me/files?limit={}&filter=s_direction_eq_INPUT".format(limit))

""" Downloads test run files to a directory hierarchy
"""
def download_test_run(self, project_id, test_run_id):
Expand Down Expand Up @@ -709,8 +757,8 @@ def format_epilog(self, formatter):
help="Username - the email address. Optional. You can use environment variable TESTDROID_USERNAME as well.")
parser.add_option("-p", "--password", dest="password",
help="Password. Required if username is used. You can use environment variable TESTDROID_PASSWORD as well.")
parser.add_option("-c", "--url", dest="url", default="https://cloud.testdroid.com",
help="Cloud endpoint. Default is https://cloud.testdroid.com. You can use environment variable TESTDROID_URL as well.")
parser.add_option("-c", "--url", dest="url", default="https://cloud.bitbar.com",
help="Cloud endpoint. Default is https://cloud.bitbar.com. You can use environment variable TESTDROID_URL as well.")
parser.add_option("-q", "--quiet", action="store_true", dest="quiet",
help="Quiet mode")
parser.add_option("-d", "--debug", action="store_true", dest="debug",
Expand All @@ -722,6 +770,7 @@ def get_commands(self):
"me": self.get_me,
"device-groups": self.print_device_groups,
"available-free-devices": self.print_available_free_devices,
"available-frameworks": self.print_available_frameworks,
"projects": self.print_projects,
"create-project": self.create_project,
"delete-project": self.delete_project,
Expand All @@ -730,12 +779,14 @@ def get_commands(self):
"upload-data": self.upload_data_file,
"set-project-config": self.set_project_config,
"start-test-run": self.start_test_run,
"start-test-run-using-config": self.start_test_run_using_config,
"start-wait-download-test-run":self.start_wait_download_test_run,
"wait-test-run":self.wait_test_run,
"test-run": self.get_test_run,
"test-runs": self.print_project_test_runs,
"device-runs": self.get_device_runs,
"device-run-files": self.get_device_run_files,
"list-input-files": self.print_input_files,
"download-test-run": self.download_test_run,
"download-test-screenshots": self.download_test_screenshots
}
Expand Down