Description
Sometimes there is a need to export test cases in bulk. However, when the number of test cases is too large, Cyara web UI may become unresponsive and even timeout. This article provides an example on how to do a massive test cases library export using Cyara API.
API endpoint: /v3.0/accounts/{accountId}/testcases/cyaraxml
Request method: GET
The approach that we will be using is to break down the large test cases library onto smaller parts and export them separately.
Cyara API allows exporting test cases one by one or folder by folder. Note, that when a folder contains a large number of test cases (>5000), performance may also be impacted and in this situation this article may not help.
Creating Supplemental Functions
-
Create HTTP Headers and global variables:
header = requests.structures.CaseInsensitiveDict()
header["accept"] = "application/json"
header["Content-Type"] = "multipart/form-data"
header["Authorization"] = "ApiKey [insert your API Key here]
path='D:/CYARA/API-Examples/Export/'
account_id='1'
-
Create HTTP Functions that we will use to fetch the data:
def get_resource(url, status=200):
response = requests.get(url, headers=header)
print (response.status_code)
if response.status_code != status:
print('Failed to get the record with error {}:'.format(response.status_code))
#print(response.text)
return False
return response
# HTTP POST FUNCTION
def post_resource(url,data,status = 200):
resource = None
response = requests.post(url, data=data,headers=header)
if response.status_code != status:
print('Failed to create the record with error {}:'.format(response.status_code))
print(response.text)
return False
-
Create Cyara specific API functions:
- Function to get the list of folders for a given account:
# Get the list of folders
def list_folders():
folders = []
url = 'https://cyaraportal.url.com/cyarawebapi/v3.0/accounts/{0}/directories/TestCase/folders'.format(account_id)
result = get_resource(url).json()
folders.extend(result)
return folders
-
- Function to export and save test cases as independent files:
# Export and save test cases from the folder to a file folderID.xml
def export_testcases(folder_id):
exp_url = 'https://cyaraportal.url.com/cyarawebapi/v3.0/accounts/{0}/testcases/cyaraxml?folderId={1}&recursive=false&includeDataDriven=true&includeServices=true&download=false&blockOptions=IncludeBlocks'.format(account_id,folder_id)
result = get_resource(exp_url)
with open(path+str(folder_id)+'.xml',mode="wb") as f:
f.write(result.content)
-
- Function to import test cases:
def import_testcase(filename):
url = 'https://cyaraportal.url.com/cyarawebapi/v3.0/accounts/{0}/testcases/import'.format(account_id)
with open(path+filename,'rb') as data:
r=post_resource(url,data)
Using Functions To Export And Import
Now let's use the functions above to export and import test cases.
- To go through all folders and export all test cases from these folders individually, we need to first list all folders, and then go folder by folder and save test cases into separate XML file:
folders = list_folders()
# Now we can go folder by folder and export test cases
for folder in folders:
print (str(folder['folderId']))
folder_id=folder['folderId']
export_testcases(folder_id) - Once test cases are exported, you can import them to a different location (account, cyara portal instance, etc.) using the following code:
for file in os.listdir(path):
print ('Importing file: '+str(file))
import_testcase(file)
Comments
0 comments
Please sign in to leave a comment.