Visual Studio App Center Test is the next generation of Xamarin Test Cloud! Read the blog post.

Start Activity with a Backdoor

PDF for offline use
Related Articles:
Related APIs:
Related Links:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2016-03

Overview

For an Android app with multiple Activities, a backdoor (a special method in the app being tested that can be invoked directly by a Xamarin.UITest) can be used to directly start an Activity other than the MainLauncher.

The sample application has two Activities. The second Activity can be accessed by clicking a button, as demonstrated by the following screenshot:

While it would be straightforward to write a UITest to tap the button on the first activity and display the second activity, this approach may not be practical for a variety of different reasons. This recipe will demonstrate the use of a backdoor to launch an Activity and test it.

A sample Xamarin.Android application with UITests may be found on Github.

Add a Backdoor method to the Startup Activity

The first step is to create the backdoor method and adorn it with the Java.Interop.Export attribute. This attribute will expose the C# method to Android. The backdoor method will contain code to execute on behalf of your test. This snippet is a sample of a backdoor method that is added to the startup Activity:

#if DEBUG
[Java.Interop.Export("StartActivityTwo")]
public void StartActivityTwo()
{
    Intent i = new Intent(this, typeof(SecondActivity));
    StartActivity(i);
}
#endif

The Java.Interop.Export attribute will expose this method to the Xamarin Test Cloud Agent under the name StartActivityTwo. The next section will demonstrate how to invoke this method from a UITest. The method is wrapped in conditional compilation directives so that it is only available in DEBUG builds of the application. This is done to prevent a possible security issue in the RELEASE build of an application.

It may be necessary to add a reference to the Mono.Android.Export.dll assembly in the Xamarin.Android project.

Call the Backdoor in A UITest

Once the backdoor has been added to the Android application IApp.Invoke method is used to execute the method from a UITest:

[Test]
public void Use_backdoor_for_second_activity()
{
    // Arrange
    app.WaitForElement(c => c.Marked("button1"));
    app.Invoke("StartActivityTwo");

    // Act 
    EnterTextOnActivityTwo("Text #2");

    //Assert
    AssertTextHasBeenEnteredOnSecondActivity("Text #2");
}

This snippet will first wait for the first Activity to finish loading, then it will Invoke the backdoor method. The method EnterTextOnActivityTwo will then interact with the Activity, filling the EditText widget with Text #2:

Summary

This recipe demonstrated how a Xamarin.UITest may start an Activity for testing using a backdoor. For more information about using backdoors in Xamarin.UITest, please consult UITest > Working With > Backdoors. The code for a sample application may be found on Github.

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.