2
votes

In Appium, how do I select a WebView other than the first in an Android app that uses multiple WebViews?

I am using Appium to attempt to create an automated test harness for our hybrid app on Android, and I'm running into a problem where there are three WebViews included in the app and I can only select one of them.

Here is the code to get the list of contexts:

Set<String> contextNames = driver.getContextHandles();
System.out.println("Available contexts:");
for (String contextName : contextNames) {
    System.out.println("\t"  + contextName);
}

This will list "NATIVE_APP" and "WEBVIEW_com.mycompany.myapp" among the available contexts. (The other contexts seem to be related to recently-used apps that Android hasn't close yet.)

When I try to get the URL of the WebView using the following code:

driver.context("WEBVIEW_com.mycompany.myapp");
System.out.println("Current URL is: " + driver.getCurrentUrl());

What I get back is "Current URL is: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html"

Using chrome://inspect from Google Chrome browser shows that the following WebViews are embedded in the application:

https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html
about:blank
file:///android_asset/www/index.html#/app/home

How do I get Appium to switch to the WebView with URL file:///android_asset/www/index.html#/app/home instead of the first one on the list?

1
why don't you use the driver.context("<desired one>"); and supply the desired webview name, if you already know that. - Pankaj Kumar Katiyar

1 Answers

0
votes

try

ReadOnlyCollection<string> windowHandles = InitialiseAppium.driver.WindowHandles;
InitialiseAppium.driver.SwitchTo().Window(windowHandles[1])