The LEAPWORK public REST API makes it very easy for DevOps, IT operations, and developers to integrate LEAPWORK with any third-party system. This article will instruct you how to automatically create bugs in HP Quality Center when automation flows in LEAPWORK fails.
The following is a Windows Powershell script that runs a pre-defined LEAPWORK schedule, polls for the results until they are available, and then loops through all failed cases and creates defects in HPQC as appropriate.
Please note that the script contains no error handling or logging mechanisms. It is meant only to demonstrate the core functionality of integrating LEAPWORK with HPQC.
Download sample PowerShell script.
# LEAPWORK REST API example: Run a schedule, iterate through the results and create defects in HP Quality Center.
#
#
# Author: Claus Topholt.
# Function that finds a schedule in LEAPWORK based on a title, runs it and polls for the results.
function RunScheduleAndGetResults($schedule)
{
Write-Host "Getting id for schedule '$schedule'."
# Get the id of the schedule.
$runScheduleId = "";
$headers = @{}
$headers.Add("AccessKey","bTyGAd0UGL70JFQg")
$runSchedules = Invoke-WebRequest -ContentType "application/json" -Headers $headers "http://localhost:9001/api/v3/schedules" | ConvertFrom-Json
foreach($runScheduleItem in $runSchedules)
{
if ($runScheduleItem.title -eq $schedule) { $runScheduleId = $runScheduleItem.id }
}
if ($runScheduleId -eq "") { throw "Could not find schedule '$schedule'." }
Write-Host "Running the schedule."
# Run the schedule now.
$timestamp = [DateTime]::UtcNow.ToString("ddMMyyyy HHmmss")
Start-Sleep 1
$runNow = Invoke-WebRequest -Method PUT -ContentType "application/json" -Headers $headers "http://localhost:9001/api/v3/schedules/$runScheduleId/runNow"
if ($runNow.StatusCode -ne 200) { throw "Could not run schedule." }
$runNowResponse=$runNow.Content | ConvertFrom-Json
$runId=$runNowResponse.RunId
# Get the result, keep polling every 5 seconds until a new result is returned.
do
{
Start-Sleep -Seconds 5
Write-Host "Polling for run results."
$runResult = Invoke-WebRequest -ContentType "application/json" -Headers $headers "http://localhost:9001/api/v3/run/$runId" | ConvertFrom-Json
}
while ($runResult.Status -ne 'Finished')
Write-Host "Results received."
return $runResult
}
# Function that creates a defect with a specific title in HPQC.
function CreateDefect($bugTitle, $bugDescription)
{
# Create an authorization header using basic auth.
$lp = "USERNAME:PASSWORD"
$lpEncoded = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($lp));
$header = @{"Authorization" = "Basic $lpEncoded"; "Content-Type" = "application/json"; "Accept" = "application/json"}
Invoke-WebRequest -Headers $header -Method Post -SessionVariable session "http://HPQC-SERVER:8080/qcbin/api/authentication/sign-in"
# Create json defect.
$data = '{ "data": [ { "type": "defect", "name": "' + $bugTitle + '", "description": "' + $bugDescription + '", "priority": "4-Very High", "severity": "2-Medium", "detected-by": "LEAPWORK", "creation-time": "2014-11-30" } ] }'
# Create defect.
Invoke-WebRequest -WebSession $session -Method Post -Body $data "http://HPQC-SERVER:8080/qcbin/api/domains/DEFAULT/projects/YOURPROJECT/defects"
}
# Run the LEAPWORK schedule "My Test Schedule" and get the results.
$runResult = RunScheduleAndGetResults("My Test Schedule")
# If there are any failed cases in the results, iterate through them.
if ($runResult.Failed -gt 0)
{
Write-Host "Found $($runResult.Failed) failed case(s)."
$headers = @{}
$headers.Add("AccessKey","bTyGAd0UGL70JFQg")
$runId=$runResult.RunId
$runItemIds = Invoke-WebRequest -ContentType "application/json" -Headers $headers http://localhost:9001/api/v3/run/$runId/runItemIds | ConvertFrom-Json
$rootPath =$runResult.RunFolderPath
foreach ($runItemId in $runItemIds.RunItemIds)
{
$runItems = Invoke-WebRequest -ContentType "application/json" -Headers $headers http://localhost:9001/api/v3/runItems/$runItemId | ConvertFrom-Json
if($runItems.FlowInfo.Status -eq 'Failed')
{
# Create a title for the bug.
$bugTitle = "LEAPWORK: " + $runItems.FlowInfo.FlowTitle
# Create a description that contains the log messages.
$newline = "\r\n";
$keyFrames = Invoke-WebRequest -ContentType "application/json" -Headers $headers http://localhost:9001/api/v3/runItems/$runItemId/keyframes/1 | ConvertFrom-Json
$bugDescription = "Log from LEAPWORK:$newline $newline"
foreach ($keyframe in $keyFrames)
{
if ($keyframe.Level -ge 1)
{
$keyframeTimestamp = get-date($keyframe.Timestamp.LocalDateTime) -Format "dd-MM-yyyy HH:mm:ss"
$bugDescription += "$keyframeTimestamp - $($keyframe.LogMessage) $newline"
}
}
# Add path to video and screenshots.
$mediaPath = Join-Path -Path $rootPath "$($runItems.RunItemId)"
$videoPath = Join-Path -Path $mediaPath "$($runItems.RunItemId).avi"
$videoPath = $videoPath.Replace('\', '\\')
$screenshotsPath = Join-Path -Path $mediaPath "Screenshots"
$screenshotsPath = $screenshotsPath.Replace('\', '\\')
$bugDescription += "$newline Video: $videoPath $newline"
$bugDescription += "$newline Screenshots (if any): $screenshotsPath $newline"
# Create or update bug in JIRA.
CreateBug $bugTitle $bugDescription
}
}
}
else
{
Write-Host "No failed cases found."
}
After running, defects will be created in HP Quality Center and can be managed from the client:
The above script uses HP Quality Center’s REST API to access HPQC.
You can explore the endpoints mentioned above by going to this url: http://localhost:9001/help/index if you have the Controller installed on your own computer.
Explore the full documentation of the LEAPWORK REST API.
If you have any questions, please contact priority support on prioritysupport@leapwork.com.