4

I am taking screenshot during UI Test execution by Azure Agent.

For some reason, this line never completes, there is no error, no exception it just waits on it indefinitely:

g.CopyFromScreen(Point.Empty, Point.Empty, new Size(recorderParams.SourceWidth, recorderParams.SourceHeight), CopyPixelOperation.SourceCopy);

This code is run in a separate thread:

   captureFrameThread = new Thread(TakeScreenshot)
            {
                IsBackground = false
            };
   captureFrameThread.Start();

and the full methods looks like this:

    public byte[] TakeScreenshot()
    {
        byte[] buffer = new byte[recorderParams.SourceWidth * recorderParams.SourceHeight * 4];
        HooksSetup.AppendToFile("Taken screenshot 1");

        using (var bmp = new Bitmap(recorderParams.SourceWidth, recorderParams.SourceHeight))
        {
            using (var g = Graphics.FromImage(bmp))
            {
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                // it gets stuck on a call below
                g.CopyFromScreen(Point.Empty, Point.Empty, new Size(recorderParams.SourceWidth, recorderParams.SourceHeight), CopyPixelOperation.SourceCopy);
                g.Flush();
                var bits = bmp.LockBits(new Rectangle(0, 0, recorderParams.SourceWidth, recorderParams.SourceHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
               Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);

                bmp.UnlockBits(bits);
            }
        }            
        return buffer;
    }

On my local machine this code always works flawlessly.

Why is it so? What is my alternative?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • Which [azure agent](https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops#use-a-microsoft-hosted-agent) are you using? – Adam May 13 '19 at 11:34
  • 1
    Your code is probably running in an application that has no "screen": https://stackoverflow.com/questions/18870987/screen-capture-using-windows-service#comment27848757_18870987 – Simon Mourier May 15 '19 at 13:23

3 Answers3

2

I think that the problem is related to the fact that your CopyFromScreen runs inside an agent without the same capabilities that you have in your desktop (log in, resolution, different browsers, Administration permissions, etc. ).

Microsoft says in his doc:

When running automated tests in the CI/CD pipeline, you may need a special configuration in order to run UI tests such as Selenium, Appium or Coded UI tests.

Considerations and configurations are explained in this article:

https://learn.microsoft.com/en-us/azure/devops/pipelines/test/ui-testing-considerations?view=azure-devops&tabs=mstest#visible-ui-mode

If you need to troubleshot your test check this article:

https://learn.microsoft.com/en-us/azure/devops/pipelines/test/ui-testing-considerations?view=azure-devops&tabs=mstest#visible-ui-mode

Roberto Borges
  • 683
  • 1
  • 5
  • 14
1

The only reason behind these problems was that agent didn't get enough time to login and Graphics.CopyFromScreen method was blocked. I had to defer taking screenshots and it all started working.

Yoda
  • 17,363
  • 67
  • 204
  • 344
0

You can refer the Azure DevOps labs here to execute UI tests as part of the pipelines.

Srivatsa Marichi
  • 649
  • 1
  • 5
  • 8