Xamarin UI test cannot tap on Button

3.9k Views Asked by At

I detected very strange behavior on Xamarin UI test. The issue is - frameworkcan not tap on some marked elements. This absolutely random problem. Issue may appear on Physical Device or Simulator, Android or iOS. Old or new OS Systems. My Test Scenario :

app.WaitForElement(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));
try
        {
            app.WaitForElement(x => x.Marked("Filter"));
            app.Tap(x => x.Marked("Filter"));
        }
        catch (Exception)
        {
            try
            {
                app.Tap(x => x.Marked("Button.Home"));
                app.Tap(x => x.Marked("IDTransHistory"));
            }
            catch (Exception)
            {
                try
                {
                    app.Tap(x => x.Marked("Button.Home"));
                    app.Tap(x => x.Marked("IDTransHistory"));
                }
                catch (Exception)
                {
                    app.Tap(x => x.Marked("Button.Home"));
                    app.Tap(x => x.Marked("IDTransHistory"));
                }
            }
        }

VS Log :

[5/18/2018 12:57:16 PM Informational] ------ Run test started ------
[5/18/2018 12:57:16 PM Informational] NUnit VS Adapter 2.0.0.0 executing tests is started
...
[5/18/2018 12:57:56 PM Informational] Waiting for element matching Marked("IDTransHistory").
[5/18/2018 12:58:03 PM Informational] Using element matching Marked("IDTransHistory").
[5/18/2018 12:58:04 PM Informational] Tapping coordinates [ 532, 1114 ].
[5/18/2018 12:58:05 PM Informational] Waiting for element matching Marked("Filter").
[5/18/2018 12:58:08 PM Informational] Using element matching Marked("Filter").
[5/18/2018 12:58:08 PM Informational] Tapping coordinates [ 684, 96 ].
[5/18/2018 12:58:24 PM Informational] Error while performing Tap(Marked("Date.Start"))
Exception: System.Exception: Unable to find element. Query for Marked("Date.Start") gave no results.
   at Xamarin.UITest.SharedApp.FirstWithLog[T](T[] results, ITokenContainer tokenContainer)
   at Xamarin.UITest.Android.AndroidApp.<Tap>c__AnonStoreyB.<>m__0()
   at Xamarin.UITest.Utils.ErrorReporting.With(Action func, Object[] args, String memberName)
[5/18/2018 12:58:24 PM Informational] Waiting for element matching Marked("Filter").
[5/18/2018 12:58:28 PM Informational] Using element matching Marked("Filter").
[5/18/2018 12:58:28 PM Informational] Tapping coordinates [ 684, 96 ].
[5/18/2018 12:58:32 PM Informational] Using element matching Marked("Date.Start").
[5/18/2018 12:58:32 PM Informational] Tapping coordinates [ 71, 298 ].
[5/18/2018 12:58:35 PM Informational] Using element matching Text("2018").
[5/18/2018 12:58:35 PM Informational] Tapping coordinates [ 139, 228 ].

Ok, as you can see the element marked Filter was using -> Tapping coordinates [ 684, 96 ]. That's mean that element is exist on monitor. But first time this not worked. I use Nunit 3.10 and Xamarin UI Test 2.24. This issue can arises on random Device - iOS or Android, Emulator or Physical. Current test was on Samsung sm a310f. I don't know is this bug of Xamarin UI test or may be Calabash.

UPD :

Code -

app.ScrollDownTo(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));
app.WaitForElement(x => x.Marked("Filter"));
app.Tap(x => x.Marked("Filter"));
app.WaitForElement(x=>x.Marked("Date.Start"));
app.Tap(x => x.Marked("Date.Start"));

Log -

