Writing Queries
Queries are the lingua franca of a UI test; they allow your tests to describe interface elements within an app, then interact with or make assertions about those elements.
Type Query.
to trigger code completion and start exploring how queries are constructed:
// Creating queries
var signInButton = Query.Button.WithId("signInButton");
var fifthCell = Query.Cell.Marked("DataRow").AtIndex(4);
var helloField = Query.Entry.WithText("Hello");
// Using queries
app.Tap(signInButton);
app.WaitFor(fifthCell);
app.Tap(helloField);
using static Xamarin.UITest.Helpers
brings base queries, from which you build more complex queries, into scope:
using static Xamarin.UITest.Helpers;
...
// Creating queries
var signInButton = Button.WithId("signInButton");
var fifthCell = Cell.Marked("DataRow").AtIndex(4);
var helloField = Entry.WithText("Hello");
If you've used Xamarin.UITest
before, Query
is a drop-in replacement for Func<AppQuery, AppQuery>
, the main type expected by methods like IApp.Tap
. Query
can be used anywhere a Func<AppQuery, AppQuery>
is expected.
Query Indexer Shorthands
Indexers on queries provide shorthands for Marked
, WithId
, AtIndex
, so our examples become even terser:
// Creating queries
var signInButton = Button["#signInButton"];
var fifthCell = Cell["DataRow"][4];
var helloField = Entry["~Hello"];
Marked
// These queries are equivalent:
var signInButton = Button.Marked("Sign In");
var signInButton = Button["Sign In"];
WithId
// These queries are equivalent:
var signInButton = Button.WithId("signInButton");
var signInButton = Button["#signInButton"];
WithClass
// These queries are equivalent:
var myView = Any.WithClass("MyCustomView");
var myView = Any[".MyCustomView"];
WithText
// These queries are equivalent:
var smithsCell = Cell.WithText("Mr. Smith");
var smithsCell = Cell["~Mr. Smith"];
AtIndex
// These queries are equivalent:
var fourthCell = Cell.AtIndex(3);
var fourthCell = Cell[3];
Query Methods
WithPlaceholder
Matches the hint (Android) or placeholder (iOS, Forms) of an entry:
Entry.WithPlaceholder("Email").EnterText("[email protected]");