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

In this Post, I will be going to explain you that how to switch from one android native app to another app at run time using Appium tool. Let say if there is a scenario where you want to perform some operations 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 the scenario.

Scenario

Lets look at the below mentioned scenario :

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 apps

Program

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SwitchAppTest {

AndroidDriver driver = null;
String calculatorAppPackageName="com.android.calculator2";
String calculatorAppActivityName="com.android.calculator2.Calculator";
String settingsAppPackageName="com.android.settings";
String settingsAppActivityName="com.android.settings.Settings";

@Before
public void setUp() throws MalformedURLException
{
DesiredCapabilities capabilities = DesiredCapabilities.android();
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.215.101:5555");
capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, calculatorAppPackageName);
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, calculatorAppActivityName);
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@Test
public void testApp() throws InterruptedException, MalformedURLException
{
//Perform calculation in calculator
driver.findElement(By.name("4")).click();
driver.findElement(By.name("×")).click();
driver.findElement(By.name("3")).click();
driver.findElement(By.name("=")).click();

//launch settings App
driver.startActivity(settingsAppPackageName, settingsAppActivityName);

//Switch OFF WIFI
driver.findElement(By.id("com.android.settings:id/switchWidget")).click();

//Re launch calculator App
driver.startActivity(calculatorAppPackageName, calculatorAppActivityName);

//Validate results
String result = driver.findElement(By.className("android.widget.EditText")).getText();
System.out.println("Result : " + result);
Assert.assertEquals("Incorrect Result", "12", result);
}

@After
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 *