Xamarin Test Cloud is now part of Visual Studio App Center! Get Started.

Timeouts & Waiting

PDF for offline use
Related APIs:

Let us know how you feel about this

Translation Quality


last updated: 2017-01

Explains how to pause test execution while waiting for views to appear on screen.

It may be necessary for a test to pause execution to allow the user interface to update or for a long running action to complete. UITest provides two API's to address these concerns:

  • IApp.WaitForElement
  • IApp.WaitForNoElement

The methods IApp.WaitForElement, and IApp.WaitForNoElement fundamentally work the same: they will wait for a query to be satisfied and will throw a TimeoutException if it is not satisfied in the specified time limit.

By default, UITests waits 15 seconds for tests running locally and one minute for tests running in Test Cloud before throwing the exception.

IApp.WaitForElement is useful for situations where the test needs to confirm the presence of a view that provides important information to the user, such as a success message or perhaps an icon. This is an example of IApp.WaitForElement that will wait 90 seconds for a view that is marked with success_message_label to appear on the screen:

app.WaitForElement(c=>c.Marked("success_message_label"), "Did not see the success message.", 
    new TimeSpan(0,0,0,90,0));

In particular, it is a good idea to call IApp.WaitForElement before interacting with views. Many of the gesture API's will fail if the view is not visible or if animating. The following snippet is one such example of an extension method that will first verify the visibility of a view and then try to enter text:

public static class UITestHelpers
    public static void WaitThenEnterText(this IApp app, Func<AppQuery, AppQuery> lambda, string text)
        app.EnterText(lambda, text);

In contrast, the method IApp.WaitForNoElement is useful to pause the test while some view, such as a progress dialog, is still on the screen:

app.WaitForNoElement(c=>c.Class("TextView").Text("Uploading data..."), 
    "Upload is taking too long", new TimeSpan(0,0,90,0));

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.