Azure devops:屏幕截图未显示在附件选项卡中

Azure devops: Screenshot are not showing in attachment tab

我正在尝试使用 VS 测试任务在 Azure DevOps 的“测试”选项卡中添加失败的测试附件。

我打电话给 Create Test Result Attachment 休息 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
}
}

我看不到相应的屏幕截图,如果我在附件选项卡中单击 .png 文件,就会显示黑花,

虽然我也在我的代码中捕获屏幕截图,

protected void StopWebDriver()
        {
            if (WebDriver != null)
            {
             string path = Directory.GetCurrentDirectory() + "\" + BaseConfig.TestCaseName + ".png";
            ((ITakesScreenshot)WebDriver).GetScreenshot().SaveAsFile(path, ScreenshotImageFormat.Png);
            WebDriver.Close();
            WebDriver.Quit();
                
            }
        }

谁能告诉我怎样才能看到截图?

根据我的测试,屏幕截图可以显示在 管道 -> 测试 -> 附件选项卡

您可以使用 PowerShell 脚本生成 Base64 文件流,而不是对文件流进行硬编码。

这是一个例子:

$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

另一方面,您可以直接尝试使用 Publish screenshots for test failure 任务 来自扩展 Publish test result screenshot.

另外,为了确认您上传的图片是否有效,您也可以下载查看是否是正确的内容。

更新:

要获取字符串中的测试用例名称,您可以参考这个示例:

$String= "ooo.iii.kkk.lll"
$a,$b,$c,$d = $String.Split(".",4)

echo $c

Powershell 脚本 task1 获取 运行 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 附上截图附件:

$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
}
}

C# 代码是正确的。在 VS 测试任务中,PowerShell 任务记得启用“Conitnue on error”。否则,如果任务失败,其他人将不会执行。