Can't switch back to native app view from web view for Android 4.4 on Sauce

Hi,

My python test script will first, in native app context, navigate to a page with a webview , and then switch to webview context to check some text in that web page. After that, self.driver.switch_to.context(‘NATIVE_APP’) is called to switch back to native app context. However, the following error occurs :

File “/Users/yinfeng/ContextLogic/clroot/mobile/sweeper/scripts/android_functional_test/page_operator/base_page_operator.py”, line 108, in switch_to_app_view
self.driver.switch_to.context(‘NATIVE_APP’)
File “/Library/Python/2.7/site-packages/appium/webdriver/switch_to.py”, line 31, in context
self._driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {‘name’: context_name})
File “/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 173, in execute
self.error_handler.check_response(response)
File “/Library/Python/2.7/site-packages/appium/webdriver/errorhandler.py”, line 29, in check_response
raise wde.

Here is the last few lines of selenium log from sauce:

2014-11-13T23:45:48.043Z - debug: Appium request initiated at /wd/hub/session/232f17476502529cbb1841dfc1872aad/context
3522014-11-13T23:45:48.044Z - debug: Request received with params: {“name”:“NATIVE_APP”}
353POST /wd/hub/session/232f17476502529cbb1841dfc1872aad/context 404 1ms - 53b
[end]

Make it simple:

self.driver.switch_to.context(‘WEBVIEW’) , then self.driver.switch_to.context(‘NATIVE_APP’), then error occurs.

Can’t figure out why it happens, need your help. Thanks.

For me also getting same issue.Can any one give solution for this

How to switch from native_app to webview in java …can any one give solution.
i tried Set contextHandles = dr.getContextHandles();
for (String context1 : contextHandles) {

    if (context1.contains("WEBVIEW")){
 dr.context(context1);
    }

but its not working

Same here - self.driver.switch_to.context(‘NATIVE_APP’) isn’t working for me in iOS when trying to switch from a WEBVIEW_1 context.

The iOS7.1 simulator remains on Safari, but the command is transmitted. It looks like the switching context method is returning a success too:

2014-11-17 19:45:48:381 - info: [debug] Responding to client with success: {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_1”],“sessionId”:“c8dabede-057f-4689-9348-a32eac486d94”}
2014-11-17 19:45:48:382 - info: ← GET /wd/hub/session/c8dabede-057f-4689-9348-a32eac486d94/contexts 200 741.256 ms - 98 {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_1”],“sessionId”:“c8dabede-057f-4689-9348-a32eac486d94”}

2014-11-17 19:45:48:386 - info: → POST /wd/hub/session/c8dabede-057f-4689-9348-a32eac486d94/context {“sessionId”:“c8dabede-057f-4689-9348-a32eac486d94”,“name”:“NATIVE_APP”}
2014-11-17 19:45:48:387 - info: [debug] Attempting to set context to ‘NATIVE_APP’
2014-11-17 19:45:48:387 - info: [debug] Responding to client with success: {“status”:0,“value”:“”,“sessionId”:“c8dabede-057f-4689-9348-a32eac486d94”}
2014-11-17 19:45:48:388 - info: ← POST /wd/hub/session/c8dabede-057f-4689-9348-a32eac486d94/context 200 1.925 ms - 74 {“status”:0,“value”:“”,“sessionId”:“c8dabede-057f-4689-9348-a32eac486d94”}

Hi. I have the same issue, But Using Robot Framework Appium Library.

I start in a native app, then, to login using facebook I need to switch context to webview, then perform the login. When I want to return to the native app I get errors like this:
WebDriverException: Message: {“status”: 6, “sessionId”: “ce6e10e5d5c846789fbbc9b90a59b2e8”, “value”: “”}

Saucelabs appium log shows:
2772014-12-10T15:22:08.071Z - debug: Appium request initiated at /wd/hub/session/5bbeca28aa1e13a58a0f67b37643d04e/context
2782014-12-10T15:22:08.072Z - debug: Request received with params: {“name”:“NATIVE_APP”}
279POST /wd/hub/session/5bbeca28aa1e13a58a0f67b37643d04e/context 404 1ms - 53b

Mybe this is an issue related to supported version of appium by saucelabs??

I don’t think it is a saucelabs issue as I mention.

It is more like a different session matter.

When I open the application in saucelabs android Vm, I get a session id, that is the session id for the job.
But when I change to the webview context, it open a chromeDriver instance, with another session id.
So, I think that when I try to return to Native context, I still in the chromeDriver session and in that session there is no context to change.
Look:

First time I change the context:
2014-12-10 16:45:30:714 - info: <-- POST /wd/hub/session/f97c5530-a41b-4e20-8e73-4e7974bc8892/element/4/click 200 460.427 ms - 76 {“status”:0,“value”:true,“sessionId”:“f97c5530-a41b-4e20-8e73-4e7974bc8892”}
2682014-12-10 16:45:32:109 - info: --> GET /wd/hub/session/f97c5530-a41b-4e20-8e73-4e7974bc8892/contexts {}
2692014-12-10 16:45:32:110 - info: [debug] Getting a list of available webviews
2702014-12-10 16:45:32:111 - info: [debug] executing cmd: /home/chef/android-sdk-linux/platform-tools/adb -s emulator-5554 shell “cat /proc/net/unix”
2712014-12-10 16:45:32:239 - info: [debug] WEBVIEW_2471 mapped to pid 2471
2722014-12-10 16:45:32:239 - info: [debug] Getting process name for webview
2732014-12-10 16:45:32:239 - info: [debug] executing cmd: /home/chef/android-sdk-linux/platform-tools/adb -s emulator-5554 shell “ps”
2742014-12-10 16:45:32:337 - info: [debug] Parsed pid: 2471 pkg: com.recarga.recarga
2752014-12-10 16:45:32:337 - info: [debug] from: u0_a54,2471,1803,312364,83940,ffffffff,b7694426,S,com.recarga.recarga
2762014-12-10 16:45:32:337 - info: [debug] returning process name: com.recarga.recarga
2772014-12-10 16:45:32:337 - info: [debug] Available contexts:
2782014-12-10 16:45:32:337 - info: [debug] [“WEBVIEW_com.recarga.recarga”]
2792014-12-10 16:45:32:338 - info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.recarga.recarga
2802014-12-10 16:45:32:338 - info: [debug] Responding to client with success: {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_com.recarga.recarga”],“sessionId”:“f97c5530-a41b-4e20-8e73-4e7974bc8892”}

