0
votes

I'm trying to handle the opening browser from a native app.

I have a native android app. When I click a link on it, it opens a browser. I'm trying to switch to that browser but couldn't do it. My code block is:

Set<String> contextNames = ((AppiumDriver) webDriver).getContextHandles();
    for (String contextName : contextNames) {
        if (contextName.contains("android.browser")) {
            ((AppiumDriver) webDriver).context(contextName);
        }
    }

After I debugged some (for further questions) My app contains a vebview on all pages. Here's what I get when I check with getContextHandles() on any page:

0 = "NATIVE_APP"
1 = "WEBVIEW_com.dmall.app"

If I switch context to webview and getPageSource(), I see there is an empty view. html xmlns="http://www.w3.org/1999/xhtml">head>/head>body>iframe name="chromedriver dummy frame" src="about:blank">/iframe>/body>

(I removed some chars because of this page accept html)

I started the test again and after click the link that opens a browser and check getContextHandles(), I see

0 = "NATIVE_APP"
1 = "WEBVIEW_com.dmall.app"
2 = "WEBVIEW_com.android.browser"

Everything looks as it should be, right?

Then I'm doing: ((AppiumDriver) webDriver).context(WEBVIEW_com.android.browser);

when I check the page source, I see the same empty webview html xmlns="http://www.w3.org/1999/xhtml">

By the way, I can't switch to other webview after this point, it says: "An unknown server-side error occurred while processing the command. Original error: We already have a chromedriver instance running (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 134 milliseconds" I guess this is normal

So, how can I switch to browser, any idea, solution, questions, brain storming, anything can be useful.

My capabilities is here:

    DesiredCapabilities capability = DesiredCapabilities.android();
    capability.setCapability(MobileCapabilityType.APP, "http://172.20.0.196:8088/android/mfandroid-debug.apk");
    capability.setCapability(MobileCapabilityType.DEVICE_NAME, "AndroidEmulator" + RandomStringUtils.randomNumeric(3));
    capability.setCapability(MobileCapabilityType.PLATFORM, "Android");
    capability.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");
    capability.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 3000);
    capability.setCapability("autoAcceptAlerts", "true");
    capability.setCapability("sendKeyStrategy", "setValue");

Thanks

1

1 Answers

0
votes

I have faced this issue before so I think I am the right person to answer this question.

You can switch to webView of the app under test [AUT] only and not to the webView of any other app. Which means your app under test is app1 and you want to switch to webView of app2.

To switch to webView of app2 you need to change the app Capabilites at run time after you perform some actions on app1. I suggest you to do:

  1. Close the driver once you click on the link in your first native app (I hope after clicking the link will be opened in the app2).
  2. Re-initialise the driver with new app capabilities of app2.
  3. Now, you should able to switch to webView of app2 (browser).