Third-Party Integration

The Leapwork public REST API makes it very easy for DevOps, IT operations, and developers to integrate Leapwork with any third-party system.


The following is a Windows PowerShell script runs a pre-defined Leapwork schedule, polls for the results until they are available and then loops through all failed cases and shows how to get the relevant data structures within. The data can then be pushed to any third-party system such as TFS, JIRA or any other Application Lifecycle Management (ALM) system.

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 a third-party system.

Download PowerShell script sample.


		# LEAPWORK REST API example: Run a schedule and iterate through the results.
#
# Author: Claus Topholt.

# Step 1: Get the id of the schedule called "My Test Schedule".
$schedule = "My Test Schedule"
$runScheduleId = "";
echo "Getting id for schedule '$scheduleTitle'."  
$headers = @{}
$headers.Add("AccessKey","APIAccessKey")
$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'." }

# Step 2: Run the schedule now.
echo "Running the schedule."
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

# Step 3: 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."

# Step 4: If there are any failed cases, iterate through them.
if ($runResult.Failed -gt 0)
{
    Write-Host "Found $($runResult.Failed) failed case(s)."    
    $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')
        {
            # Here is a good place to do integration to eg. TFS, JIRA or any other Application Lifecycle Management system.

            # Output the log messages as an example.
            echo "Case '$($runItems.FlowInfo.FlowTitle)' failed. Here are the log messages:"                
           
            $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 6)
                {
                    $keyframeTimestamp = get-date($keyframe.Timestamp.LocalDateTime) -Format "dd-MM-yyyy HH:mm:ss"
                    echo "$keyframeTimestamp - $($keyframe.LogMessage)"
                }
            }            
        }
    }
}
else
{
    Write-Host "No failed cases found."
}
	

The script consists of four steps:

Step 1: Get the id of the schedule. This is done by a call to the REST API endpoint Get All Schedules.

Step 2: Run the schedule now. This is done by a call to Run Schedule Now, which will instruct the Controller to queue the schedule for execution as soon as possible.

Step 3: Keep polling for the result every 5 seconds until a new result is returned. This is done by a call to Get Run by Id, which will return either state as “Finished” or a “Executing” until new results are available.

Step 4: If there are any failed cases, iterate through the failed cases and use the data within to understand what went wrong. This can be very useful if for instance the integration is intended to automatically create new bugs in a bug-tracking system such as JIRA.

Running the script will yield results like the following:

S C:\LEAPWORK-Scripts> run-schedule.ps1
Getting id for schedule 'My Test Schedule'.
Running the schedule.
Polling for run results.
Polling for run results.
Polling for run results.
Results received.
Found 2 failed case(s).
Case 'Simple waiter with random outcome' failed. Here are the log messages:
29-08-2016 11:15:15 - Connecting to local
29-08-2016 11:15:15 - Connected to local
29-08-2016 11:15:15 - Running
29-08-2016 11:15:20 - Waited for 5 second(s).
29-08-2016 11:15:20 - 46 generated by number generator.
29-08-2016 11:15:20 - '46' is not greater than or equal to '50' (compared as Number).
29-08-2016 11:15:21 - Case is failed.
Case 'Always fails' failed. Here are the log messages:
29-08-2016 11:15:24 - Connecting to local
29-08-2016 11:15:24 - Connected to local
29-08-2016 11:15:24 - Running
29-08-2016 11:15:26 - Case is failed.

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.