C# WPF编码的UI测试随机失败,出现异常

C# WPF编码的UI测试随机失败,出现异常,c#,wpf,exception,coded-ui-tests,base-class-library,C#,Wpf,Exception,Coded Ui Tests,Base Class Library,我在WPF应用程序中编写了UI测试,它们偶尔会失败,但有以下例外: 消息:测试方法MyMethodNameGoesher引发异常: System.ArgumentException:参数无效 和StackTrace: at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) at System.Drawing.Bitmap..ctor(Int32 width, Int32 height) at Mi

我在WPF应用程序中编写了UI测试,它们偶尔会失败,但有以下例外:

消息:测试方法MyMethodNameGoesher引发异常: System.ArgumentException:参数无效

和StackTrace:

at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at Microsoft.VisualStudio.TestTools.UITesting.LoggerUtility.GetDesktopImage()
at Microsoft.VisualStudio.TestTools.UITesting.LoggerUtility.CaptureScreenShotAndDrawBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 borderWidth, Boolean isActualControlBounds)
at Microsoft.VisualStudio.TestTools.UITest.Extension.LoggerUtilities.CaptureScreenShotAndDrawBounds(Rectangle bounds, Int32 borderWidth, Boolean isActualControlBounds)
at Microsoft.VisualStudio.TestTools.UITesting.Playback.CaptureScreenShot(UITestControl control)
at Microsoft.VisualStudio.TestTools.UITesting.Playback.GetUITestControlString(UITestControl control)
at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapControlNotFoundException(COMException ex, IPlaybackContext context)
at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException innerException, IPlaybackContext context)
at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, IPlaybackContext context)
at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, String queryId)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Int32 maxDepth, Int32& timeLeft)
at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache, ISearchArgument searchArg)
at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.<Find>b__175_0()
at Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod[T](Func`1 function, UITestControl control, Boolean firePlaybackErrorEvent, Boolean logAsAction)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.Find()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyPrivate(String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyOfType[T](String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_Exists()
at MyProjectNamespace.UITests.TestButtonExists() in E:\...\UITests.cs:line 177
有趣的是,这个问题是随机发生的,但有一些模式。如果我一个接一个地运行测试,那么它们工作得很好。若我在行中运行多个测试,那个么在一段时间后,一个测试失败,然后所有后续测试也失败。我认为这可能是一个与内存相关的问题(泄漏或内存不足),但我肯定有很多可用内存,进程似乎不会消耗太多内存

从我的调查中,UI测试框架在
FindFirstDescendant
中捕获异常,并尝试捕获屏幕截图以报告此异常,但也失败了:

// decompiled UI test framework's UITestControl.cs code
internal UITestControl FindFirstDescendant(string queryId, int maxDepth, ref int timeLeft)
{
    // ...
    try
    {
        element = this.ScreenElement.FindScreenElement(queryId, maxDepth);
    }
    catch (Exception ex) // catches exception here
    {
        // but this method does also throw exception, because of a bug:
        Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(ex, queryId); 
        throw;
    }
}
更有趣的是,这是一个非常普遍的错误,发生在.NET的
位图的构造函数中(宽度或高度参数无效?)

由于这个
位图
问题,我无法获得原始异常,因此我无法理解发生了什么
Playback.CaptureScreenShot
带有编译器警告抑制属性,该属性明确指定此方法不应引发任何异常。
这是UI测试框架中的一个bug吗

还有一个有趣的观察。它在我的测试目录中创建了屏幕截图,但是应用程序的窗口在屏幕截图上被错误地突出显示。这就是它的样子。红色是我的显示设置,蓝色是主窗口的实际位置,绿色是WPF在测试结果屏幕截图上突出显示的方式:

角的绿色窄线和蓝色窄线的距离是相等的,因此对我来说,它似乎在单个主显示器内获取应用程序窗口的偏移,但它试图通过将偏移应用于所有显示器来获取屏幕截图。
这可能是一个bug的原因(比如超出显示范围),还是仅仅是另一回事?

因为它看起来像个bug,我已经问过了

似乎不支持多屏幕:

感谢您对这个问题的调查。看起来您正在使用多屏幕运行编码的ui测试。这实际上是一个不受支持的场景

编码的UI测试也在弃用路径中。检查

我们建议根据具体情况使用WinAppDriver迁移到Selenium或Appium


因为它看起来像一只虫子,我已经问过了

似乎不支持多屏幕:

感谢您对这个问题的调查。看起来您正在使用多屏幕运行编码的ui测试。这实际上是一个不受支持的场景

编码的UI测试也在弃用路径中。检查

我们建议根据具体情况使用WinAppDriver迁移到Selenium或Appium

// decompiled UI test framework's UITestControl.cs code
internal UITestControl FindFirstDescendant(string queryId, int maxDepth, ref int timeLeft)
{
    // ...
    try
    {
        element = this.ScreenElement.FindScreenElement(queryId, maxDepth);
    }
    catch (Exception ex) // catches exception here
    {
        // but this method does also throw exception, because of a bug:
        Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(ex, queryId); 
        throw;
    }
}