How to Test an Android Application Across Multiple Activities

How to Test an Android Application Across Multiple Activities

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Developing complex Android applications often involves creating multiple screens and workflows across various activities. For instance, consider a banking application where a user logs in, navigates through different menus, and performs various transactions. Testing such workflows end-to-end can be challenging. This guide will help you understand how to create automated tests that span multiple activities.

Challenges of Multi-Activity Testing

Testing across multiple activities in Android is not straightforward. Traditional tools like ActivityInstrumentationTestCase2 can be limited to a single activity, making it difficult to test complete workflows. However, there are several solutions available that can help you overcome these challenges.

Solution: Using Instrumentation Tests

Instrumentation tests allow you to interact with the application’s UI and validate the behavior across multiple activities. Below is a simplified example of how you can use instrumentation tests to test a login workflow:

package com.mycompany;

import android.content.*;
import android.test.*;
import android.test.suitebuilder.annotation.*;
import android.util.*;
import android.view.*;
import android.widget.*;

import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.*;
import static*;

public class LoginTests extends InstrumentationTestCase {

   public void testAValidUserCanLogIn() {

      Instrumentation instrumentation = getInstrumentation();

      // Register we are interested in the authentication activity...
      Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(AuthenticateActivity.class.getName(), null, false);

      // Start the authentication activity as the first activity...
      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.setClassName(instrumentation.getTargetContext(), AuthenticateActivity.class.getName());

      // Wait for it to start...
      Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
      assertThat(currentActivity, is(notNullValue()));

      // Type into the username field...
      View currentView = currentActivity.findViewById(username_field);
      assertThat(currentView, is(notNullValue()));
      assertThat(currentView, instanceOf(EditText.class));
      TouchUtils.clickView(this, currentView);

      // Type into the password field...
      currentView = currentActivity.findViewById(password_field);
      assertThat(currentView, is(notNullValue()));
      assertThat(currentView, instanceOf(EditText.class));
      TouchUtils.clickView(this, currentView);

      // Register we are interested in the welcome activity...
      monitor = instrumentation.addMonitor(WelcomeActivity.class.getName(), null, false);

      // Click the login button...
      currentView = currentActivity.findViewById(login_button);
      assertThat(currentView, is(notNullValue()));
      assertThat(currentView, instanceOf(Button.class));
      TouchUtils.clickView(this, currentView);

      // Wait for the welcome page to start...
      currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
      assertThat(currentActivity, is(notNullValue()));

      // Make sure we are logged in...
      currentView = currentActivity.findViewById(welcome_message);
      assertThat(currentView, is(notNullValue()));
      assertThat(currentView, instanceOf(TextView.class));
      assertThat(((TextView)currentView).getText().toString(), is("Welcome, MyUsername!"));

Improving Test Reliability

While the above example demonstrates a basic approach, you may encounter timing issues where tests do not always pass reliably. To mitigate these issues, you can use getInstrumentation().waitForIdleSync() to ensure the UI thread is idle before proceeding with the next step. Additionally, using tools like Robotium can simplify the process further.

Alternative Tools for Multi-Activity Testing

Several tools can help simplify multi-activity testing:

  • Robotium: An open-source test framework for Android applications that supports black-box testing.
  • MonkeyTalk: Provides recording and scripting capabilities for both Android and iOS applications.
  • Calabash-Android: Enables writing tests in a business-readable language called Gherkin.

For more information on these tools, check out our Android Testing Tool page.

Leverage No-Code Test Automation with Repeato

If you’re looking for a more intuitive and faster way to create, run, and maintain automated tests for your Android apps, consider using Repeato. Repeato is a no-code test automation tool that leverages computer vision and AI to make test automation accessible to everyone, regardless of their technical expertise. Its intuitive test recorder and scripting interface allow you to automate complex use cases efficiently.

With Repeato, you can ensure your multi-activity workflows are thoroughly tested without the hassle of writing extensive code. Learn more about Repeato’s capabilities in our documentation.

For further assistance or inquiries, feel free to contact us.

Like this article? there’s more where that came from!