appium: 🐞 bug: Appium crashes when switching from native context to a WebView context when multiple WebViews are present (iOS 17.x real device)

Is there an existing issue for this?

  • I have searched the existing issues

Appium Version

2.11.3

Appium Host Type

Cloud Provider (e.g. BrowserStack/Sauce Labs)

Are you using Appium components not maintained by the Appium team?

No

Is this issue reproducible using the latest components?

  • I confirm the issue is still reproducible with the latest component versions

Current Behavior

I have a hybrid app containing multiple WebViews that I’m attempting to run tests against. When running my test on a real iOS device (via Sauce Labs), Appium successfully switches into the first WebView. However, after returning to the native context and navigating to a second screen with another WebView, Appium crashes when attempting to switch into the new WebView context.

Although getContextHandles() lists valid-looking contexts (e.g. WEBVIEW_739.5, WEBVIEW_739.8), switching to them causes Appium to abruptly terminate the session with:

Appium unexpectedly ended the connection. It may have quit abnormally.

Expected Behavior

Appium should switch to the requested WebView

Appium Log

https://gist.github.com/caseycling/04051bb6cb4a83b3ce0c0949a7070060

Environment Details

Appium version: 2.13.1

XCUITest Driver version: 7.35.0

Remote Debugger version: 12.1.4

iOS version: 17.* and above

Device: Real iPhone (via Sauce Labs)

App type: Hybrid app with multiple WebViews

Context handles at failure:

[NATIVE_APP, WEBVIEW_739.5, WEBVIEW_739.8]

Minimal Reproducible Example

I can’t share the app where I encountered this issue but to reproduce, you would just need an app that has multiple WebViews present and attempt to switch context from native to one of the WebViews.

Here is the code block where context switching fails, leading to the Appium server hanging and eventually timing out:

   // Print available contexts
    System.out.println("Available contexts after Scan: " + driver.getContextHandles());

    // Switch to WebView
   boolean contextSwitched = false;

    for (String context : driver.getContextHandles()) {
        if (!context.equals("NATIVE_APP")) {
            System.out.println("Trying context: " + context);
            driver.context(context);

            try {
                WebElement header = driver.findElement(By.xpath("//XCUIElementTypeApplication[@name=\"Salesforce\"]"));
                System.out.println("Found header in context: " + context);
                contextSwitched = true;
                break;
            } catch (Exception e) {
                System.out.println("Context " + context + " threw on element lookup: " + e.getMessage());
            }
        }
    }`

Further Information

About this issue

Most upvoted comments

Correct. You can verify the most recent version by checking tags on the corresponding github repository: https://github.com/appium/appium-xcuitest-driver/tags