Xamarin: how to add UITests in an application – Xamarin Diaries

Do you have a mobile application development in Xamarin and do you want to add UI tests (UITests) on it? Solid Gear teaches you how to do it.

Xamarin.UITest is a testing framework that allows tests written in NUnit to be run in iOS and Android applications. It is easily integrated into Xamarin.iOS and Xamarin.Android projects.

 

Add UITests project to the solution

 

The first thing we have to do is create a UI Test App type project in our solution. This is where we will add the user interface tests for our application, and these will be the ones that interact with the user interface of the app just as a user would: entering text, pressing buttons, handling gestures, etc.

 

 

Add new UITest project

 

Generally, each ITUest is written as a method, which will be a test. The class containing the test is known as a fixture test. The text fixture contains a single test or a logical grouping of tests and is responsible for executing the tests and cleaning at the end of the execution.

 

using NUnit.Framework;
using Xamarin.UITest;

namespace UITests
{
    [TestFixture(Platform.iOS)]
    public class LoginTests
    {
        IApp app;
        Platform platform;

        public LoginTests(Platform platform)
        {
            this.platform = platform;
        }

        [SetUp]
        public void BeforeEachTest()
        {
            app = AppInitializer.StartApp(platform);
        }

        [Test]
        public void Login()
        {
            //Login user
            app.Tap(x => x.Marked("usernameEntry"));
            app.EnterText(x => x.Marked("usernameEntry"), "test_user");
            app.EnterText(x => x.Marked("passwordEntry"), "aaaaa");
            app.Tap(x => x.Marked("loginButton"));

            app.Screenshot("Tapped Login");
        }

    }
}

 

How to add UITests for a login screen

 

Login view

 

The first thing we have to do is, in the code of the login view, add the AutomationId property (and give it a name) to the elements that we want to control over later in the tests. In our case, for the example we are going to use, we will add it to the user and password text fields and the button.

 

<?xml version="1.0" encoding="utf-8" ?>
<local:LoginPageXaml xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:i18n="clr-namespace:LoginProject;assembly=LoginProject"
             xmlns:local="clr-namespace:LoginProject;assembly=LoginProject"
             x:Class="LoginProject.LoginPage"
             Title="{i18n:Translate app_name}"
             Style="{DynamicResource pageStyle}">
  <local:LoginPageXaml.Resources>
    <ResourceDictionary>
      <local:InverseBooleanConverter x:Key="inverter"/>
    </ResourceDictionary>
  </local:LoginPageXaml.Resources>
  <local:LoginPageXaml.Content>
    <ScrollView>
      <StackLayout Orientation="Vertical">
        <StackLayout Orientation="Vertical" Padding="0,25,0,0" HeightRequest="40" BackgroundColor="{DynamicResource redColor}">
          <Label XAlign="Center" Text="{i18n:Translate app_name}" HorizontalOptions = "Center" VerticalOptions ="Center" FontSize="22" FontFamily="Roboto-Regular" TextColor="{DynamicResource whiteColor}"/>
        </StackLayout>
        <StackLayout Padding="25,30,25,0" Spacing="20" Orientation="Vertical">
            <Label XAlign="Center" Text="{i18n:Translate auth_login_text}" FontFamily="Roboto-Light" FontSize="23" TextColor="{DynamicResource darkGray}"/>
            <Entry x:Name="UsernameEntry"
                   AutomationId="usernameEntry"
                   Text="{Binding Username}"
                   Placeholder="{i18n:Translate auth_username}"
                   FontFamily="Roboto-Regular"
                   HeightRequest="45"
                   FontSize="18"
                   TextColor="{DynamicResource darkGray}"
                   HorizontalOptions="FillAndExpand"
                   VerticalOptions="Center" />
            <Entry x:Name="PasswordEntry"
                   AutomationId="passwordEntry"
                   Text="{Binding Password}"
                   Placeholder="{i18n:Translate auth_password}"
                   FontFamily="Roboto-Regular"
                   IsPassword="True"
                   HeightRequest="45"
                   FontSize="18"
                   TextColor="{DynamicResource darkGray}"
                   HorizontalOptions="FillAndExpand"
                   VerticalOptions="Center" />
            <Button x:Name="LoginButton"
                  AutomationId="loginButton"
                  Text="{i18n:Translate global_login}"
                  Command="{Binding LoginCommand}"
                  Style="{DynamicResource buttonStyle}" 
                  HeightRequest="45"
                  FontFamily="Roboto-Medium"
                  FontSize="20" />
        </StackLayout>
      </StackLayout>
    </ScrollView>
  </local:LoginPageXaml.Content>
</local:LoginPageXaml>

 

Afterwards, if we go to the Tests.cs class, we will add the method for testing the login functionality.

Since our test requires us to type the user name and password in the corresponding fields, we will use the IApp.EnterText method as follows

 

[Test]
public void Login()
{
     //Login user
     app.Tap(x => x.Marked("usernameEntry"));
     app.EnterText(x => x.Marked("usernameEntry"), "test_user");
     app.EnterText(x => x.Marked("passwordEntry"), "aaaaa");
     app.Tap(x => x.Marked("loginButton"));

     app.Screenshot("Tapped Login");
}

 

where usernameEntry and passwordEntry are the AutomationId of the Entry type fields in which the text must be entered. Afterwards, once the values have been entered in these fields, we will use the IApp. Tap method to simulate the tap on the “Login” button that triggers the validation of credentials against the server.

 

Running the tests

 

To run the tests we have to select the UITests project and the mode of deployment.

 

Tests

 

In our case we have selected the Debug mode in an iPhone simulator. When we give the Play, a simulator will be opened and we will see how the execution will introduce the user and the password that we have defined in the tests and later on how the login button is pressed.

 

 

Artículos Relacionados

Cross-platform development with Xamarin

Xamarin Diaries – Offline by default

Leave a Comment

Responsable » Solidgear.
Finalidad » Gestionar los comentarios.
Legitimación » Tu consentimiento.
Destinatarios » Los datos que me facilitas estarán ubicados en los servidores SolidgearGroup dentro de la UE.
Derechos » Podrás ejercer tus derechos, entre otros, a acceder, rectificar, limitar y suprimir tus datos.

By completing the form you agree to the Privacy Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.