Question

Is there any possibility to switch from one application to another application at run time using Appium.

Thanks

Was it helpful?

Solution

Finally I found accurate answer, May it will be usefull for some one

source https://www.linkedin.com/grp/post/6669152-6027319885992841219?trk=groups-post-b-title

 // App1 capabilities
 String calculatorAppPackageName="com.android.calculator2";
 String calculatorAppActivityName="com.android.calculator2.Calculator";

// App2 capabilities
 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);
    }

OTHER TIPS

You can change applications by re-instantiating the webdriver with the new application's attributes.

driver = webdriver.Remote(appiumUrl,dcapabilityApp1)
[Your tests]
driver = webdriver.Remote(appiumUrl,dcapabilityApp2)
[New app tests]

As long as you don't close/disconnect the emulator/simulator/device then your user data will be maintained.

You can use:

driver.startActivity(settingsAppPackageName, settingsAppActivityName);

to invoke another app withing the same code.

Going through question , i have an assumption that it might break your driver current session.and if the driver command failed there is no fall back for it. Can't it been done with adb command . One can use above solution or might use abd command

adb shell am start -d <YOUR_ACTIVITY_NAME>

And this will open directly appActivity without fail.

driver.startActivity() method can be used to switch between apps. For more details how it works you can check below video.

Watch "Appium Tutorial- Switching between apps (Contact and SMS)" on YouTube https://youtu.be/sH1bHeDDj8U

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top