Automated UI Testing In Xamarin app

Xamarin.UITest is a C# test automation framework that enables testing mobile apps on Android and iOS. Tests are written locally using either physical devices or simulators/emulators and then submitted to Xamarin Test Cloud for more extensive testing across hundreds of devices and Calabash enables you to write and execute automated tests of mobile apps. It's cross-platform, supporting Android and iOS native apps. It is also open source developed and maintained by Xamarin.

First, create a new project for UI Tests inside Xamarin.Forms Project Solution.

Then Install with these NuGet Packages,

  • NUnit
  • NUnitTestAdapter
  • Xamarin.UITest

There are two files we have in Xamarin UI test project that we need to have a deep look

1> AppInitializer.cs

  1. public static IApp StartApp(Platform platform) {  
  2.     if (platform == Platform.Android) {  
  3.         return ConfigureApp.Android.StartApp();  
  4.     }  
  5.     return ConfigureApp.iOS.StartApp();  
  6. }  
IApp represents the main gateway to interact an app.

 

This interface contains the shared functionality between androidApp and iOSApp

2>Tests.cs

  1. // These lines are for choosing the platform in which you want to run tests.  
  2. [TestFixture(Platform.iOS)]  
  3. [TestFixture(Platform.Android)]  
  4. public class Tests {  
  5.     IApp app;  
  6.     Platform platform;  
  7.     public Tests(Platform platform) {  
  8.             this.platform = platform;  
  9.         }  
  10.         [SetUp] // It will help to start emulator for your UI Tests  
  11.     public void BeforeEachTest() {  
  12.             app = AppInitializer.StartApp(platform);  
  13.         }  
  14.         [Test]  
  15.     public void AppLaunches() {  
  16.         app.Repl();  
  17.         app.Screenshot("First screen.");  
  18.     }  

One of the important tools in creating UITests is the REPL (read-eval-print-loop). REPL is a console-like environment in which the developer enters expressions or commands. It will then evaluate those expressions, and display the results to the user.

->app.Repl(): This will open a console with the emulator, then you can type all the commands and see the changes happening in your UI. Its amazing experience, you can write these commands in Tests.cs file, it will run your UI without touching screen and test your UI.

Xamarin UI test works best with automation Id so you need to give automation Id in XAML for every element, then we can use that id where ever we want.

This line will test if my Button UI control is clickable or not.

Example: app.Tap(“Button_Automation ID”)

This line will enter Username into the entry text with automation Id entryText_AutomationId.

-app.EnterText(“entryText_AutomationId”,”Username”)

The REPL is helpful when creating UITests – it allows us to explore the user interface and create the queries and statements so that the test may interact with the application.

Ebook Download
View all
Learn
View all