Azure devops: Screenshot are not showing in attachment tab

2.3k Views Asked by At

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, enter image description here

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?

2

There are 2 best solutions below

10
On BEST ANSWER

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

enter image description here

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
0
On

Powershell script task1 to get the run id:

$AzureDevOpsPAT = {PAT}
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$UriOrga = "https://dev.azure.com/{org}/{proj}/" 
$uriAccount = $UriOrga + "_apis/test/runs?api-version=6.0"

$response = Invoke-RestMethod -Uri $uriAccount -Headers $AzureDevOpsAuthenicationHeader -Method Get
$testRunsIdSorted = $response.value | sort-object id -Descending
$result = Invoke-RestMethod -Uri https://dev.azure.com/{org}/{proj}/_apis/test/runs/$($testRunsIdSorted[0].id)?api-version=6.0 -Headers $AzureDevOpsAuthenicationHeader -Method Get
Write-Host "results = $($result | ConvertTo-Json -Depth 100)"
Write-Host "##vso[task.setvariable variable=runId]$($result.id | ConvertTo-Json -Depth 100)"

Powershell task2 to attach the screenshot attachment:

$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
$TestTitle=$Run.testCase.name
$CharArray =$TestTitle.Split(".")
$TestCase=$CharArray[6]

$CharArraytwo=$TestCase.Split("(")
$TestName =$CharArraytwo[0]

$file= [IO.File]::ReadAllBytes("$(System.DefaultWorkingDirectory)\{Source alias}\tests\{testproject}\bin\Release\$TestName.png")
$Base64file= [Convert]::ToBase64String($file)

#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": "$Base64file",
  "fileName": "$TestName.png",
  "comment": "Test attachment upload",
  "attachmentType": "GeneralAttachment"
}
"@
$TestResultAttachmentResult = Invoke-RestMethod -Uri $TestResultAttachmentURL -ContentType "application/json" -Body $body -Headers $AzureDevOpsAuthenicationHeader -Method POST
}
}

The C# code was correct. In VS test task, PowerShell tasks remember to enable "Conitnue on error". Otherwise, if the task failed, others will not execute.