I am trying to add failed test attachments in the Test tab in Azure DevOps using VS test task.
I am calling the Create Test Result Attachment rest api,
$AzureDevOpsPAT = {PAT}
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$result = Invoke-RestMethod -Uri https://dev.azure.com/{org}/{proj}/_apis/test/runs/$(runId)/results?api-version=6.0 -Headers $AzureDevOpsAuthenicationHeader -Method Get
#List all test result get the test result ID via result
foreach($Run in $result.value){
#Get the test result ID via result
If($Run.outcome -eq "Failed"){
$TestResultID = $Run.id
$Name=$Run.testCase.name
Write-Host $TestResultID $Name
#Add attachment via test run ID and test result ID
$TestResultAttachmentURL = "https://dev.azure.com/{org}/{proj}/_apis/test/Runs/$(runId)/results/$($TestResultID)/attachments?api-version=6.0-preview.1"
$body =@"
{
"stream": "abc",
"fileName": "$(System.DefaultWorkingDirectory)/$Name.png",
"comment": "Test attachment upload",
"attachmentType": "GeneralAttachment"
}
"@
$TestResultAttachmentResult = Invoke-RestMethod -Uri $TestResultAttachmentURL -ContentType "application/json" -Body $body -Headers $AzureDevOpsAuthenicationHeader -Method POST
}
}
I cant see the respective screenshot, the black flower is showing if I click on .png file in the attachment tab,
Though I am capturing a screenshot in my code too,
protected void StopWebDriver()
{
if (WebDriver != null)
{
string path = Directory.GetCurrentDirectory() + "\\" + BaseConfig.TestCaseName + ".png";
((ITakesScreenshot)WebDriver).GetScreenshot().SaveAsFile(path, ScreenshotImageFormat.Png);
WebDriver.Close();
WebDriver.Quit();
}
}
Can anyone please tell me how can I see screenshots?
Based on my test , the screenshot could show in the Pipeline -> Test -> Attachment tab.
You could use the PowerShell Script to generate the Base64 file Stream instead of hardcode the file stream.
Here is an example:
$file= [IO.File]::ReadAllBytes("filepath\$Name.png")
$Base64file= [Convert]::ToBase64String($file)
echo $Base64file
$token = "PAT"
$url="https://dev.azure.com/{org}/{proj}/_apis/test/Runs/$(runId)/results/$($TestResultID)/attachments?api-version=6.0-preview.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = "
{
`"stream`": `"$Base64file`",
`"fileName`": `"$Name.png`",
`"comment`": `"Test attachment upload`",
`"attachmentType`": `"GeneralAttachment`"
}"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
On the other hand, you could directly try to use the Publish screenshots for test failure task from the extension Publish test result screenshot.
In addition, in order to confirm whether the image you uploaded is valid, you can also download and check whether it is the correct content.
Update:
To get the Test case name in a string, you could refer to this sample:
$String= "ooo.iii.kkk.lll"
$a,$b,$c,$d = $String.Split(".",4)
echo $c