有没有办法在 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 状态时的基线。
在托管代理中 运行 进行测试时,是否有办法在某处保存和检索这些基线图像?
备注:
目前我正在使用一种解决方法,以便我可以保存和上传从托管代理生成的基线图像。强制测试失败并通过 TestContext.AddResultFile.
在测试结果中附加图像,然后下载每个图像并将其保存到本地存储库,然后在 azure 存储库中同步。我不想再这样做了,因为已经有数百张图像要生成了。
我无法使用从本地计算机生成的基线图像,因为托管代理具有不同的视口。因此,图像比较总是会发现基线和实际之间存在差异。
如果您通过 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
管道构建完成后。您可以直接从摘要页面下载屏幕截图。
我在 Windows 2019 Azure 托管代理中有一套 运行 的 Selenium 测试。每个测试捕获特定网页的图像。
为了将来的测试 运行,这些图像将用作比较网页 current/actual 状态时的基线。
在托管代理中 运行 进行测试时,是否有办法在某处保存和检索这些基线图像?
备注:
目前我正在使用一种解决方法,以便我可以保存和上传从托管代理生成的基线图像。强制测试失败并通过
TestContext.AddResultFile.
在测试结果中附加图像,然后下载每个图像并将其保存到本地存储库,然后在 azure 存储库中同步。我不想再这样做了,因为已经有数百张图像要生成了。我无法使用从本地计算机生成的基线图像,因为托管代理具有不同的视口。因此,图像比较总是会发现基线和实际之间存在差异。
如果您通过 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
管道构建完成后。您可以直接从摘要页面下载屏幕截图。