Configuring backdoor methods in C# for Xamarin Test Cloud

One of the most powerful mechanisms we offer in Xamarin Test Cloud is the ability to invoke specific code in your application from your UI Tests. This is called invoking a backdoor method.

Like the Xamarin Test Cloud Agent for iOS, backdoors should never be enabled in your release build as they could pose a security risk. Your test/debug build should have all the backdoors and configuration needed for proper UI testing.

Let’s say I’ve built a Xamarin.Forms application that simply loads a WebView, and I want to control the loaded URL via a backdoor method. In other words, I want the test to control what gets loaded in the WebView.

iOS

On iOS, the method signature for a backdoor method needs to look like this:


[Export("SetWebviewUrl:")]

public NSString SetWebviewUrl(NSString webviewUrl)

{

// set the webview's .Source property to webviewUrl

}

A couple of things to point out:

  • The signature must have NSString for both the return type and the argument.
  • The Export attribute is found in System.ComponentModel.Composition, so make sure to add a reference to this assembly.
  • Note the trailing colon in the method name in the Export attribute.

The backdoor method is invoked like this:


app.Invoke("SetWebviewUrl", "http://www.xamarin.com");

Android

On Android, it’s a little bit different. First of all, the reference we need is not System.ComponentModel.Composition – you need to add Mono.Android.Export as a reference instead.

On Android, the method may take any number of arguments as well as try to type convert the return value for you. This method is nonetheless the same on Android:


[Java.Interop.Export("SetWebviewUrl")] // note there is no trailing colon

public String SetWebviewUrl(String webviewUrl)

{

// Set the webview URL

}

And this method is invoked in exactly the same way:


app.Invoke("SetWebviewUrl", "http://www.xamarin.com");

Happy Testing!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s