由Azure代理执行时,CopyFromScreen方法永远不会完成

时间:2019-05-10 15:44:29

标签: c# .net azure azure-devops

我正在通过Azure代理执行UI测试期间截取屏幕截图。 由于某种原因,该行永远不会完成,没有错误,也没有例外,它会无限期地等待它:

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

此代码在单独的线程中运行:

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

完整的方法如下:

    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 stucked 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;
    }

在我的本地计算机上,此代码始终可以完美运行。 为什么会这样呢?我有什么选择?

3 个答案:

答案 0 :(得分:2)

我认为该问题与以下事实有关:CopyFromScreen在代理程序内部运行时,其功能与您的台式机相同(登录,解析,不同的浏览器,管理权限等)。

Microsoft在他的文档中说:

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.

在本文中说明了注意事项和配置:

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

如果您需要对测试进行麻烦检查,请查看本文:

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

答案 1 :(得分:1)

这些问题背后的唯一原因是代理没有足够的时间登录并且Graphics.CopyFromScreen方法被阻止。我不得不推迟截屏,一切都开始起作用。

答案 2 :(得分:0)

您可以在此处引用Azure DevOps labs来执行UI测试,并将其作为管道的一部分。