org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 400. Message: No such context found

Hello Gurus;
I was working on our mobileweb automation since a considerable time. Even though the component software(s) give some issue now and then, but this time it is more like a black box, and I need the community help.

Here is component software being used…

Environment

  • Operating system: Mac Sequoia 15.3.1
  • Appium server version:2.15.0
  • Appium driver(s) and their version(s): [email protected]
  • Appium plugin(s) and their version(s): No plugins activated
  • Node.js version: v20.16.0
  • npm version: 10.8.1
  • Platform and version under test: iOS 18.3.1
  • Real device: Apple iPhone 11
  • JDK: 11.0.26
  • appium-java-client: 9.4.0
  • selenium-java client: 4.28.0
  • TestNG: 7.11.0

The console output:

**Exception during returning AndroidDriver**

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 400. Message: No such context found.

Host info: host: 'macmini.lan', ip: '192.168.86.21'

Build info: version: '4.29.0', revision: '5fc1ec94cb'

System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '15.3.1', java.version: '11.0.26'

Driver info: io.appium.java_client.android.AndroidDriver

Command: [null, newSession {capabilities=[Capabilities {appium:autoGrantPermissions: true, appium:autoWebview: true, appium:automationName: UiAutomator2, appium:deviceName: Galaxy S21 FE 5G, appium:fastReset: true, appium:platformVersion: 14, appium:udid: R5CW71PYTHE, appium:webviewConnectTimeout: 50000, browserName: Chrome, platformName: ANDROID}]}]

Capabilities {appium:autoGrantPermissions: true, appium:autoWebview: true, appium:automationName: UiAutomator2, appium:deviceName: Galaxy S21 FE 5G, appium:fastReset: true, appium:platformVersion: 14, appium:udid: R5CW71PYTHE, appium:webviewConnectTimeout: 50000, browserName: Chrome, platformName: ANDROID}

at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:114)

at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:75)

at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:61)

at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:178)

at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:243)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)

at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:313)

at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:174)

at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:101)

at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:113)

at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:109)

Can you post your capabilities? Like as is in code or from the log.

**public AppiumDriver getDriver4MobileWeb(*String platform, String browser, String deviceName*) throws Exception** {
	URL url = URI.create("http://127.0.0.1:4723").toURL();
	 switch(platform){ 
        **case "Android":**
            try {
				UiAutomator2Options andyOptions = new UiAutomator2Options();
				 andyOptions.setDeviceName(deviceName);
				 andyOptions.setPlatformName(platform);
				 andyOptions.setAutomationName("UiAutomator2");
				 andyOptions.setUdid(udId);
				 andyOptions.setPlatformVersion(platformVersion);
				 andyOptions.setCapability("browserName", browser);
				 andyOptions.setCapability("fastReset", "true");// prerequisite for 'accept all alerts'
				 andyOptions.setCapability("autoGrantPermissions", "true");// accept all alerts
				 andyOptions.setCapability("webviewConnectTimeout", "50000");
				 andyOptions.setCapability("autoWebview", true);// automatically shift to web view, almost unnecessary for Android
				 return new AndroidDriver(url, andyOptions);
			} catch (Exception e) {
				System.out.println("Exception during returning AndroidDriver");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
     **case "iOS":**
     	 try {
				XCUITestOptions iOsOptions = new XCUITestOptions();
				 iOsOptions.setPlatformName(platform);
				 iOsOptions.setDeviceName(deviceName);
				 iOsOptions.setAutomationName("XCUITest");
				 iOsOptions.setUdid(udId);
				 iOsOptions.setPlatformVersion(platformVersion); // is it needed ?
				 iOsOptions.setCapability("browserName", browser);
				 iOsOptions.setCapability("autoAcceptAlerts", "true"); // accept all alerts
				 iOsOptions.setCapability("includeSafariInWebviews", true); // make WebView context to be visible
				 iOsOptions.setCapability("webviewConnectTimeout", "50000"); // make WebView context to be visible
				 iOsOptions.setCapability("autoWebview", true);// automatically shift to web view

				 return new IOSDriver(url, iOsOptions);
		} catch (Exception e) {
			System.out.println("Exception during returning the IOSDriver");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     default:
         throw new Exception("invalid platform");
	 }
}
  1. I think main problem is iOsOptions.setCapability(“appium:autoWebview”, true); because that capability makes appium automatically start in WebContext and if the app you are testing doesn’t have web context by default then appium will throw an error. In this case since you are testing browser directly I guess WebContext is the default context?

  2. Since Appium Java Client 9.2.3 all desired capabilities must have prefix:
    “appium:” try to change those to:

andyOptions.setCapability("appium:browserName", browser);
				 andyOptions.setCapability("appium:fastReset", "true");// prerequisite for 'accept all alerts'
				 andyOptions.setCapability("appium:autoGrantPermissions", "true");// accept all alerts
				 andyOptions.setCapability("appium:webviewConnectTimeout", "50000");
				 andyOptions.setCapability("appium:autoWebview", true);

				 iOsOptions.setCapability("appium:browserName", browser);
				 iOsOptions.setCapability("appium:autoAcceptAlerts", "true"); // accept all alerts
				 iOsOptions.setCapability("appium:includeSafariInWebviews", true); // make WebView context to be visible
				 iOsOptions.setCapability("appium:webviewConnectTimeout", "50000"); // make WebView context to be visible
				 iOsOptions.setCapability("appium:autoWebview", true);
  1. Can you try to run with code with iOsOptions.setCapability(“appium:autoWebview”, true);

  2. If that doesn’t work try again to run the test with:
    iOsOptions.setCapability(“appium:autoWebview”, false);

@derolk
Thanks for you kind input.
I did the change, the AppiumDriver for both Android and iOS are created and retrieved perfectly, without throwing an Exception.
During the previous 6 months this code worked just fine. It must be a change enforced newly with Selenium and Appium Java-Client software components.
I thought, for Android, WEB_VIEW is default and for iOS, it need be shifted from NATIVE.
It is completely strange how new things get enforced with each release of software components !! If what I am thinking is true, what is the capability of WEB_VIEW is useful, anyways ??

Your suggestion saved me a ton of time.
Thanks again,