Xamarin UiTest Backdoor Method

Introduction

 
In the previous article, I explained some facts about Uitest and Setting it Up. Now we are going to learn about Backdoor methods and we will see how we can achieve some special actions in Xamarin Uitest.
 
Backdoor methods are an implementation introduced in Xamarin Native to achieve special actions while automating your App. Some of the scenarios are:
  • Sending app to background and bringing it back(function is available in xamarin.uitest for IOS. In Android, we can achieve it using backdoor ).
  • Map-related applicabilities (like getting a polyline count)
  • Introducing some Test values in DB and testing whether it's reflected in Ui.

Key Points

 
The backdoor method return type will be either string or void.
 
The backdoor method accepts string, int, or bool as a parameter, and can even be empty.
 

Implementation

 
Let's take the example of sending an app to background and then bringing it back:
 
On Android
 
Xamarin UiTest Backdoor Method
 
Before starting, the backdoor method [Java.Interop.Export("TestingBackdoorToBackgroundApp")] attribute is added to expose the backdoor Method .
 
Name of the Backdoor method - TestingBackdoorToBackgroundApp.
Return Type - Void(As we are not expecting any value to be returned)
 
On iOS
 
Xamarin UiTest Backdoor Method
 
Backdoor methods are written inside the AppDelegate class.
 
[Export("TestingBackdoorToBackgroundApp")] is used to to expose the backdoor method .
 
Now, as we are done with the platform related stuff, let's see how we can call these functions.
 
You need to write the test page where you want to send the app to the background. 
 
App.Invoke("TestingBackdoorToBackgroundApp"); 
 
It will invoke your backdoor method and perform the operations.
 
Let's see an example of a backdoor that returns int.
 
iOS Code 
  1. [Export("myBackdoorMethodToReturnInt")]    
  2. public int myBackdoorMethodToReturnInt()    
  3.     
  4. {    
  5.    return 1;    
  6. }    
Calling it from your test Page
  1. int Count = App.Invoke("myBackdoorMethodToReturnInt");     
Here, our count will get a value of 1.
Another scenario to cover is how we can pass a value to the backdoor Method. 
 
iOS Code 
  1. [Export("myBackdoorMethod")]   
  2.        public void MyBackdoorMethod(string Value)    
  3.        {    
  4.           //Write your implementation  
  5.        }     
Calling it from your test Page:
  1. App.Invoke("myBackdoorMethod", Value);  //Here value is string which we want to pass to our backdoor method    
Here, we have covered most of the scenarios of the backdoor method and how we can use it. There are some more concepts of Uitest, like REPL, which we will be learning in our next blog.
 
Happy Coding :)