Search code examples
cordovaselenium-webdriverautomationappiumhybrid-mobile-app

how to get web element in AppiumDriver from a hybird app?


I am trying to automate a Cordova build hybird app using appium and selenium.

However, I am unable to get any element using

driver.findElement(By.id("xxx"));

when I do a pageSource dump using

String pageSource = driver.getPageSource();

    System.out.print("*************dumping: ***********\n"+pageSource+"\n\n\n");

I am getting

<?xml version="1.0" encoding="UTF-8"?><hierarchy rotation="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.alarmforce.connect.client.keypad" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1024,552]" resource-id="" instance="0"><android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.alarmforce.connect.client.keypad" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1024,552]" resource-id="" instance="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.alarmforce.connect.client.keypad" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,25][1024,552]" resource-id="android:id/content" instance="1"><android.webkit.WebView index="0" text="" class="android.webkit.WebView" package="com.alarmforce.connect.client.keypad" content-desc="Web View" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,25][1024,552]" resource-id="" instance="0"/></android.widget.FrameLayout></android.widget.LinearLayout></android.widget.FrameLayout></hierarchy>

How do I make it show up in the HTML format like what was showing in chrome://inspect when I connected the device?

I tried:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }

in MainActivity.java

I also tried:

driver.context("web_view");

the capabilities:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName","xxxxxxx");
capabilities.setCapability("platformVersion", "4.4");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

Solution

  • A good way to switch the context using appium is :

    Set<String> contextNames = driver.getContextHandles();
    String setContext = contextNames.toArray()[1].toString(); //generally your WEBVIEW context if available
    driver.context(setContext);// set context to the webview name in string format