[5/21/2018 3:05:00 PM Informational] Skipping installation: Already installed.
----
[5/21/2018 3:05:34 PM Informational] Scrolling down to Marked("IDTransHistory")
[5/21/2018 3:05:37 PM Informational] Using element matching Marked("IDTransHistory").
[5/21/2018 3:05:37 PM Informational] Tapping coordinates [ 533, 1114 ].
[5/21/2018 3:05:38 PM Informational] Waiting for element matching Marked("Filter").
[5/21/2018 3:05:40 PM Informational] Using element matching Marked("Filter").
[5/21/2018 3:05:40 PM Informational] Tapping coordinates [ 684, 96 ].
[5/21/2018 3:05:41 PM Informational] Waiting for element matching Marked("Date.Start").
[5/21/2018 3:05:56 PM Informational] Error while performing WaitForElement(Marked("Date.Start"), "Timed out waiting for element...", null, null, null)
Exception: System.TimeoutException: Timed out waiting for element...
   at Xamarin.UITest.Shared.WaitForHelper.WaitForAny[T](Func`1 query, String timeoutMessage, Nullable`1 timeout, Nullable`1 retryFrequency, Nullable`1 postTimeout)
   at Xamarin.UITest.Android.AndroidApp.<WaitForElement>c__AnonStorey31.<>m__0()
   at Xamarin.UITest.Utils.ErrorReporting.With[T](Func`1 func, Object[] args, String memberName)
[5/21/2018 3:05:57 PM Informational] Query for * gave 46 results.
[5/21/2018 3:05:57 PM Informational] [
  {
4

There are 4 best solutions below

0
On

Run app.Repl(); and enter tree . You will find the list of UI visible code try there once with your code. If it is working its fine or else use Class and the class name instead of the Marked with the text on the UI. For instance:- app.Tap( a => a.Class("classs-name")); // if you have same class name used use index for that as app.Tap( a => a.Class("classs-name").index(value));

0
On

I faced this issue when using ipod touch simulator, when i changed to iPhone simulator it is working as expected

11
On

It's normal practice with UITest tests to wait for some defined state after you perform a gesture - so I would advise adding a wait command after each tap.

For example I would code your example as:

app.WaitForElement(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));
app.WaitForElement(x => x.Marked("Button.Home"));
app.Tap(x => x.Marked("Button.Home"));
app.WaitForElement(x => x.Marked("IDTransHistory"));
app.Tap(x => x.Marked("IDTransHistory"));

This way, you're waiting for the element to appear on screen before attempting to press it (with certain devices this is more important than others because screen transition animations can take some time) - also it may be that the element flags up as having co-ordinates when it's not yet ready to receive gestures - so if the above doesn't work you could try adding a Thread.Sleep before the Tap to give the UI time to be ready - this may help stabilise your test.

If that becomes necessary - you'll end up with test code something like this:

app.WaitForElement(x => x.Marked("IDTransHistory"));
Thread.Sleep(500);
app.Tap(x => x.Marked("IDTransHistory"));
app.WaitForElement(x => x.Marked("Button.Home"));
Thread.Sleep(500);
app.Tap(x => x.Marked("Button.Home"));
app.WaitForElement(x => x.Marked("IDTransHistory"));
Thread.Sleep(500);
app.Tap(x => x.Marked("IDTransHistory"));

I would advise starting with a Sleep of 500, raising it in increments of 500 until the test becomes stable.

If all else fails - I would investigate the issue using Repl - it may be that something is blocking the gesture.

It's also worth noting (although based on your description this isn't your issue) that in iOS 11 - Apple made some changes to the way elements are (or are not) Marked so some previously working tests may stop working because the UI is no longer recognising them as Marked in the same way. The best way to work this out is again to interrogate the app using Repl.

0
On

From documentation: https://learn.microsoft.com/en-us/appcenter/test-cloud/uitest/

Xamarin.UITest requires NUnit 2.6.3 or 2.6.4 to run tests. Xamarin.UITest is not compatible with NUnit 3.x.

That might be your problem.

I would also check if:

  1. Tapping on "Filter" - doesn't cause any layout changes(animations for example) -> pause might help in this case.

  2. Changed the element ID from "Date.Start" to something without dot(.), like "StartDate"