0

I have a suite of Selenium Tests that run inside Windows 2019 Azure Hosted Agent. Each Test captures an image of a particular web page.

For future test runs these images will be used as a baseline when comparing the current/actual state of a web page.

Is there a way to save and retrieve these baseline images somewhere when tests are running in the hosted agent?

Notes:

  • Currently I am using a workaround so I can save and upload the baseline images generated from the hosted agent. Force the tests to fail and attach the images in the test results via TestContext.AddResultFile. Then download each image and save it to local repo and then sync in azure repo. I don't want to do this any longer because there are hundreds of images to be generated already.

  • I can't use the baseline images generated from my local machine because the hosted agent has a different viewport. Hence, the image comparison will always find a difference between the baseline and actual.

  • comparing images is not a great idea. There can be differences regarding font rendering for instance... (rounding when smoothing text for display) Having said that, you can store all images as base-64 in a single html page for cataloging the whole test in one file. – pcalkins Jan 15 '21 at 20:03
  • @pcalkins This is not a viable solution for us. We tried converting and comparing the images as base64 strings and it took a longer time (+1 min average) to execute each test. But thank you for your insight. – ArkishaFran Jan 20 '21 at 10:22
  • Did you get a chance to check out below answer? How did it go? – Levi Lu-MSFT Jan 22 '21 at 09:45
  • @LeviLu-MSFT yes. I just accepted the answer, sorry. I was able to do it on my end. I can now easily update and add baseline images when needed. Thank you – ArkishaFran Jan 22 '21 at 11:26

1 Answers1

1

If you saved your screenshots to hosted agent via selenium. You can publish the test screenshots to azure pipeline build artifacts via Publish Build Artifacts task. See below example:

TestMethod:

 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();

Above code will save the screenshot to folder screenshots folder.

Then you can add a copy files task to copy the screenshots to folder $(build.artifactstagingdirectory)

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

And then. Add Publish Build Artifacts task to publish the screenshots to build artifact.

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

After your pipeline build is complete. You can download the screenshots directly from the summary page.

enter image description here

enter image description here

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • This is not exactly what I was looking for but I am accepting this answer because this is a better workaround for getting images captured from the azure hosted agent. – ArkishaFran Jan 22 '21 at 11:20