Skip to content

In the first article, we have installed and set up Visual Studio Enterprise 2019 for automated test development using Xamarin.UITest. We have created a simple cross-platform app using Xamarin.Forms and a simple automated UI test scenario that could interact with the elements in this app. However, to write more complicated Xamarin.UITest test scenarios, we need to find and use its locators.

This is the second blog of our Xamarin.UITest 101 series. In this blog, we will go through a couple of different types of Xamarin.UITest locators and use them to get access to the layout of the elements so that we can work with them.

The Dot Operator

Xamarin.UITest has a nice feature – the dot operator, which brings us a lot of locators that can be used to interact with the app elements.

The below screenshot briefly shows what the dot operator can do for us. And we’ll be using some of the Xamarin.UITest locators later.

dot operator in xamarin.uitest finding locators

Working with the Xamarin REPL Tool

Before using the functions and locators under the dot operator, we need to find a particular element in the app layout. For that, we can use Xamarin REPL (read-eval-print-loop).

Xamarin REPL is one of the most important tools in creating Xamarin.UITest tests. It is a console-like environment in which the developer enters expressions or commands, and it will then evaluate those expressions, and display the results to the user. It allows us to explore the user interface and create the queries and statements so that the test may interact with the application.

To run the REPL tool we could add next line to our test method:

app.Repl();

adding app.repl to test method for xamarin.uitest

Now, let’s run our sample test created in the first blog.

run the sample test created in the first xamarin.uitest blog

Once the test is running, you will see a Xamarin.UITest REPL console window like below.

xamarin.uitest repl console window

Just like in Visual Studio, here we can enter a variable app and then enter the dot operator. Now we have a list of all possible Xamarin.UITest locators that are available for working with the app.

Dot Operator in Xamarin.UITest REPL

Get Started with Different Xamarin.UITest Locators

Now that we know the Xamarin.UITest REPL tool can help us work with the elements of a Xamarin.Forms app, let’s check the validity of the locators that we want to implement in Xamarin.UITest tests.

Tap

Let’s first create a new item by clicking on the ‘ADD’ button. To do so, we can use the Tap locator and call the following command in the console:

app.Tap(x => x.Text("Add"))

After you press Enter, there will be a click action on the ‘ADD’ button and a new item is created for further actions.

Add locators

The Tap locator also contains many ways of finding an element in the console by using the following command:

app.Tap(x => x.

app.Tap locators

To use all these access types, you need to know the structure of elements that are displayed on the screen. To see this structure, you can use the following command:

Tree

Tree

EnterText

To enter text in the identified element, we can use the following function:

EnterText

For example, let’s enter some text to the ‘Item name’ input field.

app.EnterText((x => x.Marked("item_name_Container").Descendant().Index(1)), "Test text")

EnterText locators xamarin.uitest

ClearText

Removing texts in the field is easy by using the following locator:

ClearText

So, to remove all the texts in the first field, we can use the following command:

app.ClearText(x => x.Marked("item_name_Container").Descendant().Index(1))

ClearText locators xamarin.uitest

Here, we have used the following command to find the path to the needed element.

x => x.Marked("item_name_Container").Descendant().Index(1)

Query

To find the above element, we can use this locator:

Query

Query

Descendant()

As you can see, this Xamarin.UITest locator returns the label element of the needed text field. The text field is a descendant of the label element.

We can use the following command with the Descendant() function to get all the descendants of the label element.

app.Query(x => x.Marked("item_name_Container").Descendant())

Descendant()

This method returns a list of two elements

  • The [0] element. It is a parent element of the descendants that we’re searching for.
  • The [1] element. It is the text field that we need for entering the Item name.

Index()

If you want to get access to some element in the list, you can use the .Index() function, for example:

x => x.Marked("item_name_Container").Descendant().Index(1)

Index()

Child()

Similar to the Descendant() locator, you can also use the Child() method to find the descendant/child elements.

app.Query(x => x.Marked("item_name_Container").Child().Index(0))

Child()

Now, with this method, we can find all the elements we need for the scenario under the test.

Using Different Locators to Create a Sample Xamarin.UITest Test

Now let’s create a test that creates a new item and checks if this item is in the item list.

[Test]
public void FirstTest()
{
  // Generating random item name and description using tempale:
  // item_name + timestamp
  // item_description + timestamp
  String itemName = "item_name" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
  String itemDescription = "item_description" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();

  //Waiting 'ADD' button
  app.WaitForElement(x => x.Marked("Add"));

  // Tap 'ADD' button
  app.Tap(x => x.Marked("Add"));

  //Waiting 'Item name' text field
  app.WaitForElement(x => x.Marked("item_name_Container").Descendant().Index(1));

  //Clear text from 'Item name' text field
  app.ClearText(x => x.Marked("item_name_Container").Descendant().Index(1));

  //Set text to 'Item name' text field
  app.EnterText(x => x.Marked("item_name_Container").Descendant().Index(1), itemName);

  //Waiting 'Item name' text field
  app.WaitForElement(x => x.Marked("item_description_Container").Descendant().Index(1));

  //Clear text from 'Item name' text field
  app.ClearText(x => x.Marked("item_description_Container").Descendant().Index(1));

  //Set text to 'Item name' text field
  app.EnterText(x => x.Marked("item_description_Container").Descendant().Index(1), itemDescription);

  //Waiting 'Save' button
  app.WaitForElement(x => x.Marked("save_button"));

  //Tap 'Save' button
  app.Tap(x => x.Marked("save_button"));

  //Verifying item with generated itemName is present
  Assert.IsTrue(app.Query(x => x.Text(itemName)).Any());

  //Verifying item with generated itemDescription is present
  Assert.IsTrue(app.Query(x => x.Text(itemDescription)).Any());
}

After the test run, the new item will be created in the item list.

a new item created xamarin.uitest

Conclusion

In this article, we used the Xamarin REPL tool and many locators as a more advanced way of finding elements in our Xamarin.Forms app. In the next article, we will improve the structure of our test project, using best practices for creating Xamarin.UITest tests. Stay tuned!

Petro Kiryshikin

Tech Writer