Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.UITest: How To Pause a Test Step

I have a UITest written in C#/Xamarin and the test is executing too quickly, causing it to fail. I need the test to wait for an Image to appear on the screen before executing the next step.

How can I pause the Xamarin UITest and make it wait for an Image to load on the screen before continuing to the next step?

like image 872
Brandon Minnick Avatar asked Dec 08 '16 22:12

Brandon Minnick


3 Answers

In my experience, there are a couple of ways to pause or slow down Xamarin.UITest.

Best Way

Use the WaitForElement API.

For example, here I am waiting for a Button called "imageButton".

app.WaitForElement(x => x.Marked("imageButton"));

The example above waits for the element "imageButton" to appear before executing another step.

Another Way

In the case you don't know what element you want to interact with or you simply want to pause Xamarin.UITest, you can use Thread.Sleep:

Thread.Sleep(8000);

Here, I'm pausing Xamarin.UITest for 8 seconds. It is important to note that you will need to add this library to use Thread.Sleep:

using System.Threading;

Hope this helps!

like image 74
June Avatar answered Oct 24 '22 23:10

June


The problem with "pausing" UITest

You asked:

"How can I pause the Xamarin.UITest and make it wait for an Image to load on the screen before continuing to the next step?"

It is possible to pause Xamarin.UITest using something like Thread.Sleep() for whatever length of time you require. However, the problem with this is that Thread.Sleep () freezes the test run inflexibly. For example, if you set Thread.Sleep (10000), then the thread would pause for exactly 10 seconds, regardless of how long your app actually takes to load the desired element.

If your picture only takes 1 second to load on faster devices, then using Thread.Sleep(10000) your test will still always take at least 10 seconds. What's worse is if you use this approach and don't Thread.Sleep long enough, the test will still fail; so you have to force your tests to run as slow as your "worst case scenario" every single time. More info: Thread.Sleep documentation

Solution by waiting for a UI element without "pausing" execution

Xamarin.UITest has the IApp.WaitForElement & IApp.WaitForNoElement APIs to handle these scenarios. These APIs are superior to Thread.Sleep because you can customize the condition at which it resumes beyond just waiting for a set amount of time.

Example

This snippet example will wait 90 seconds for an element to appear that is either labeled or has an ID as "myButton."

    // Wait up to 90 seconds for myButton to appear             
    app.WaitForElement(c=>c.Marked("myButton"), "Could not find myButton", new TimeSpan(0,0,0,90,0));

If the element takes only 30 seconds to load, than in this example app.WaitForElement will continue after 30 seconds, it will only throw the error "Could not find myButton" if after waiting the full 90 seconds the element is still hasn't appeared.

Simple query using default timeouts & error messages

You can also use either of these API calls without defining a timeout length, in which case for local UITests they will timeout after 15 seconds, or if run in Xamarin Test Cloud the will timeout after 1 minute. Source: Xamarin.UITest "Timeouts & Waiting" guide

This is the simplest possible form of the above query, using the default timeouts & default error message "Timed out waiting for element...":

    app.WaitForElement (x => x.Marked ("myButton"));
like image 8
user62171 Avatar answered Oct 25 '22 00:10

user62171


This solution works if you aren't waiting for a particular UI element and don't want to sleep the current thread:

public static void WaitForTimeSpan(IApp app, TimeSpan timeSpan)
{
    try
    {
        app.WaitForElement(c => c.Text("BLAH BLAH BLAH"), "Waiting for something, killing time", timeSpan);
    }
    catch (Exception)
    {
        //do nothing we just wanted to kill some time
    }
}
like image 5
Jared Avatar answered Oct 25 '22 00:10

Jared