有没有办法在 Azure 托管代理中的 selenium 测试 运行 时保存和检索文件?

Is there a way to save and retrieve files while selenium tests are running in Azure Hosted Agent?

我在 Windows 2019 Azure 托管代理中有一套 运行 的 Selenium 测试。每个测试捕获特定网页的图像。

为了将来的测试 运行,这些图像将用作比较网页 current/actual 状态时的基线。

在托管代理中 运行 进行测试时,是否有办法在某处保存和检索这些基线图像?

备注:

如果您通过 selenium 将屏幕截图保存到托管代理。您可以通过 Publish Build Artifacts task 将测试屏幕截图发布到 azure pipeline build artifacts。请参阅以下示例:

测试方法:

 webDriver = new ChromeDriver();
 webDriver.Manage().Window.Maximize();
 webDriver.Navigate().GoToUrl(test_url);
           
 Screenshot screenshot = ((ITakesScreenshot)webDriver).GetScreenshot();
 
# create a screenshots directory to save the screenshots.
Directory.CreateDirectory("screenshots");
 
 string attachmentsPath = Directory.GetCurrentDirectory() + "\" +"screenshots"+"\" +
            "testName1.png";

 # save the screenshot to the screenshots directory.
 screenshot.SaveAsFile(attachmentsPath);
                    
 Assert.AreEqual(webDriver.Title,"Google");
 webDriver.Quit();

以上代码会将屏幕截图保存到文件夹 screenshots 文件夹。

然后您可以添加复制文件任务,将屏幕截图复制到文件夹$(build.artifactstagingdirectory)

- task: CopyFiles@2
  displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
  inputs:
    SourceFolder: '$(system.defaultworkingdirectory)'
    Contents: '**\screenshots\**'
    TargetFolder: '$(build.artifactstagingdirectory)'
    flattenFolders: true

然后。添加 Publish Build Artifacts 任务以发布屏幕截图以构建工件。

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: screenshots'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
    ArtifactName: screenshots

管道构建完成后。您可以直接从摘要页面下载屏幕截图。