And finally try to back to Native:
2014-12-10 16:45:47:425 - info: [debug] Making http request with opts: {“url”:“http://127.0.0.1:9515/wd/hub/session/cd7850026d3170ff18494184cdec8ce7/element/0.002443393925204873-1/click",“method”:“POST”,“json”:{“id”:"0.002443393925204873-1”}}
3652014-12-10 16:45:48:122 - info: [debug] Proxied response received with status 200: {“sessionId”:“cd7850026d3170ff18494184cdec8ce7”,“status”:0,“value”:null}
3662014-12-10 16:45:48:123 - info: <-- POST /wd/hub/session/cd7850026d3170ff18494184cdec8ce7/element/0.002443393925204873-1/click 200 698.677 ms - 72
3672014-12-10 16:45:49:608 - info: --> GET /wd/hub/session/cd7850026d3170ff18494184cdec8ce7/contexts {}
3682014-12-10 16:45:49:609 - info: <-- GET /wd/hub/session/cd7850026d3170ff18494184cdec8ce7/contexts 404 1.621 ms - 40
369

Anyone found solution for this? Facing the same error for Android 4.4 on Saucelabs while automating native app. Appium successfully switches to Webview context but returns a SessionNotFound exception when switching back to native context. Works fine on emulator though.
Any solution?

I am still searching for solution. I could find, that te problem is like this:
When you switch to webview context, appium starts a chromeDriver instance with a new session id (different from the appium session you are in). After interact with te webview (for example, make a login to facebook) the chromeDriver session is terminated, but, at this point, the appium session is lost, and you can not return to native, just because you have no more session.

It is giving an error : Responding to client with success: {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_1”]

and i found that you are trying with WEBVIEW…so i suggest that you try with the following code

Set contextWindows = driver.getContextHandles();
for (String contexts : contextWindows) {
System.out.println(contexts);
if (contexts.contains(“WEBVIEW_1”)) {
driver.context(contexts);
}
}

Hello Everyone,
I am automating Hybrid IOS apps using Appium in Ruby. I’m using
driver.appium_device.set_context(driver.appium_device.available_contexts.last) to change to webview after working with webview trying to switch to native_app using
driver.appium_device.set_context(driver.appium_device.available_contexts.first) but not able to find native app elements after switching back to native app.

Can anyone please help me out with this?

Hey everyone,

I am running into the same issue both on 1.3.7 and 1.4.1 versions of appium.

While debugging through the appium logs I see that if I am running a gherkin scenario outline the first instance of switch_to_webview and switch_to_native_view works fine, but after that the next time when it has to switch to webview the text the hangs there and returns an EOF error in ruby. Appium times out with 60 secs of no activity.

For the first instance of switching in and out of webview the chromedriver process is killed and new one is started, whereas when the second time the earlier instance of chromedriver is used. Also the context switch out webview is not happening correctly.

Assuming that could be the problem,

I made this fix in my code and my tests seem to be running:

start test
switch to webview
verify text
switch out of webview
kill chromedriver
end test

This seemed to be passing my tests.

If this helps anyone else please let me know. This will be best for me to validate if i am doing the right thing.

Much appreciated!

@dmenezes,
Thank you for your solution.
I have two concerns:

  1. When you are running test on iOS simulator hybrid app, it is running on AppiumDriver. how can you ‘kill chromedriver’ ?
  2. When there are more tests to be done after switching web - native - web -native view, your solution don’t seem to work.

Hi @sankethm7,

  1. I only tested this on android, I didnt really spend alot of time for iOS about this yet.
  2. I was able to get this working on appium 1.4.1 using appium_lib 7.0 . If this helps. I added killall chromedriver after my line of switch out of webview.

Also if this helps, seems like webview is good on 1.3.7 some of my colleagues were able to successfully switch in and out of webview continuously without any failures.

Hi @dmenezes,

Thank you for the reply.

I also found that this issue is fixed recently and available on version 1.4.6, but only for iOS. Here is the link about the fix:

I have not tried it though.

I hope it gets fixed soon for Android as well.

@Ernesto is correct! The issue is that appium was responding with the chromedriver session id, which it should keep private. The python client would pick up this new session id and try to run commands with it (rather than keeping the same session id as the test was started in). We are releasing version 1.4.7 specifically to fix this issue :smile:

Expect this to be released today or tomorrow.

If you think your issue was different, please respond here.

Thanks @jonahss. I will download the new version and will make some tests. Then I will back here.

Just hold on while we publish :smile:

Just published 1.4.7

I updated to 1.4.7 and I still not able to switch back to native context or to a second webview.

I am using the python client.

self.switch_to.context(context) self.execute_script(str(script)) self.switch_to.context('NATIVE_APP')

Works fine on first Webview but then I cannot interact with a new Webview or with the Native App.

What happens if you don’t run the execute_script? Still can’t switch back to Native context?

Could you open an issue on github.com/appium/appium/issues and include the server logs?