Last active
July 24, 2018 06:48
-
-
Save pvasek/eb4596d8dc3fff319a307dcfdba81e9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Uploads all screenshot images generated by end to end test to the TFS/vsonline server. | |
.DESCRIPTION | |
The images are expected to be organized in the $screenshotRootDir in subfolders named as Release.Name | |
for example `C:/screenshots/Release-21`. The images should be named as the test itself for example | |
if we have test `Should_add_new_user` the image has to be named `Should_add_new_user.png`. | |
This script uses the OAuth token to access TFS therefore the option "Allow scripts to access OAuth token" | |
has to be enabled on the build that use it. | |
#> | |
$urlRoot = "<your tfs url>/<colleciton>/<project>" # must include collection/project | |
$screenshotRootDir = "c:\screenshots" | |
$releaseId = $env:RELEASE_RELEASEID | |
$environmentId = $env:RELEASE_ENVIRONMENTID | |
$phaseId = $env:RELEASE_DEPLOYPHASEID | |
$releaseName = $env:RELEASE_RELEASENAME | |
$taskId = 8 # the id of the "Publish Test Results" task | |
$apiVersion = "4.0-preview" | |
$imageFilter = "*.png" | |
$headers = @{ | |
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" # ensure that the option "Allow scripts to access OAuth token" is checked on the build | |
} | |
# 1) read the logs from "Publish Test Results" and try to find the test run id | |
$getLogUrl = "$urlRoot/_apis/release/releases/$releaseId/environments/$environmentId/deployPhases/$phaseId/tasks/$taskId/logs" | |
$getLogResponse = Invoke-RestMethod -Uri $getLogUrl -Headers $headers | |
$lines = $getLogResponse.split([System.Environment]::NewLine) | |
$testRunIdText = ($lines | select-string -pattern "(?:Test run id: )(\d+)") | |
$runId = $testRunIdText.Matches[0].Groups[1].Value | |
# 2) read all results that can be used to find appropriate test id based on test name | |
$getResultsUrl = "$urlRoot/_apis/test/Runs/$runId/Results?api-version=$apiVersion" | |
$getResultsResponse = Invoke-RestMethod -Uri $getResultsUrl -Headers $headers -ContentType "application/json" | |
# 3) find all screenshots in the screenshot folder | |
$screenshots = Get-Item -Path "$screenshotRootDir\$releaseName\$imageFilter" | |
foreach ($screenshot in $screenshots) { | |
# 3.1) find test id for the given screenshot name | |
$testId = ($getResultsResponse.value | Where-Object { $_.testCase.name.EndsWith($screenshot.BaseName)}).id | |
$url = "$urlRoot/_apis/test/Runs/$runId/results/$testId/attachments?api-version=$apiVersion" | |
# 3.2) prepare body that should be send to the server | |
$stream = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes($screenshot.FullName)) | |
$body = @{ | |
stream = $stream | |
fileName = $screenshot.Name | |
attachmentType = "GeneralAttachment" | |
} | |
# 3.3) upload screenshot to the server | |
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -ContentType "application/json" -Body ($body | ConvertTo-Json) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment