Xamarin UI Test


Xamarin.Uitest is a testing framework using which we can automate our application in C# using the Nunit framework which will run on Android and iOS.
Each Uitest is written as methods and is referred to as tests.
The test cases will interact with the UI and perform the operations just as a user does like tapping on buttons, entering text and performing gestures like swiping or pinching. 

Setting up Xamarin UiTest

File-->New Solution-->Multiplatform-->Tests-->Solution name.
Xamarin UI Test
After creating the project the solution will look like this:
Xamarin UI Test
We will start with App Initializer and will try to understand how it works.
  1. public static IApp StartApp(Platform platform)  
  2. {  
  3. // TODO: If the iOS or Android app being tested is included in the solution  
  4. // then open the Unit Tests window, right click Test Apps, select Add App Project  
  5. // and select the app projects that should be tested.  
  6. //  
  7. // The iOS project should have the Xamarin.TestCloud.Agent NuGet package  
  8. // installed. To start the Test Cloud Agent the following code should be  
  9. // added to the FinishedLaunching method of the AppDelegate:  
  10. //  
  11. // #if ENABLE_TEST_CLOUD  
  12. // Xamarin.Calabash.Start();  
  13. // #endif  
  14. if (platform == Platform.Android)  
  15. {  
  16. return ConfigureApp  
  17. .Android  
  18. // TODO: Update this path to point to your Android app and uncomment the  
  19. // code if the app is not included in the solution.  
  20. //.ApkFile ("../../../Droid/bin/Debug/xamarinforms.apk")  
  21. .StartApp();  
  22. }  
  23. return ConfigureApp  
  24. .iOS  
  25. // TODO: Update this path to point to your iOS app and uncomment the  
  26. // code if the app is not included in the solution.  
  27. //.AppBundle ("../../../iOS/bin/iPhoneSimulator/Debug/XamarinForms.iOS.app")  
  28. .StartApp();  
  29. }  
Start App will be the first function called when starting the Test Execution.
So here we have to do the Setup for iOS and Android.

Android Setup

We can either use the application that is already there on the Device using the Package name of the Application or we can install the apk from a specified location.
  1. //Already Installed on device  
  2. return ConfigureApp  
  3. .Android  
  4. .Debug()  
  5. .InstalledApp(“com.Abc.XYZ”)// Package name of Application .  
  6. .DeviceSerial(“ce081718341fbf1f01")//Pass DeviceSerial, in case more than one device connected.  
  7. .StartApp();  
  9. //Specified Location  
  10. return ConfigureApp  
  11. .Android  
  12. .Debug()   
  13. .ApkFile(“../../../com.Abc.XYZ”)//APK will be part of the solution.  
  14. .DeviceSerial(“ce081718341fbf1f05")  
  15. .StartApp();   
Getting the Serial Number of Device,
  • adb devices
  • List of devices attached
  • ce081718341fbf1f05 device
  • emulator-5554 device 
 iOS Setup
  1. //Already Installed on device  
  2. return ConfigureApp  
  3. .iOS  
  4. .Debug()  
  5. .InstalledApp("com.Abc.XYZ")// Bundle id  
  6. .StartApp();  
  8.  //Specified Location  
  9. return ConfigureApp  
  10. .iOS  
  11. .Debug()  
  12. .AppBundle("../../../iOSAppProject/bin/iPhoneSimulator/Debug/iosapp.app")// Getting it from specific Location   
  13. .StartApp();  

Addition Setup Required for iOS

In your Ios Application, install Xamarin.TestClound.Agent .
In the finished launching function of iOS put the below code if you want to be enabled always .
  1. Xamarin.Calabash.Start();  
Even you can put condition on enabling it .
Make sure test cloud is enabled for iOS apps .
Steps to enable,
Right click on solution file-->options -->Complier--> define symbols 
Xamarin UI Test
Now our Android and iOS setup are finished.  Let's try to understand how we will automate that .
We are taking a scenario of Login Validation.
  1. IApp App;   
  2. Query _Username = x => x.Marked("username"); //username is accessibility id for Username textfield  
  3. Query _Password = x => x.Marked("username");    
  4. App.Tap(_Username);  
  5. App.EnterText("Valid Username");//So here we are tapping on username and entering the Correct username   
  6. App.Tap(_Password);  
  7. App.EnterText("Valid Password");//So here we are tapping on Password and entering the Password.  
  8. App.Tap("Login");//This will search for button with text Login and serach for it .  
Now we have successfully executed the login request.
To crosscheck whether we have successfully  logged in or not we need to wait until it navigates to the next page.
So let's see how we can achieve that.
  1. App.WaitForElement(l => l.Marked("Login Success"), timeout: TimeSpan.FromSeconds(100)); //It will wait till the request tin eout after 100 Sec and if the next page title message is "Login success" It gets that element our test Case is pass.   
  2. Assert.Pass(" we have succefully Logged In ");   
So with this article we have suceesfully done the setup for UI Automation and we have gone through a scenario on how to execute it by taking the login example.
I am still working on some complex scenarios and will publish that soon!!!!
Happy Coding :)