The Xamarin.UITest framework

According to the Xamarin website (https://developer.xamarin.com/guides/testcloud/uitest/), Xamarin.UITest is an Automated UI Acceptance Testing framework that allows programmers to write and execute tests in C# and NUnit that validate the functionality of an application.

That means we can write test methods using C# to test the functionality of an Xamarin application. For example, we can test these and many more scenarios:

  • Launch the application, wait for the landing page, click on the SignUp button, enter Username, Password, Full Name, and so on, in the relevant entry fields, click on the Create button, and check that the browser naviagates to the Home page
  • Launch the application, enter Mouse into the search field, click on the Find button, wait for the result list to be filled, and check if there are 25 items in the list
  • Launch the application, swipe five times to the right, wait for the image to load, click the Buy button, and check if the You have to login first message appears

Xamarin.UITest uses the Xamarin Test Cloud Agents. Xamarin Test Cloud Agent is an HTTP server that will communicate with the mobile application being tested.

Unfortunately, we can only test Android and iOS applications with the Xamarin.UITest framework. It is not possible to test Windows Phone applications at the time of writing.

Creating a sample UITest project

Let's open Xamarin Studio and create a sample UITest project:

Creating a sample UITest project

  1. Select Multiplatform | App from the list on the left-hand side, and Xamarin.Forms | Forms App from the list on the right-hand side.
  2. Click on the Next button.
  3. Type UITestSample into the App Name field.
  4. Click on the Next button.
  5. Check Add an automated UI test project in the Xamarin Test Cloud group.
  6. Click on the Create button:

Creating a sample UITest project

The only difference is that the UITestSample.UITests project was created with solution.

Let's investigate the Test.cs file inside the UITestSample.UITests project by double-clicking on it.

We can see that the Tests class is annotated with a [TestFixture] attribute.

The TestFixture attribute turns the Tests class into a test methods container class.

When testing starts, the TestFixture annotated classes begin to run.

First of all, a method with the [SetUp] attribute annotation is executed. This method handles starting test logic.

Then all the methods with the [Test] attribute annotation are executed.

We should only write the [Test] attribute annotated methods. These methods basically do whatever the client (the application's end user) does with our application by automation:

    [TestFixture] 
    public class Tests 
    { 
        AndroidApp app; 
 
        [SetUp] 
        public void BeforeEachTest() 
        { 
            app = ConfigureApp.Android.StartApp(); 
        } 
 
        [Test] 
        public void WelcomeTextIsDisplayed() 
        { 
            AppResult[] results = app.WaitForElement(c => c.Marked("Welcome to Xamarin Forms!")); 
            app.Screenshot("Welcome screen."); 
 
            Assert.IsTrue(results.Any()); 
        } 
    } 

We can easily see that only the test is waiting for the Welcome to Xamarin Forms! label on the screen.

The TextFixture attribute changes classes to test classes. All the methods with a Test attribute listed in the Unit Test Panel can be run to test.

We created a test method to test if the label contains the Welcome to Xamarin Forms! text.

The WaitForElement method iteratively queries the parameter to find the matching element. If there is no element that can be found in a timely fashion, TimeoutException will be thrown.

Some of the other methods are listed here:

  • ClearText
  • EnterText
  • Tap
  • DoubleTap
  • TouchAndHold
  • PinchToZoomIn
  • PinchToZoomOut
  • PressVolumeDown
  • PressVolumeUp
  • Screenshot
  • ScrollDown
  • ScrollTo
  • ScrollUp
  • SetOrientationPortrait
  • SetOrientationLandscape
  • SwipeRight
  • SwipeLeft

We may use these methods to create any test scenario we want using C#. If we use the WaitForElement method, its first parameter is type of AppQuery. The AppQuery class lets us write a query and returns matched view elements.

Some of the methods we may use with AppQuery are listed here:

  • All
  • Button
  • Id
  • Index
  • Child
  • Parent
  • TexField
  • Marked

These methods take parameters and find the matched view elements.

For example, the All() method returns all view elements in a page.

Another example, the Marked() method, takes a parameter and finds its value in all view elements' ID or text fields in page and returns an element if found.

Tip

There is a platform difference here, the Marked() method in Android finds an ID, contentDescription, or text fields, in iOS an accessibilityLabel or accessibilityIdentifier fields.

Assert calls are for what we want to test.

We should open the Unit Tests panel by clicking View | Unit Testing in the Xamarin Studio IDE:

Creating a sample UITest project

We can run individual tests by right-clicking and clicking Run on the menu, or we can run all tests by clicking the Run All button.

When tests are completed to run they should be green. If there is a red test we should check what is wrong and fix it to make the test green again:

Creating a sample UITest project

We can easily use the following methods to traverse and interact with the application's UI surface:

  • Button
  • Id
  • Index
  • Text
  • TextField
  • Class
  • Marked
  • Tap
  • Parent
  • Query
  • PressEnter
  • EnterText
  • WaitForElement
  • Screenshot

With these methods we can easily write thousands of tests to test the application's UI and execute them within seconds.

Tip

For example, if we need to test a page with an e-mail address entry box, we may use the following code block: app.WaitForElement(c => c.Marked("lblEmail").Text("Enter Email")); app.EnterText(c=>c.Marked("txtEmail"), "test@test"); app.Tap(c => c.Marked("btnSend")); app.WaitForElement(c => c.Marked("lblError").Text("Entered email address is invalid"));

The following list is the test flow:  Running test wait for an element appear on the screen, named lblEmail, have  Enter Email Running test enter test@test to txtEmail named element Running test click on the btnSend named element Running test wait for the Entered email address is invalid text appears in the  lblError named element

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.105