I tried your code with my custom built app and i was able to switch to WebContext. Please find below my appium server logs
[MJSONWP] Calling AppiumDriver.setContext() with args: [âWEBVIEWâ,â5edc2707-fe06-4e4a-a343-ab266f554822â]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Getting connected devicesâŚ
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /android-sdk/platform-tools/adb with args: ["-P",5037,"-s",âZX1D62B2JMâ,âshellâ,âcatâ,"/proc/net/unix"]
[debug] [AndroidDriver] WEBVIEW_15249 mapped to pid 15249
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Getting connected devicesâŚ
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /android-sdk/platform-tools/adb with args: ["-P",5037,"-s",âZX1D62B2JMâ,âshellâ,âpsâ]
[debug] [AndroidDriver] Parsed pid: 15249 pkg: com.example.android.apis!
[debug] [AndroidDriver] from: u0_a630,15249,353,1029252,66024,ffffffff,00000000,R,com.example.android.apis
[debug] [AndroidDriver] returning process name: com.example.android.apis
[debug] [AndroidDriver] Found webviews: [âWEBVIEW_com.example.android.apisâ]
[debug] [AndroidDriver] Available contexts: [âNATIVE_APPâ,âWEBVIEW_com.example.android.apisâ]
[debug] [AndroidDriver] Connecting to chrome-backed webview context âWEBVIEW_com.example.android.apisâ
[debug] [Chromedriver] Changed state to âstartingâ
[Chromedriver] Set chromedriver binary as: /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver
[Chromedriver] Killing any old chromedrivers, running: ps -ef | grep /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver | grep -v grep |grep -e ââport=9515(\s.*)?$â | awk â{ print $2 }â | xargs kill -15
[Chromedriver] Successfully cleaned up old chromedrivers
[Chromedriver] Spawning chromedriver with: /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver --url-base=wd/hub --port=9515 --adb-port=5037
[Chromedriver] [STDOUT] Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 9515
Only local connections are allowed.
[JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
[JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
[JSONWP Proxy] Got response with status 200: â{âsessionIdâ:â",âstatusâ:0,âvalueâ:{âbuildâ:{âversionâ:âalphaâ},âosâ:{âarchâ:âx86_64â,ânameâ:âMac OS Xâ,âversionâ:â10.11.4â}}}"
[JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {âdesiredCapabilitiesâ:{âchromeOptionsâ:{âandroidPackageâ:âcom.example.android.apisâ,âandroidUseRunningAppâ:true,âandroidDeviceSerialâ:âZX1D62B2JMâ}}}
[JSONWP Proxy] Got response with status 200: {âsessionIdâ:â690190e7735af36a2d524a0bbf89f15dâ,âstatusâ:0,âvalueâ:{âacceptSslCertsâ:true,âapplicationCacheEnabledâ:false,âbrowserConnectionEnabledâ:false,âbrowserNameâ:âchromeâ,âchromeâ:{âchromedrâŚ
[debug] [Chromedriver] Changed state to âonlineâ
[MJSONWP] Responding to client with driver.setContext() result: null
[HTTP] <-- POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/context 200 2500 ms - 76
[HTTP] --> GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/context {}
[MJSONWP] Calling AppiumDriver.getCurrentContext() with args: [â5edc2707-fe06-4e4a-a343-ab266f554822â]
[MJSONWP] Responding to client with driver.getCurrentContext() result: âWEBVIEW_com.example.android.apisâ
[HTTP] <-- GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/context 200 6 ms - 106
[HTTP] --> POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element {âusingâ:âxpathâ,âvalueâ:â/html/body/a"}
[MJSONWP] Driver proxy active, passing request on via HTTP proxy
[JSONWP Proxy] Proxying [POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element] to [POST http://127.0.0.1:9515/wd/hub/session/690190e7735af36a2d524a0bbf89f15d/element] with body: {âusingâ:âxpathâ,âvalueâ:"/html/body/a"}
[JSONWP Proxy] Got response with status 200: {âsessionIdâ:â690190e7735af36a2d524a0bbf89f15dâ,âstatusâ:0,âvalueâ:{âELEMENTâ:â0.8669703400228173-1â}}
[JSONWP Proxy] Replacing sessionId 690190e7735af36a2d524a0bbf89f15d with 5edc2707-fe06-4e4a-a343-ab266f554822
[HTTP] <-- POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element 200 195 ms - 106
[HTTP] --> GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element/0.8669703400228173-1/text {}
[MJSONWP] Driver proxy active, passing request on via HTTP proxy
[JSONWP Proxy] Proxying [GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element/0.8669703400228173-1/text] to [GET http://127.0.0.1:9515/wd/hub/session/690190e7735af36a2d524a0bbf89f15d/element/0.8669703400228173-1/text] with body: {}
[JSONWP Proxy] Got response with status 200: â{âsessionIdâ:â690190e7735af36a2d524a0bbf89f15dâ,âstatusâ:0,âvalueâ:âHello World! - 1â}â
[JSONWP Proxy] Replacing sessionId 690190e7735af36a2d524a0bbf89f15d with 5edc2707-fe06-4e4a-a343-ab266f554822
[HTTP] <-- GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element/0.8669703400228173-1/text 200 152 ms - 90
[HTTP] --> DELETE /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822 {}
[MJSONWP] Calling AppiumDriver.deleteSession() with args: [â5edc2707-fe06-4e4a-a343-ab266f554822â]
[debug] [AndroidDriver] Shutting down Android driver
[debug] [AndroidDriver] Stopping chromedriver for context WEBVIEW_com.example.android.apis