Appium Concepts: How to Switch (Interact) Between 3 Apps (Native/Web Browser) Using Appium
driver.executeScript(“mobile:startActivity”,ImmutableMap.of(“intent”,appPackage/appActivity”));

Get a step-by-step walkthrough in the video below!
Introduction
Inmobile automation testing, interacting with multiple apps within a single test session is a crucial requirement. Appium, an open-source automation framework, provides the capability to switch between native and web-based applications seamlessly.
In this guide, we will walk through the process of switching and interacting with three apps (Native & Web Browser) using Appium.
Command:
driver.executeScript("mobile:startActivity",ImmutableMap.of(
"intent",appPackage/appActivity”));
The driver.executeScript(“mobile: startActivity”, ImmutableMap.of(…)) command in Appium is used to start a specific Android activity within an app or even switch between different apps. It is particularly useful when testing deep links, switching activities, or launching a different app without closing the existing one.

Parameters Explanation:
- “appPackage” → The package name of the app (e.g., “com.example.yourapp”).
- “appActivity” → The full name of the activity you want to launch (e.g., “com.example.yourapp.MainActivity”).

“Complete Code: Optimized and Ready to Use”:
package demo;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
/**
* Script Details - Appium Concepts : How to Switch(Interact) between 3 Apps(Native/WebBrowser)-MobileAutomation
*
* appium-java-client version: 9.3.0
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class SwitchAppsTest {
private AndroidDriver driver;
String AUTappPackage="com.fastaguser";
String AUTappActivity="com.fastaguser.newui.InteractiveActivity";
@BeforeTest
public void steup() throws MalformedURLException {
UiAutomator2Options cap =new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("device11");
cap.setAppPackage(AUTappPackage);
cap.setAppActivity(AUTappActivity);
cap.setNoReset(true);
driver=new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Execution Completed");
}
}
@Test
public void switchappexample() throws InterruptedException {
Thread.sleep(5000);
driver.findElement(AppiumBy.xpath("//android.widget.LinearLayout[@resource-id=\"com.fastaguser:id/nhaionlinenew\"]/android.widget.LinearLayout/android.widget.ImageView")).click();
Thread.sleep(5000);
driver.findElement(AppiumBy.xpath("//android.widget.TextView[@resource-id=\"com.fastaguser:id/branch_company_name\" and @text=\"Amazon\"]")).click();
String AmazonappPackage=driver.getCurrentPackage();
String AmazonappActivity=driver.currentActivity();
System.out.println("Amazon appPackage - "+AmazonappPackage);
System.out.println("Amazon appActivity - "+AmazonappActivity);
Thread.sleep(5000);
driver.executeScript("mobile:startActivity",ImmutableMap.of("intent",AUTappPackage+"/"+AUTappActivity));
Thread.sleep(5000);
driver.findElement(AppiumBy.xpath("//android.widget.TextView[@resource-id=\"com.fastaguser:id/branch_company_name\" and @text=\"Flipkart\"]")).click();
String chromeappPackage=driver.getCurrentPackage();
String chromeappActivity=driver.currentActivity();
System.out.println("Chrome appPackage - "+chromeappPackage);
System.out.println("Chrome appActivity - "+chromeappPackage);
Thread.sleep(5000);
driver.executeScript("mobile:startActivity", ImmutableMap.of("intent",AUTappPackage+"/"+AUTappActivity));
Thread.sleep(5000);
driver.executeScript("mobile:startActivity", ImmutableMap.of("intent",AmazonappPackage+"/"+AmazonappActivity));
Thread.sleep(5000);
driver.executeScript("mobile:startActivity", ImmutableMap.of("intent",chromeappPackage+"/"+chromeappActivity));
Thread.sleep(5000);
driver.executeScript("mobile:startActivity", ImmutableMap.of("intent",AUTappPackage+"/"+AUTappActivity));
}
}
Common Issues & Fixes:

Conclusion
The “mobile: startActivity” command is a powerful command in Appium that allows you to launch activities, switch between apps, handle deep links, and test different scenarios. By using the correct parameters, you can effectively automate complex mobile testing workflows.
Get a step-by-step walkthrough in the video below!
Explore on GitHub:
No comments:
Post a Comment