Appium : How to switch from one native app to another native app

In this Post, we will be going to discuss about that how to switch from one android mobile app to another app at run time using Appium tool. Let say, if you come up with scenario where where you want to perform some actions on one app and in between there is a need to access another app and then again you want to switch back to the original app in order to complete your test scenario. It’s not that difficult as Appium allows you to perform App switching during script execution.

Lets have a look at the scenario mentioned below

1. Launch Calculator App
2. Multiply 2 numbers
3. Launch Settings App
4. Switch Off the Wifi
5. Re-Launch Calculator app and then validate the multiplication result which you have performed in step 2.
6. Close both apps

NOTE : Just for reference, sharing the software versions used in below code is as follows :
Selenium

Complete Code

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class SwitchAppTest {
	public AppiumDriver<MobileElement> driver = null;

	String calculatorAppPackageName = "com.sec.android.app.popupcalculator";
	String calculatorAppActivityName = "Calculator";
	String settingsAppPackageName = "com.android.settings";
	String settingsAppActivityName = "com.android.settings.GridSettings";

	@BeforeTest
	public void setupstart() throws MalformedURLException {
		DesiredCapabilities capabilities = DesiredCapabilities.android();
		capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
		capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
		capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");
		capabilities.setCapability("appPackage", calculatorAppPackageName);
		capabilities.setCapability("appActivity", calculatorAppActivityName);
		driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities);
	}

	@Test
	public void calcTest1() throws Exception {
		//Multiply 2 numbers in calculator app
		driver.findElement(By.xpath("//android.widget.Button[@text='4']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='Multiplication']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@text='4']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='Equal']")).click();

		// launch settings App
		Activity activity = new Activity(settingsAppPackageName, settingsAppActivityName);
		activity.setStopApp(false);
		((AndroidDriver<MobileElement>) driver).startActivity(activity);

		// Switch OFF WIFI
		driver.findElement(By.xpath("//android.widget.LinearLayout[@content-desc='Wi-Fi']")).click();
		driver.findElement(By.className("android.widget.Switch")).click();

		// Re launch calculator App
		activity = new Activity(calculatorAppPackageName, calculatorAppActivityName);
		activity.setStopApp(false);
		((AndroidDriver<MobileElement>) driver).startActivity(activity);
		String result = driver.findElement(By.className("android.widget.EditText")).getText();
		System.out.println("Result : " + result);

	}

	@AfterTest
	public void tearDown() {
		driver.quit();
	}
}

If you really like the information provided above, please don’t forget to leave the comment. You can also like us on Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *