Hybrid app automation using appium

Some important points

  1. Hybrid app contains native as well as web elements
  2. We need to switch to web context to automate web elements and then switch back to native context to perform actions on native controls
  3. 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.
  4. In same way, we can do this action in iOS as well

Below hypothetical code demonstrates how you can perform actions in hybrid app using appium.

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!!