iOS Can't switch context or even list contexts

Testing on OSX 10.9.5 with Xcode 6.1, on an iOS 7.1 simulator, with Appium 1.3.4, Java-Client 1.2.1, writing tests in Java.

In my tests, I’m trying to determine if specific elements are displayed on the screen in a web view. I’ve tried different variations of the findElement() method, notably xpath, class, and id. Not a single one works, returning failure indicating that “an element could not be located on the page using the given search parameters.” However, everything works perfectly fine on Android, so I know that my xpaths are right…

I think this is related to being unable to switch context to webview - or even list contexts at all - as I’ve tried doing the element check both with and without the switch. My code is as follows:

System.out.println("LOGGING IN");
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
    System.out.println(contextName);
}
System.out.println("PRINTED CONTEXTS");
driver.context((String) contextNames.toArray()[1]);
if (driver.findElementById("serverURL").isDisplayed()) {
    configureWebAdapter();
}

No print statement outside of the first one is ever executed… Here’s my Appium log from right after the app has launched:

info: [debug] Device launched! Ready for commands
info: [debug] Setting command timeout to the default of 60 secs
info: [debug] Appium session started with sessionId 2175de5f-9f83-44ef-8476-456b21a6a6d4
info: <-- POST /wd/hub/session 303 17086.151 ms - 9 
info: --> GET /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4 {}
info: [debug] Responding to client with success: {"status":0,"value":{"webStorageEnabled":false,"locationContextEnabled":false,"browserName":"iOS","platform":"MAC","javascriptEnabled":true,"databaseEnabled":false,"takesScreenshot":true,"networkConnectionEnabled":false,"warnings":{},"desired":{"automationName":"appium","app":"/.../MyProject_122/apps/MyApp/iphone/native/build/Debug-iphonesimulator/MyApp.app","platformVersion":"7.1","platformName":"iOS","deviceName":"iPhone 5s","browserName":"iOS","launchTimeout":"120000","udid":""},"automationName":"appium","app":"/.../MyProject_122/apps/MyApp/iphone/native/build/Debug-iphonesimulator/MyApp.app","platformVersion":"7.1","platformName":"iOS","deviceName":"iPhone 5s","launchTimeout":"120000","udid":""},"sessionId":"2175de5f-9f83-44ef-8476-456b21a6a6d4"}
info: <-- GET /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4 200 1.476 ms - 902 {"status":0,"value":{"webStorageEnabled":false,"locationContextEnabled":false,"browserName":"iOS","platform":"MAC","javascriptEnabled":true,"databaseEnabled":false,"takesScreenshot":true,"networkConnectionEnabled":false,"warnings":{},"desired":{"automationName":"appium","app":"/.../MyProject_122/apps/MyApp/iphone/native/build/Debug-iphonesimulator/MyApp.app","platformVersion":"7.1","platformName":"iOS","deviceName":"iPhone 5s","browserName":"iOS","launchTimeout":"120000","udid":""},"automationName":"appium","app":"/.../MyProject_122/apps/MyApp/iphone/native/build/Debug-iphonesimulator/MyApp.app","platformVersion":"7.1","platformName":"iOS","deviceName":"iPhone 5s","launchTimeout":"120000","udid":""},"sessionId":"2175de5f-9f83-44ef-8476-456b21a6a6d4"}
info: --> POST /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4/element {"using":"partial link text","value":"Loading"}
info: [debug] Invalid locator strategy: partial link text
info: [debug] Responding to client with error: {"status":9,"value":{"message":"Invalid locator strategy: partial link text","origValue":"Invalid locator strategy: partial link text"},"sessionId":"2175de5f-9f83-44ef-8476-456b21a6a6d4"}
info: <-- POST /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4/element 500 1.022 ms - 187 
info: --> GET /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4/contexts {}
info: [debug] [REMOTE] Getting WebKitRemoteDebugger pageArray
error: [REMOTE] Could not connect to WebKitRemoteDebugger server
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. (Original error: connect ECONNREFUSED)","code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","origValue":"connect ECONNREFUSED"},"sessionId":"2175de5f-9f83-44ef-8476-456b21a6a6d4"}
info: <-- GET /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4/contexts 500 5.386 ms - 293 
info: --> DELETE /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4 {}
info: Shutting down appium session
info: [debug] Stopping ios
info: [debug] Destroying instruments client socket.
info: [debug] Closing socket server.
info: [debug] Instruments socket server was closed
info: [debug] Sending sigterm to instruments
info: [debug] [INST] 2015-01-30 19:45:03 +0000 Stopped: Script was stopped by the user
   [testng] LOGGING IN
