飞跃 上市 REST API 使DevOps,IT运营和开发人员可以很轻松地将LEAPWORK与任何第三方系统集成。 本文将指导您如何 当LEAPWORK中的自动化流程失败时,会在HP Quality Center中自动创建错误。
以下是Windows Powershell脚本,该脚本运行预定义的LEAPWORK计划,轮询结果直到可用,然后遍历所有失败的情况并创建缺陷 in HPQC as appropriate.
请注意,该脚本不包含任何错误处理或日志记录机制。它仅旨在演示将LEAPWORK与HPQC集成的核心功能。
# 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 在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."
}
运行后,将在HP Quality Center中创建缺陷,并可以从客户端进行管理:
上面的脚本使用HP Quality Center的REST API来访问HPQC。
您可以通过以下URL探索上述端点: http:// localhost:9001 / help / index 如果您在自己的计算机上安装了Controller。
如有任何疑问,请联系优先支持 prioritysupport@leapwork.com.