Param ( [string]$result, [string]$rootPath ) # ************************************************************************************* # ADD YOUR INFO HERE: # Domain name where the TFS API is hosted $domain = "https://dev.azure.com/akr2303" # Path to the project to integrate to $tfsPath = "/Leapwork_test" # Authentication to the TFS API is based on Personal Access Token. $pat = "kpoitii6ndel2zeocboiopn623kz6iinaf4or35xwlqqtpnqtogq" # Log file used for integrating to TFS. New log entries are appended to this file. $logfile = "D:\scripts\tfs.log" # ************************************************************************************* # IterateResult looks into the result data coming from Leaptest. # If any failed cases are found in the result data, the failures are iterated # and the title and description for the failure is calculated. function IterateResult($result, $rootPath) { $now = Get-Date Write-Output "" Write-Output "$now ---------" try { $runResult = "[$result]" | ConvertFrom-Json # converting from Leaptest JSON object to PS object # If there are any failed cases in the results, iterate through them. if ($runResult.FailedCount -gt 0) { Write-output "Found $($runResult.FailedCount) failed case(s)." foreach ($item in $runResult.RunItems) { if ($item.Status -eq "Failed") { # Create a title for the bug. $bugTitle = "Leaptest: " + $item.Case.Title + " (" + $item.Environment.Title + ")" # Create a description that contains the log messages. $bugDescription = "Log from Leaptest:

" foreach ($keyframe in $item.Keyframes) { if ($keyframe.Level -ge 1) { $keyframeTimestamp = get-date($keyframe.Timestamp) -Format "dd-MM-yyyy HH:mm:ss" $bugDescription += "$keyframeTimestamp - $($keyframe.LogMessage)
" } } # Add path to video and screenshots. $videoPath = Join-Path -Path $rootPath "$($item.Id).avi" $videoPath = $videoPath.Replace('\', '\\') $screenshotsPath = Join-Path -Path $rootPath "$($item.Id)\" $screenshotsPath = $screenshotsPath.Replace('\', '\\') $bugDescription += "
Video: $videoPath
" $bugDescription += "
Screenshots (if any): $screenshotsPath
" # Create or update bug in TFS. CreateOrUpdateBug($bugTitle, $bugDescription) # Create of Update the bug found } } } else { Write-output "No failed cases found" } } Catch { Write-Output $_.Exception.GetType().FullName Write-Output "Message: " $_.Exception.Message Write-Output "Line number: " $_.Exception.InvocationInfo.ScriptLineNumber } } # Function that creates a bug with a specific title in TFS or updates if it already exists. function CreateOrUpdateBug($title, $description) { # Create an authorization header using a Personal Access Token. $patEncoded = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)")); $header = @{"Authorization" = "Basic $patEncoded"} # See if a bug with the same title already exists. $queryAlreadyExists = '{ "query": "SELECT [System.Id], [System.Title], [System.State] FROM WorkItems ' + 'WHERE [System.WorkItemType] = ''Bug'' AND [System.Title] = ''' + $bugTitle + ''' AND [System.State] <> ''Removed'' ' + 'ORDER BY [System.Id] DESC" }' $urlAlreadyExists = "${domain}${tfsPath}/_apis/wit/wiql?api-version=1.0" $header['Content-Type'] = 'application/json' $resultAlreadyExists = Invoke-RestMethod -Headers $header -Method Post -Body $queryAlreadyExists $urlAlreadyExists # If the bug already exists, update it. if ($resultAlreadyExists.workItems.Count -gt 0) { Write-Output "Updating test case i TFS: " $title # Add a note that this bug was updated or re-opened. $timestamp = [DateTime]::UtcNow.ToString("dd-MM-yyyy HH:mm:ss") $bugDescription = "Updated or re-opened by Leaptest on $timestamp.

" + $bugDescription # Update bug. $queryUpdateBug = '[ { "op" : "replace", "path" : "/fields/System.Title", "value" : "' + $bugTitle + '" }, ' + '{ "op" : "add", "path" : "/fields/Microsoft.VSTS.TCM.ReproSteps", "value" : "' + $bugDescription + '" }, ' + '{ "op" : "replace", "path" : "/fields/System.State", "value" : "New" } ]' $urlUpdateBug = "${domain}/_apis/wit/workitems/$($resultAlreadyExists.workItems[0].id)?api-version=1.0" $header['Content-Type'] = 'application/json-patch+json' $resultUpdateBug = Invoke-RestMethod $urlUpdateBug -Headers $header -Method Patch -Body $queryUpdateBug } else { Write-Output "Created new test case in TFS: " $title $queryCreateNewBug = '[ { "op" : "add", "path" : "/fields/System.Title", "value" : "' + $bugTitle + '" }, ' + '{ "op" : "add", "path" : "/fields/Microsoft.VSTS.TCM.ReproSteps", "value" : "' + $bugDescription + '" }, ' + '{ "op" : "add", "path" : "/fields/System.State", "value" : "New" } ]' $urlCreateNewBug = "${domain}${tfsPath}/_apis/wit/workitems/`$Bug?api-version=1.0" $header['Content-Type'] = 'application/json-patch+json' $resultCreateNewBug = Invoke-RestMethod $urlCreateNewBug -Headers $header -Method Patch -Body $queryCreateNewBug } } # Starting point. All outputs are piped to the log file. IterateResult $result $rootPath | Write-Output | Out-File -FilePath $logfile -Append