WPF coded UI test randomly fails with weird ArgumentException

148 Views Asked by At

I have coded UI tests in WPF application and they occasionally fail with the following exception:

Message: Test method MyMethodNameGoesHere threw exception: System.ArgumentException: Parameter is not valid.

and 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. 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:

// 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?).

Because of this Bitmap issue I can't get the original exception, so I can't understand what happens. Playback.CaptureScreenShot is marked with compiler warning suppression attribute, which clearly specifies that this method shouldn't throw any exceptions.
Is this a bug in UI testing framework?

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:

How screenshot looks like

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?

1

There are 1 best solutions below

0
Yeldar Kurmangaliyev On BEST ANSWER

Since it looked like a bug, I have asked the same question on 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. This is actually an unsupported scenario.

Also Coded UI test is in deprecation path. Check https://learn.microsoft.com/en-us/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