简体   繁体   English

WPF编码的UI测试随机失败,出现奇怪的ArgumentException

[英]WPF coded UI test randomly fails with weird ArgumentException

I have coded UI tests in WPF application and they occasionally fail with the following exception: 我已经在WPF应用程序中对UI测试进行了编码,但有时会因以下异常而失败:

Message: Test method MyMethodNameGoesHere threw exception: System.ArgumentException: Parameter is not valid. 消息:测试方法MyMethodNameGoesHere引发了异常:System.ArgumentException:参数无效。

and StackTrace: 和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

Any code that tries to access properties of UITestControl like Exists , Checked or Selected throws it. 任何试图访问UITestControl属性(例如ExistsCheckedSelected将其抛出。 For example: 例如:

WpfButton button = UIControlBuilder<WpfButton>.Builder()
    .Parent(MainPane)
    .AutomationID(MyButtonId)
    .Build();

// ...    

Assert.IsTrue(button.Exists);

Interestingly, this issue happens randomly, but with some pattern. 有趣的是,此问题是随机发生的,但有一定规律。 If I run tests one by one, then they work well. 如果我一个接一个地运行测试,那么它们会很好地工作。 If I run many tests in row, then after some time one test fails and then all consequent tests fail too. 如果我连续运行许多测试,则一段时间后一个测试将失败,然后所有后续测试也将失败。 I thought that it could be a memory-related issue (leakage or out of memory), but I definitely have lots of memory available and processes don't seem to consume much of it. 我以为这可能是与内存相关的问题(内存泄漏或内存不足),但是我确实有很多可用的内存,进程似乎并没有消耗太多内存。

From my investigation UI test framework catches an exception in FindFirstDescendant and tries to capture a screenshot to report this exception, but fails to do this too: 从我的调查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;
    }
}

Even more interesting that this is a very generic error which happens in the constructor of .NET's Bitmap (invalid width or height parameter?). 更有趣的是,这是一个非常通用的错误,它发生在.NET的Bitmap的构造函数中(无效的width或height参数?)。

Because of this Bitmap issue I can't get the original exception, so I can't understand what happens. 由于存在此Bitmap问题,因此我无法获得原始异常,因此无法理解会发生什么。 Playback.CaptureScreenShot is marked with compiler warning suppression attribute, which clearly specifies that this method shouldn't throw any exceptions. Playback.CaptureScreenShot带有编译器警告抑制属性标记,该属性明确指定此方法不应引发任何异常。
Is this a bug in UI testing framework? 这是UI测试框架中的错误吗?

One more interesting observation. 另一项有趣的观察。 It creates screenshots in my test directory, but the application's window is highlighted incorrectly on screenshots. 它在我的测试目录中创建了屏幕截图,但是该应用程序的窗口在屏幕截图上未正确突出显示。 This is how it looks like. 这就是它的样子。 Red is my display setup, blue is where the main window actually is, green is how WPF highlights on test result screenshot: 红色是我的显示设置,蓝色是主窗口实际所在的位置,绿色是WPF在测试结果屏幕截图上的突出显示方式:

屏幕截图看起来如何

The distance of green narrow line and blue narrow line for corners are equal, so for me it seems like like it takes the application's window's offset within single main display, but then it tries to take a screenshot by applying the offset to all displays. 拐角处的绿色细线和蓝色细线的距离相等,因此对我来说,好像它占据了单个主显示器中应用程序窗口的偏移量,但随后尝试通过将偏移量应用于所有显示器来截取屏幕截图。 Can it be a cause of a bug (like going out of display's bounds) or is it just another thing? 可能是导致错误的原因(例如超出显示范围)还是仅仅是另一回事?

Since it looked like a bug, I have asked the same question on DeveloperCommunity . 由于它看起来像个错误,所以我在DeveloperCommunity上了同样的问题

It seems like the multi-screen is just unsupported: 似乎不支持多屏幕:

Thanks for investigation on this issue. 感谢您对此问题的调查。 looks like you are running coded ui test with multi screen. 看起来您正在多屏幕上运行已编码的ui测试。 This is actually an unsupported scenario. 这实际上是不受支持的方案。

Also Coded UI test is in deprecation path. 另外,已编码UI测试也在弃用路径中。 Check https://docs.microsoft.com/en-us/visualstudio/test/use-ui-automation-to-test-your-code?view=vs-2017 检查https://docs.microsoft.com/zh-cn/visualstudio/test/use-ui-automation-to-test-your-code?view=vs-2017

We suggest moving to Selenium or Appium with WinAppDriver as the case may be 我们建议视情况而定使用WinAppDriver迁移到Selenium或Appium

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM