# Leapwork REST API example: Run a schedule, iterate through the results and create bugs in JIRA.# # # 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/v4/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/v4/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/v4/run/$runId" | ConvertFrom-Json } while ($runResult.Status -ne 'Finished') Write-Host "Results received." return $runResult } # Function that creates a bug with a specific title in JIRA. function CreateBug($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"} # Create json issue. $body = '{ "fields": { "project": { "key": "TEST0" }, "summary": "' + $bugTitle + '", "description": "' + $bugDescription + '", "issuetype": { "name": "Bug" } } } ' # Create bug. Invoke-RestMethod -Headers $header -Method Post -Body $body "https://YOURINSTANCE.atlassian.net/rest/api/2/issue/" } # 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/v4/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/v4/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/v4/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." }