info: [debug] [INST] Instruments Trace Complete (Duration : 30.568544s; Output : /tmp/appium-instruments/instrumentscli0.trace)
info: [debug] [INSTSERVER] Instruments exited with code 0
info: [debug] Cleaning up after instruments exit
info: [debug] Stopping iOS log capture
error: Unhandled error: TypeError: undefined is not a function
    at WebKitRemoteDebugger.disconnect (/usr/local/lib/node_modules/appium/lib/devices/ios/webkit-remote-debugger.js:60:5)
    at null.<anonymous> (/usr/local/lib/node_modules/appium/lib/devices/ios/ios-hybrid.js:211:21)
    at iOSHybrid.stopRemote (/usr/local/lib/node_modules/appium/lib/devices/ios/ios-hybrid.js:223:7)
    at IOS.postCleanup (/usr/local/lib/node_modules/appium/lib/devices/ios/ios.js:1342:10)
    at IOS.shutdown (/usr/local/lib/node_modules/appium/lib/devices/ios/ios.js:1394:8)
    at null.<anonymous> (/usr/local/lib/node_modules/appium/lib/devices/ios/ios.js:1385:14)
    at wrap [as shutdownCb] (/usr/local/lib/node_modules/appium/node_modules/appium-instruments/lib/instruments.js:418:5)
    at Instruments.onInstrumentsExit (/usr/local/lib/node_modules/appium/node_modules/appium-instruments/lib/instruments.js:397:10)
    at null.<anonymous> (/usr/local/lib/node_modules/appium/node_modules/appium-instruments/lib/instruments.js:308:12)
    at ChildProcess.emit (events.js:98:17) context: [POST /wd/hub/session {"desiredCapabilities":{"automationName":"appium","app":"/...]
info: [debug] Didn't get a new command in 60 secs, shutting down...
info: [debug] Trying to stop appium but there's no session, doing nothing
info: [debug] We shut down because no new commands came in
info: <-- DELETE /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4 - - ms - - 
info: --> DELETE /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4 {}
info: <-- DELETE /wd/hub/session/2175de5f-9f83-44ef-8476-456b21a6a6d4 404 0.877 ms - 40 
   [testng] Jan 30, 2015 1:55:03 PM org.apache.http.impl.execchain.RetryExec execute
   [testng] INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:4723: The target server failed to respond
   [testng] Jan 30, 2015 1:55:03 PM org.apache.http.impl.execchain.RetryExec execute
   [testng] INFO: Retrying request to {}->http://localhost:4723
   [testng] FAILED: verifyPersonAndVehiclePage
   [testng] org.openqa.selenium.WebDriverException:
 An unknown server-side error occurred while processing the command. 
(Original error: connect ECONNREFUSED) (WARNING: The server did not 
provide any stacktrace information)
   [testng] Command duration or timeout: 9 milliseconds
   [testng] Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
   [testng] System info: host: '...', ip: '...', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.5', java.version: '1.7.0_67'
   [testng] Session ID: 2175de5f-9f83-44ef-8476-456b21a6a6d4
   [testng] Driver info: io.appium.java_client.ios.IOSDriver
   [testng] Capabilities [{automationName=appium, platform=MAC, app=/.../MyProject_122/apps/MyApp/iphone/native/build/Debug-iphonesimulator/MyApp.app, javascriptEnabled=true, browserName=iOS, launchTimeout=120000, networkConnectionEnabled=false, udid=, desired={automationName=appium, app=/.../MyProject_122/apps/MyApp/iphone/native/build/Debug-iphonesimulator/MyApp.app,
 platformVersion=7.1, platformName=iOS, deviceName=iPhone 5s, 
browserName=iOS, launchTimeout=120000, udid=}, 
locationContextEnabled=false, platformVersion=7.1, 
databaseEnabled=false, platformName=iOS, deviceName=iPhone 5s, 
webStorageEnabled=false, warnings={}, takesScreenshot=true}]
   [testng]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   [testng]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
   [testng]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   [testng]     at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
   [testng]     at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
   [testng]     at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
   [testng]     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
   [testng]     at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:180)
   [testng]     at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:185)
   [testng]     at io.appium.java_client.AppiumDriver.getContextHandles(AppiumDriver.java:543)
   [testng]     at my.package.tasks.Tasks.login(Tasks.java:33)
   [testng]     at my.package.test.SearchTest.verifyPage(SearchTest.java:49)
   [testng]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   [testng]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   [testng]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   [testng]     at java.lang.reflect.Method.invoke(Method.java:606)
   [testng]     at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
   [testng]     at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
   [testng]     at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
   [testng]     at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
   [testng]     at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
   [testng]     at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
   [testng]     at org.testng.TestRunner.privateRun(TestRunner.java:767)
   [testng]     at org.testng.TestRunner.run(TestRunner.java:617)
   [testng]     at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
   [testng]     at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
   [testng]     at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
   [testng]     at org.testng.SuiteRunner.run(SuiteRunner.java:240)
   [testng]     at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
   [testng]     at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
   [testng]     at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
   [testng]     at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
   [testng]     at org.testng.TestNG.run(TestNG.java:1031)
   [testng]     at org.testng.TestNG.privateMain(TestNG.java:1338)
   [testng]     at org.testng.TestNG.main(TestNG.java:1307)
   [testng] 
   [testng] 
   [testng] ===============================================
   [testng]     BVT-Suite-iOS
   [testng]     Tests run: 1, Failures: 1, Skips: 0
   [testng] ===============================================
   [testng] 
   [testng] 
   [testng] ===============================================
   [testng] BVT
   [testng] Total tests run: 1, Failures: 1, Skips: 0
   [testng] ===============================================
   [testng] 
   [testng] [TestNG] Time taken by org.testng.reporters.EmailableReporter@2e3f8a3e: 4 ms
   [testng] [TestNG] Time taken by org.uncommons.reportng.HTMLReporter@29bcbda5: 83 ms
   [testng] [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@72d60cdd: 11 ms
   [testng] [TestNG] Time taken by org.uncommons.reportng.JUnitXMLReporter@21d377a4: 18 ms
   [testng] [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 4 ms
   [testng] [TestNG] Time taken by org.testng.reporters.XMLReporter@3dd36fa0: 11 ms
   [testng] [TestNG] Time taken by org.testng.reporters.jq.Main@66d2c37c: 24 ms
   [testng] [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@f7fb9e0: 2 ms
   [testng] The tests failed.

Line 33 of Tasks.java is Set contextNames = driver.getContextHandles();

Do you run ios-webkit-debug-proxy? If not, go to appium folder and run:

node bin/ios-webkit-debug-proxy-launcher.js -c device_udid:27753

I am not. However, that appears to be needed only for testing on physical devices and I’m using a simulator.

That’s right. Debug proxy is required for real device.

Right. Which I’m not using. So it doesn’t exactly do anything to fix my issue…

I am seeing the exact same issue. I am running on the simulator so it shouldn’t be an ios_webkit_debug_proxy issue.

I am using ruby though and my call is this:
$driver.available_contexts

The error is the same error you are getting:
info: --> GET /wd/hub/session/51f71c03-8f3a-403a-8772-85afd9f7b6af/contexts {}
info: [debug] [REMOTE] Getting WebKitRemoteDebugger pageArray
error: [REMOTE] Could not connect to WebKitRemoteDebugger server
info: [debug] Responding to client with error: {“status”:13,“value”:{“message”:“An unknown server-side error occurred while processing the command. (Original error: connect ECONNREFUSED)”,“code”:“ECONNREFUSED”,“errno”:“ECONNREFUSED”,“syscall”:“connect”,“origValue”:“connect ECONNREFUSED”},“sessionId”:“51f71c03-8f3a-403a-8772-85afd9f7b6af”}
info: <-- GET /wd/hub/session/51f71c03-8f3a-403a-8772-85afd9f7b6af/contexts 500 2.746 ms - 293

Confusingly, I can successfully make a call to driver.getContext() and get a response back of NATIVE_APP. But any attempt at anything else regarding contexts, including changing to a webview directly without the loop code in my original post (that was just copied from here https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/hybrid.md) just results in the same failure.

Hi Guys,
Is this problem solved. I am also facing the same problem with iOS Emulators.
Is there a workaround

I am facing similar issue. On trying to switch context to webview on my hybrid app it results in error.
I am running on real device with iOS 9.3.1 and Appium 1.5.2.

I have installed ios-webkit-debug through brew.

debug] [iOS] Executing iOS command ‘setContext’
[debug] [iOS] Attempting to set context to ‘WEBVIEW_com.synacor.hermes4’
[debug] [iOS] Getting list of available contexts
[debug] [iOS] Retrieving contexts and views
[debug] [iOS] Selecting by url: false
[debug] [RemoteDebugger] Getting WebKitRemoteDebugger pageArray: localhost, 27753
[debug] [RemoteDebugger] Sending request to: http://localhost:27753/json
[MJSONWP] Encountered internal error running command: RequestError: Error: connect ECONNREFUSED 127.0.0.1:27753
at new RequestError (/Users/pbhandolkar/appium/appium/node_modules/request-promise/lib/errors.js:11:15)
at Request.RP$callback [as _callback] (/Users/pbhandolkar/appium/appium/node_modules/request-promise/lib/rp.js:60:32)
at self.callback (/Users/pbhandolkar/appium/appium/node_modules/request/request.js:200:22)
at emitOne (events.js:77:13)
at Request.emit (events.js:169:7)
at Request.onRequestError (/Users/pbhandolkar/appium/appium/node_modules/request/request.js:831:8)
at emitOne (events.js:77:13)
at ClientRequest.emit (events.js:169:7)
at Socket.socketErrorListener (_http_client.js:265:9)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1253:8)
at doNTCallback2 (node.js:452:9)
at process._tickCallback (node.js:366:17)