noam noam - 3 months ago 40
C# Question

Coded UI Mouse.Click() sometimes gets an error about int32

When I am trying to preform a mouse.Click() on a certain point inside a viewer I get the following error.

The code first:

public void ClickOnViewerSpace(int addX = 0, int addY = 0) {
//Mouse.Click(new Point(2439 + addX, 560 + addY));
int x = _contentContainer.BoundingRectangle.Location.X;
int y = _contentContainer.BoundingRectangle.Location.Y;
Mouse.Click(new Point(x + addX, y + addY));
}


The error:


Test Name: TC1_9_2_AdminEnter Test FullName: Test Source: line 427
Test Outcome: Failed Test Duration: 0:00:31.5364285

Result Message: Test method threw exception:
Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException:
Cannot perform 'Click' on the control. Additional Details: The control
details were not specified. --->
System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has
been returned from a call to a COM component. Result StackTrace: at
Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IScreenElement.MouseButtonClick(Int32
x, Int32 y, Int32 nButton, Int32 fEnsureVisible, String
bstrKeyModifiers) at
Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.MouseButtonClick(Int32
x, Int32 y, MouseButtons button, ModifierKeys modifierKeys, Int32
ensureVisible) at
Microsoft.VisualStudio.TestTools.UITesting.UITestActionExecutorCore.Click(UITestControl
control, MouseButtons button, ModifierKeys modifierKeys, Point
relativeCoordinate) --- End of inner exception stack trace ---
at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException
innerException, IPlaybackContext context) at
Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException
exception, IPlaybackContext context) at
Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException
exception, String actionName, UITestControl uiControl) at
Microsoft.VisualStudio.TestTools.UITesting.UITestActionExecutorCore.Click(UITestControl
control, MouseButtons button, ModifierKeys modifierKeys, Point
relativeCoordinate) at
Microsoft.VisualStudio.TestTools.UITesting.Mouse.ClickImplementation(UITestControl
control, MouseButtons button, ModifierKeys modifierKeys, Point
relativeCoordinate) at
Microsoft.VisualStudio.TestTools.UITesting.Mouse.<>c__DisplayClass6.b__5()
at
Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod[T](Func`1
function, UITestControl control, Boolean firePlaybackErrorEvent,
Boolean logAsAction) at
Microsoft.VisualStudio.TestTools.UITesting.Mouse.Click(UITestControl
control, MouseButtons button, ModifierKeys modifierKeys, Point
relativeCoordinate) at
Microsoft.VisualStudio.TestTools.UITesting.Mouse.Click(Point
screenCoordinate) at
ObjectsRepository.TouchViewer.ClickOnViewerSpace(Int32 addX, Int32
addY) in d:\test\auto\ObjectsRepository\TouchViewer.cs:line 109

Answer

It seems that even though the Mouse.Click(Point) method exists, the CUIT framework does not allow a click action without a UITestControl. Here is a good link to follow: Mouse.Click using Point only

The stack trace looks like it aligns with this since every method after the initial Mouse.Click(Point) call depends on a UITestControl.

Based on the code posted, I would try something like this:

public void ClickOnViewerSpace(int addX = 0, int addY = 0)
{
    //int x = _contentContainer.BoundingRectangle.Location.X;
    //int y = _contentContainer.BoundingRectangle.Location.Y;
    //Mouse.Click(new Point(x + addX, y + addY));
    Mouse.Click(_contentContainer, new Point(addX, addY)); // relative coords
}
Comments