Working with backdoors
Important
Visual Studio App Center is scheduled for retirement on March 31, 2025. While you can continue to use Visual Studio App Center until it is fully retired, there are several recommended alternatives that you may consider migrating to.
Backdoors are methods that are included in a Xamarin.iOS or Xamarin.Android app, and are invoked during a test run to do some special action like configuring a testing state on a device. For example, a backdoor may be used to seed a database with some test data, so that all tests in a test fixture can use it consistently.
The IApp.Invoke
allows a test to call a backdoor method in an app.
How to use Invoke
and implement a backdoor method in an 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.Export
attribute 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 accepts 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.Application
subclass. - 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");
}
}
Note
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 project's AppDelegate according to the following rules:
- The method must be
public
. - The method must be adorned with the
ExportAttribute
and the name of the exposed C# method identified. The exposed name must append a:
(colon) to the name.IApp.Invoke
must use the iOS form of the method name. - The method must take a parameter of
NSString
. - The method must return
NSString
or void.
The following code is a snippet showing how to 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");
}
}