Backdoors
- PDF for offline use:
- Related APIs:
Let us know how you feel about this.
0/250
last updated: 2016-10
Backdoors are methods that can be invoked during a test run to perform some special action to configure or set up testing state on a device.
Overview
Backdoors are methods that can be invoked during a test run to perform some special action to configure or set up testing state on a device. For example, a backdoor may be used to seed a database with some test data so that all of the tests in a given fixture have the same data.
This guide will discuss how to add and use backdoors to test your Xamarin.Android and Xamarin.iOS applications with UITest.
Using IApp.Invoke
IApp.Invoke will allow a test to call a backdoor.
How to use Invoke and implement it in your application is different between iOS and Android.
IApp.Invoke on Android
On Android, IApp.Invoke can be used to invoke a method in the Xamarin.Android application according to the following rules:
- The method must be
public. - The backdoor method must be adorned with the
Java.Interop.Exportattribute that exposes the name of the backdoor method. - The method may return one of
string,Java.Lang.String, orvoid. - The method may accept a parameter which may be a
string,int, orbool. - If the method does accept a parameter, it must be provided to
IApp.Invoke.
The Xamarin Test Cloud Agent will try to locate the method in the following order:
- The
Android.App.Applicationsubclass. - The current activity.
- The context of the root view.
The following code is a snippet of how create a backdoor method in an Activity:
[Activity(Label = "@string/activity_main", MainLauncher = true]
public class MainActivity : Activity
{
[Export("MyBackdoorMethod")]
public void MyBackdoorMethod()
{
// In through the backdoor - do some work.
}
}
To call this method, first wait for the Activity to load, and pass Invoke the name of the method as a string, as shown in the following snippet:
[TestFixture]
public class InvokeExampleTestFixture()
{
[Test]
public void InvokeTest()
{
// Wait for the Activity to load
app.WaitForElement(c => c.Marked("action_bar_title").Text("Enter Credit Card Number"));
// Invoke the backdoor method MainActivity.MyBackDoorMethod
app.Invoke("MyBackdoorMethod");
}
}
It may be necessary to add a reference to the Mono.Android.Export.dll assembly in the Xamarin.Android project.
IApp.Invoke on iOS
On iOS, IApp.Invoke can call a C# method on the AppDelegate according to the following rules:
- The method must be
public. - The method must be adorned with the
ExportAttributeand the name of the exposed C# method identified. The exposed name must append a : (colon) to the name.IApp.Invokemust use the iOS form of the method name. - The method must take a parameter of
NSString. - The method must return
NSString.
The following code is a snippet showing how declare a backdoor method in iOS:
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
[Export("myBackdoorMethod:")] // notice the colon at the end of the method name
public NSString MyBackdoorMethod(NSString value)
{
// In through the backdoor - do some work.
}
}
To call this method, pass the Invoke method the name of the method that was specified in the ExportAttribute, as demonstrated in the following snippet:
[TestFixture]
public class InvokeExampleTestFixture()
{
[Test]
public void InvokeTest()
{
// Wait for the ViewController to appear.
app.WaitForElement(c=>c.Class("UINavigationBar").Marked("Simple Credit Card Validator"));
// Now invoke the backdoor.
app.Invoke("myBackdoorMethod:", "the value");
}
}
Let us know how you feel about this.
0/250
Xamarin Workbook
If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.