Search code examples
seleniumazure-devopsazure-devops-hosted-agent

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


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.


Solution

  • 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