How to get started with Xamarin.UI test for Android?

tudip-logo

Tudip

22 June 2020

What is the Xamarin.UI test?

Xamarin.ui test is a testing framework that is used to perform automation testing on both iOS and Android application.With Xamarin.UI test we can not only test the Xamarin.Forms app but also the application written natively in Swift, Objective C for iOS and Java and Kotlin for Android.

With Xamarin.UI test we can automate the actions and the operations that the normal user performs on the device such as Touch, Tab, Scroll, Drag and Drop etc. Xamarin Unit test interacts with the mobile application through an interface Xamarin.UITest.IApp. This interface defines methods which are responsible for the collaboration of the application and user interface. This is mainly useful for two platforms like iOS and Android.

  1. Xamarin.UITest.iOS.iOSApp This class is used to perform automation tests on the iOS application.
  2. Xamarin.UITest.Android.AndroidApp This class is used to perform automation on the Android application.

In this article we are going to automate only an android app.

What do we need to get started with the Xamarin.UI test?

  1. Visual Studio
  2. A signed .apk file or and .ipa file.

How to start testing the Project with the Xamarin.UI test?

  1. Go to the visual studio and click on the New Project.

    Add new project > Visual C# > Test > Xamarin.UI Cross-Platform Test Project

  2. Initialise the application for test

    Inside the AppInitialiser Class, you will find a method named “StartApp()”. We need to provide the path to our .apk file inside the AppInitialiser Class.Also this class is responsible for setting up the test environment.

    We can also also define the AppDataMode for the apk that needs to be tested.

    IApp interface is used for each test case performed in the automation. This interface is to prevent spilling one test into another. It is useful to maintain the purity of the test case. Generally, the NUnit test initializes an instance of IApp in the two places below.

    1. In the SetUp method Typically, a test fixture is a logical grouping of related tests, each of them running independently of the other. In this scenario the IApp should be initialized in the SetUp method, ensuring that a new IApp is available for each test.
    2. In the TestFixtureSetup method, in some situations, a single test may require its own test fixture. In this case, it may make more sense to initialize the IApp object once in the TestFixtureSetup method.This class is also responsible for Starting the app.
    public class AppInitializer
    
    {  
    
     public static IApp StartApp(Platform platform)
    
     {  
    
      if (platform == Platform.Android)
    
      {  
    
       IApp iApp = ConfigureApp  
    
        .Android  
    
        .ApkFile("/Users/Documents/ProjectName/ProjectName.Android/bin/Release/ProjectName.apk")  
    
        //Apk file path    
    
        .Debug()  
    
        .EnableLocalScreenshots()  
    
        .StartApp(Xamarin.UITest.Configuration.AppDataMode.Clear);  
    
       return iApp;  
    
      }
    
      else
    
      {  
    
       IApp app = ConfigureApp  
    
       .iOS  
    
       .StartApp();  
    
       return app;  
    
      }  
    
     }  
    
    }
  3. How to write your first test in Xamarin.UI test?

    In this example we will validate the Welcome Message that appears once the app is launched and also we will validate the menu items present inside the hamburger menu list.

    [TestFixture(Platform.Android)]  
    
    public class Tests  
    
    {  
    
        protected IApp app;  
    
        protected Platform platform;  
    
        Pages.LoginPage loginPageObj { get; set; }  
    
        Pages.GuideViewPage guideViewPageObj { get; set; }  
    
        Pages.HamburgerMenuPage hamrgerMenuPageObj { get; set; }  
    
    public Tests(Platform platform)  
    
    {  
    
        this.platform = platform;  
    
    }  
    
    [SetUp]  
    
    public void BeforeEachTest()  
    
     {  
    
      if (this.app == null)  
    
      {  
    
       this.app = AppInitializer.StartApp(platform);  
    
       loginPageObj = new LoginPage(this.app, this.platform);  
    
       hamrgerMenuPageObj = new HamburgerMenuPage(this.app, this.platform);  
    
      }  
    
      else  
    
       hamrgerMenuPageObj.DoLogout(this.platform);  
    
     
    
       loginPageObj.DoLogin();  
    
     }
    
     
    
    [TearDown]  
    
    public void AfterEachTest()  
    
    {  
    
     hamrgerMenuPageObj.DoLogout(this.platform);  
    
    }
    
    [Test, Order(1)]
    
    public void CheckWelcomeMessage()
    
    {
    
    app.WaitForElement(() => app.Query(x=>x.Text(), "Welcome Message not appeared in 10 seconds", new TimeSpan(0,0,10) ;
    
    AppResult[] welcomeText = app.Query(x=>x.Text())
    
    Assert.That(welcomeText[0].Equals("Welcome to the Xamarin.UI test project"),"Welcome text do not match!!");
    
    app.SaveScreenshot();
    
    }
    
     
    
    [Test, Order(2)]
    
    public void CheckHamburgerMenuList()
    
    {
    
    app.Tap(x => x.id("HamburgerMenuList"))
    
    app.WaitForElement(() => app.Query(x=>x.Id("MenuListItem-1"), "Home did not appear   in 5 seconds", new TimeSpan(0,0,5) ;
    
    app.WaitForElement(() => app.Query(x=>x.Id("MenuListItem-1"), "Logout did not appear in 5 seconds", new TimeSpan(0,0,5) ;
    
    }    
    
    }

What is REPL?

To get the mobile screen controls we have REPL (Read Eval Print Loop) method which opens the console with application details using the app.REPL()

In the console window, type tree. This will open a screen in a tree view as per the image.

Xamarin.UI-test-for-andriod-01

How to run the test?

To run the test on the application we have to simply build the Xamarin.UI test project and then the project.This will simply run all the test cases present inside the test class or if we want to run the specific test case then we have to select that particular test case and run it.

In this way we can build the test project in the Xamarin.UI test project.

Request a quote