Home Appium Environment Setup Android Emulators iOS Simulators Appium Architecture Android chrome app automation Safari browser automation Appium Capabilities Inspecting android apps Native Android app automation Inspecting iOS app Native iOS app Automation Real Android Phone automation Real iPhone automation Element Identification Pressing button FaceId and TouchId Fingerprint automation Drag and Drop Tap and press Swipe gestures Hybrid app automation Execute script Synchronization in Appium Screenshots Page Object Models Cucumber and Appium Appium integration with azure Appium and Selenium Grid Running tests in browserstack Parallel testing Recording tests Mobile automation framework Errors and Exceptions in Appium Mobile automation test strategy Mobile automation challenges Appium QnA
Hybrid app automation using appium
Some important points
- Hybrid app contains native as well as web elements
- We need to switch to web context to automate web elements and then switch back to native context to perform actions on native controls
- In Android hybrid apps, developers may disable web context switching. So In this case, you need to ask them to enable the web debugging in the app settings.
- In same way, we can do this action in iOS as well
package org.softpost.android.simulator;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created by Sagar on 10-07-2016.
*/
public class HybridTest {
AndroidDriver driver;
@Test
public void test1() throws Exception{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "android");
caps.setCapability("deviceName", "Pixel 9");
caps.setCapability("appPackage", "com.android.calculator2");
caps.setCapability("appActivity","com.android.calculator2.Calculator");
caps.setCapability("automationName","uiautomator2");
try {
driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[@resource-id='com.android.calculator2:id/digit_2']")).click();
driver.findElement(By.xpath("//*[@text='+']")).click();
//We are assuming that calculator app has 2 contexts
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println("Context -> " + contextName);
if (contextName.toLowerCase().contains("webview"))
driver.context(contextName);
}
//web actions
driver.findElement(By.cssSelector("xyz")).click();
//Now switch back to Native app context
driver.context("NATIVE_APP");
}catch (Exception ex) {
ex.printStackTrace();
}finally {
driver.quit();
}
}
}
Web development and Automation testing
solutions delivered!!