Switch apps

In mobile automation testing, there are times when you need to interact with more than one app during a single test session. For example, if you are testing a scenario that involves your main app and an external messaging or payment app. In Appium, this process is known as Switching Between Apps.

Appium allows you to move back and forth between different apps on a device without having to end the test session. Let’s go through how to do this in a simple way.

Switching between apps is useful for testing multi-app scenarios. For instance:

  • Authentication Flows: Switching to an SMS or email app to retrieve an OTP (One-Time Password) for authentication.
  • Social Media Integration: Launching a social media app to log in or share content.
  • Payment Gateway Testing: Navigating to a payment app, like PayPal, to complete a transaction.
  • App Linking: Testing if links open in another app, like opening a YouTube video link from within an app.

Appium provides methods to make app switching straightforward:

  • driver.activateApp(appPackage): Launches a specified app on the device. If the app is already running, it brings the app to the foreground.
  • driver.terminateApp(appPackage): Stops a specific app on the device.
  • driver.queryAppState(appPackage): Checks the current state of the app (whether it’s running, backgrounded, etc.).

Let's go through the steps to switch between apps with an example. In this example, we are switching to a messaging app.

  1. Set Up Desired Capabilities for Main App: Start the test session with your main app (the app under test) using Appium’s desired capabilities.
  2. Activate Another App: To switch to another app, you’ll need to know its appPackage (Android) or bundleId (iOS). You can use the activateApp method to launch the messaging app.
  3. Perform Actions in the Other App: Interact with the second app, such as retrieving an OTP or confirming a payment.
  4. Switch Back to the Main App: Once done, use activateApp to bring the main app back to the foreground and continue your testing.
Java
C#
Python
Javascript
Kotlin
Copy
// Step 1: Start by launching the main app
UiAutomator2Options options = new UiAutomator2Options();
options.setPlatformName("Android");
options.setPlatformVersion("12");
options.setAppPackage("com.example.demoapp");
options.setDeviceName("Pixel_4_Emulator");
options.setAutomationName("UiAutomator2");
options.setApp("/path/to/your/app.apk");

URL appiumServerUrl = new URL("http://localhost:4723");
AndroidDriver driver = new AndroidDriver<>(appiumServerUrl, options);

// Interact with the main app
driver.findElement(By.id("loginButton")).click();

// Step 2: Switch to Messaging App (e.g., SMS app) to retrieve OTP
String messagingAppPackage = "com.android.messaging"; // Messaging app package name for Android
driver.activateApp(messagingAppPackage);

// Interact with the messaging app to retrieve OTP
String otpMessage = driver.findElement(By.id("com.android.messaging:id/message_text")).getText();
System.out.println("Retrieved OTP: " + otpMessage);

// Step 3: Switch back to the main app
driver.activateApp("com.example.mainapp"); // Switch back to the main app

// Use the retrieved OTP in the main app
driver.findElement(By.id("com.example.mainapp:id/otpField")).sendKeys(otpMessage);
driver.findElement(By.id("com.example.mainapp:id/submitOtpButton")).click();
C# code coming soon
Python code coming soon
Javascript code coming soon
Kotlin code coming soon