Backdoors

PDF for offline use
Related APIs:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2016-09

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 Android and iOS applications with Calabash.

Using the Calabash backdoor API

Both calabash-ios and calabash-android have an API called backdoor that will invoke methods inside and iOS or Android application. Conceptually, backdoor is identical between iOS and Android. However, the implementation is different on each platform.

backdoor in Android

calabash-android will attempt to locate the specified backdoor method according to the following guidelines:

  1. The android.app.Application subclass.
  2. The current activity.
  3. The context of the root view.

A backdoor method is subject to the following limitations:

  • The method must be public.
  • The method may return one of string, Java.Lang.String, or void.
  • The method may accept multiple parameters.

As an example, consider the following Java method in an Android Activity:

public class MainActivity extends Activity {
    public void backdoorFoo(String baz) {
        Log.w(TAG, String.format("backdoorFoo was called the value of '%s'", baz));
    }
}

The following Ruby code can be used to call the backdoor:

backdoor "backdoorFoo", "the value of baz"

backdoor in Xamarin.Android

Xamarin.Android applications must expose the backdoor method using the Java.Interop.ExportAttribute, but otherwise follow the same rules and restrictions as a Java backdoor method. The following C# demonstrates how to expose a backdoor method in an Activity:

[Activity(Label = "@string/activity_main", MainLauncher = true)]
public class MainActivity : Activity
{
    [Export("backdoorFoo")]
    public void BackdoorFoo(string baz)
    {
        // In through the backdoor - do some work.
    }
}

The following Ruby code can be used to call the backdoor:

backdoor "backdoorFoo", "the value of baz"

backdoor in iOS

In iOS, backdoor can be used to invoke any method in the AppDelegate which has either of the following signatures:

- (NSString *)<method name>:(NSString *)aIgnorable;
- (NSString *)<method name>:(NSDictionary *)dictionary;

For example, these two methods are valid Calabash backdoor methods:

- (NSString *)calabashBackdoorLogoutUser:(NSString *)username {
  // Log out a user programmatically.
}

- (NSString *)calabashBackdoorLoginUser:(NSDictionary *)dictionary {
  NSString *username = dictionary[@"username"];
  NSString *password = dictionary[@"password"];
  NSString *authtoken = dictionary[@"authtoken"];
  // Log in a user programmatically.
}

These are not a valid Calabash backdoor methods because the do not have valid method signatures:

- (BOOL)userIsLoggedIn:(NSString *)username;
- (void)logUserOut;
- (NSArray *)addNewFriends:(NSArray *)newFriends;

The following Ruby snippet is an example of how to invoke a backdoor with a single parameter:

backdoor "backdoorFoo:", 'parameter value'

This snippet is an example of calling a backdoor that takes multiple parameters:

backdoor "backdoorFoo:", {baz:'The value of baz'}

backdoor in Xamarin.iOS

In order to use backdoor in Xamarin.iOS, the C# method must be exported using the ExportAttribute.

The following code is a snippet showing how declare a backdoor method in iOS:

[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
    [Export("backdoorFoo:")] // notice the colon at the end of the method name
    public NSString backdoorFoo(NSString value)
    {
        // In through the backdoor - do some work.
    }
}

To invoke this method in calabash-ios:

backdoor "backdoorFoo:", 'The value of baz'

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.