Can't able to query for iOS context, while trying to switch iOSDriver context to WEB_VIEW (Probably Apple broke the compatibility of XCUITest with iOS 18.3.1 ?)

I had come across this post around 6 months ago, and I happily made use of the discussion in my code, successfully. I was using the ((IOSDriver) driver).getContextHandles() looping logic given by snoopgr2 and checking whether (SupportsContextSwitching)driver).getContext().contains(“WEBVIEW”) is true which was working fine.

Suddenly, today, the line before retrieving the ContextHandles() is throwing the error
getWindowHandle()=>page-69C76D33-9B55-4A11-AA2D-808829F9855Corg.openqa.selenium.**UnsupportedCommandException**: The command 'GET /session/FBE0A449-3336-453C-AFB2-96ECB3A72296/context' was not found.

I checked xcode project WebDriverAgentRunner is running OK or not, which is positive.

I don’t know why it is throwing this exception just today.

Looping logic for only iOS

if(!((SupportsContextSwitching)driver).getContext().contains(“WEBVIEW”)) {// sometimes default WEBVIEW_xxxx.y happens
whileloop:
while (loopCount < 10) { // looping ONLY for ‘iOS’
System.out.println(“loopCount-”+loopCount);
Thread.sleep(10000);//sleep and hope, the driver pulls the webview context
Set allContexts = ((IOSDriver) driver).getContextHandles();//non duplicates
System.out.println("iOS allContexts = “+loopCount+”, “+allContexts);
for (String context : allContexts) {
if (context.contains(“WEBVIEW”)) {
System.out.println(“WEBVIEW was found in iOS allcontexts.”);
((IOSDriver) driver).context(context); // set
break whileloop; //break both loops
} // if
} // for
loopCount++;
} // while
System.out.println(“Current iOS Context=”+loopCount+”, "+((SupportsContextSwitching)driver).getContext());
}//if

I am at (latest) Mac OS: 15.3.1, Appium: 2.11.5, Mobile: 18.3.1 (latest), Appium Java-Client: 9.3.0, selenium-java: 4.25.0, JDK: 21

The same code in STG box, which was deployed a few weeks ago is producing the below system output to console

Diver instance=IOSDriver: Safari on IOS (695e527b-10f5-4d3b-a9b1-4a1a7cf5fd60)
Current Platform=iOS
((SupportsContextSwitching)driver).getContext()=WEBVIEW_18770.1
loopCount-1
iOS allContexts = 1, [NATIVE_APP]
loopCount-2
iOS allContexts = 2, [NATIVE_APP]
loopCount-3
iOS allContexts = 3, [NATIVE_APP]
loopCount-4
iOS allContexts = 4, [NATIVE_APP]
loopCount-5
iOS allContexts = 5, [NATIVE_APP]
loopCount-6
iOS allContexts = 6, [NATIVE_APP]
loopCount-7
iOS allContexts = 7, [NATIVE_APP]
loopCount-8
iOS allContexts = 8, [NATIVE_APP]
loopCount-9
iOS allContexts = 9, [NATIVE_APP]
Current iOS Context=10, WEBVIEW_18770.1

I don’t know why it is throwing this exception today.

Please help.

I tried the below code snippet

 Set<String> contexts = ((IOSDriver) driver).getContextHandles();|

 for (String context : contexts) {|
     if (context.contains(WEBVIEW)) {|
         ((IOSDriver) driver).context(context);|
         break;|
     }|
 }|

I met with the same exception on the line
*Set<String> contexts = ((IOSDriver) driver).getContextHandles();*

Only possibility I can think of as a reason is the update of mobile device from 18.3 to 18.3.1. and the xode downloading iOS 18.2

I updated the components to the latest versions like…

  • Upgrade selenium-java client from 4.25 to 4.28
  • Upgrade io.appium java-client from 9.3 to 9.4
  • Upgrade appium server from 2.11.5 to 2.15
    I am still getting the same error.
org.openqa.selenium.UnsupportedCommandException: 
The command 'GET /session/913ED857-88E1-4FAC-945C-31C73AEB1DC1/contexts' was not found.```

Can you switch to this webView with Safari browser?

@Aleksei
I was first trying to get the MobileWebDriver, like AndroidDriver, iOSDriver which are essentially subclasses for AppiumDriver, where I am getting this exception. If successful at this step, my next step is to switch to the WebView.

Bottom line, is I have not reached that point to switch to WEB_VIEW.

Please find the logs and console output attached as a text file
AppiumLogs for issue20991.txt (16.2 KB)

@Aleksei
Please see also, the posting I did on GitHub.

Eeeeee you already using web browser. Which is already should be in webView.

And as said in gitHub link issue it just does not support switch to any other context.

Maybe you better say what you need to test?

@Aleksei
Thanks for your response.
I have been testing MobileWeb, that is web site on real mobile devices. It is true that, by default it should be in WEB_VIEW is only true for Android, so far I have observed.

Specifically with iOS, I am getting the present reported exception UnsupportedCommand The command ‘GET /session/70AA8DD1-1488-4AB2-B83D-546F73CC1925/context’ was not found. at the line,

Set<String> contexts = ((IOSDriver) driver).getContextHandles();

It looks like, I need to split the test in to two so that new window opening could be relegated in to another test. That is the conclusion for the issue openqa.selenium.UnsupportedCommandException: Method has not yet been implemented at XCUITestDriver.executeCommand · Issue #2268 · appium/java-client · GitHub

But for the present issue, the basic MobileWeb Driver itself could not be retrieved. With out which, automation on iOS(Android could work, I have to test further) may not be feasible.

Thanks for the help @Aleksei

@Aleksei
I updated with simple and brief issue description at

This gives only required details and no unnecessary background.

@Aleksei

Sorry for the confusion and trouble for the collaborator(s). I kind of missed to see the line of code where I was setting the automationName as Safari, which has this problem and I keep worrying about why on earth the program is failing to retrieve context Handles. It is hurriedness due to loss of time.
All is good now with AutomationName restored to XCUITest. But switching windows is not possible for now even with XCUITest. Correct ??

If you start Safari browser = NO as far as this is NOT XCUITest.

here are your capabilities per logs you send in ticket →

Capabilities {appium:autoAcceptAlerts: true, appium:autoWebview: true, appium:automationName: Safari, appium:deviceName: Venkata Nutalapati's iPhone, appium:includeSafariInWebviews: true, appium:platformVersion: 18.3, appium:udid: 

appium:automationName: Safari

While to use XCUITest you need →

// Java
final XCUITestOptions optionsXCUITest = new XCUITestOptions();
optionsXCUITest.setAutomationName(AutomationName.IOS_XCUI_TEST)