Testing the hybrid Android application using Appium

Testing the Hybrid apps is very simple. All you have to do is switch the context to Webview and perform normal selenium operations. Note that Webview debugging should be enabled in the app by developer otherwise you will not be able to switch the context to Webview.

package hybridtests;

import io.appium.java_client.android.AndroidDriver;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;


public class LaunchLocaleur {

    private AndroidDriver driver;
@Test
public void test() throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "4.4.4");
    capabilities.setCapability("deviceName", "sagarcell");
    capabilities.setCapability("browser_Name", "Android");
    capabilities.setCapability("appPackage", "package1");
    capabilities.setCapability("appActivity","ActivityName");

    driver = new AndroidDriver(new URL("https://127.0.0.1:4723/wd/hub"), capabilities) {
    };
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    //get contexts
    //If your app has got any WebViews, count should be more than 1.
    System.out.println("Context count " + driver.getContextHandles().size());

    for (Object contextName : driver.getContextHandles()) {
        System.out.println("Context Name -> " + contextName);
        if (contextName.toString().toUpperCase().contains("WEBVIEW")){
            driver.context(contextName.toString());
            System.out.println("Switched to WebView Context");
        }
    }
    driver.get("https://www.softpost.org");
    driver.close();

    //Switch back to Native app
    driver.context("NATIVE_APP");
    driver.quit();
}
}

You may also like...