Import Test Case with API

Description

Sometimes there is a need to automate Test Case import. This article provides examples on how to do it in the following programming languages.

API endpoint: /v3.0/accounts/{accountId}/testcases/import

Request method: POST

Note: The size of a Test Case import file should not exceed 52.4MB.

Python

One of the most popular languages for scripting is Python. Following is the code that shows how to import a sample test case:

import requests
url = "https://www.cyaraportal.us/cyarawebapi/v3.0/accounts/[account_id]/testcases/import"
header = requests.structures.CaseInsensitiveDict()
header["accept"] = "application/json"
header["Content-Type"] = "multipart/form-data"
header["Authorization"] = "ApiKey [enter your key here]"
attachment = "/Users/username/Desktop/SampleTC.xml"
with open(attachment, 'rb') as f:
r=requests.post(url, headers=header, data=f)
print(r.content)
print(r.status_code)

 

PowerShell

Another popular method is to use PowerShell on Windows. PowerShell is a very powerful tool that allows to do a lot of things including operations with API.

The following example circles through the range of accounts and import given Test Case XML file to each of the accounts in the defined range:

#start with accepting input parameters from the command line
param([string]$url = "https://enter your URL here",
[string]$APIKey = "use your Key Here",
[string]$filename = ".\TestCases.xml",
[int32]$end_accountID=5,
[int32]$start_accountID=2)
clear-host
pause
#Assign variables
$accept_header = "application/json"
$content_header = "Content-Type: multipart/form-data"
#I renamed input variables, but dint want to change the code
$AccountID = $end_accountID
$i = $start_accountID
#build the API Key for the auth header
$API='ApiKey '+$APIKey
#Build the api url
$apiurl = $url+'/cyarawebapi/v3.0/accounts/'
#create HTTP Request headers object
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $API)
$headers.Add("Accept", $accept_header)
$headers.Add("Content-Type", $content_header)

# This block is to skip security sertificate validation
# it will allow script to work with the self-signed certificates
if (-not("dummy" -as [type])) {
add-type -TypeDefinition @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public static class Dummy {
public static bool ReturnTrue(object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; }
public static RemoteCertificateValidationCallback GetDelegate() {
return new RemoteCertificateValidationCallback(Dummy.ReturnTrue);
}
}
"@
}
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [dummy]::GetDelegate()
#end of certificate block

#begin
write 'StartAccountID '+$start_accountID
write 'EndAccountID '+$end_accountID
#cycle through all the accounts in the defined range and import the given Test Case XML file
foreach ($_ in $i..$AccountID)
{
#build the final API URL call
$resource = $apiurl+$i+'/testcases/import'
#execute the POST API call to import the file
$return = Invoke-RestMethod -Method POST -Uri $resource -Headers $headers -ContentType "application/json" -Infile $filename
#return the result in more or less readable format
ConvertTo-Json $return

#Write to disk
ConvertTo-Json $return | Out-File output.json
}

Was this article helpful?

1 out of 1 found this helpful