How to Switch Between Apps at Runtime Using Appium

How to Switch Between Apps at Runtime Using Appium

5 April 2024 Stephan Petzl Leave a comment Tech-Help

When it comes to automated testing of mobile applications, Appium is a go-to framework for many professionals. A common scenario encountered during testing is the need to switch between two applications at runtime. This guide provides a step-by-step approach to accomplish this task using Appium.

Setting Up Your Test Environment

Before you begin, it’s important to set up your test environment correctly. You’ll need to specify the capabilities for the applications you intend to switch between. Here’s how to set up for two example applications: a calculator app and a settings app on an Android device.

Specifying App Capabilities

            
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, "com.android.calculator2");
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator");

AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            
        

Executing Tests Across Multiple Apps

Once your initial application is launched and your tests are underway, you may want to switch to another application. Here’s an example of how to perform a simple calculation in the calculator app and then switch to the settings app to turn off Wi-Fi:

Performing Actions in the First App

            
// 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();
            
        

Switching to the Second App

            
// Launch settings App
driver.startActivity("com.android.settings", "com.android.settings.Settings");

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

Returning to the First App

            
// Re-launch calculator App
driver.startActivity("com.android.calculator2", "com.android.calculator2.Calculator");

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

Conclusion

Switching between applications during runtime using Appium is a straightforward process once you have properly configured the desired capabilities for each app. By following the steps outlined above, you can automate tests that involve multiple applications, enhancing your testing efficiency and coverage.

Note: Always ensure that your test environment is set up correctly and that you understand the specific capabilities required for the applications you are testing. This will help avoid common pitfalls and ensure a smooth testing experience.

Like this article? there’s more where that came from!