Test Execution does not move to the next step while handling context switching

Hello,
I have been testing a app where i need to switch to WEBVIEW context for clicking an element.
The problem that is being faced is after clicking on the “Sign In” button in WebView Context, an authentication pop-up appears which is NATIVE_APP view , so the execution pointer keeps executing the step of clicking the element and does not come out of the step and the execution fails as a result of TIMEOUT Exception.

Technical Specifications :
Appium version - 1.4.13.1
Language - Java
Genymotion Emulator - Nexus 6 (5.1.1)

These are the Appium logs for the same :

info: → GET /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/contexts {}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: C:\Sumitpal\Androidsdk\sdk\platform-tools\adb.exe -s 192.168.91.101:5555 shell “cat /proc/net/unix”
info: [debug] WEBVIEW_3851 mapped to pid 3851
info: [debug] Getting process name for webview
info: [debug] executing cmd: C:\Sumitpal\Androidsdk\sdk\platform-tools\adb.exe -s 192.168.91.101:5555 shell “ps”
info: [debug] Parsed pid: 3851 pkg: com.appName.xmobile
info: [debug] from: u0_a84,3851,95,1519808,135668,ffffffff,b750ed05,S,com.appName.xmobile
info: [debug] returning process name: com.appName.xmobile
info: [debug] Available contexts: NATIVE_APP
info: [debug] [“WEBVIEW_com.appName.xmobile”]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.appName.xmobile
info: [debug] Responding to client with success: {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_com.appName.xmobile”],“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: ← GET /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/contexts 200 66.494 ms - 117 {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_com.appName.xmobile”],“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: → GET /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/contexts {}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: C:\Sumitpal\Androidsdk\sdk\platform-tools\adb.exe -s 192.168.91.101:5555 shell “cat /proc/net/unix”
info: [debug] WEBVIEW_3851 mapped to pid 3851
info: [debug] Getting process name for webview
info: [debug] executing cmd: C:\Sumitpal\Androidsdk\sdk\platform-tools\adb.exe -s 192.168.91.101:5555 shell “ps”
info: [debug] Parsed pid: 3851 pkg: com.appName.xmobile
info: [debug] from: u0_a84,3851,95,1519808,135932,ffffffff,b750ed05,S,com.appName.xmobile
info: [debug] returning process name: com.appName.xmobile
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.appName.xmobile
info: [debug] [“WEBVIEW_com.appName.xmobile”]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.appName.xmobile
info: [debug] Responding to client with success: {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_com.appName.xmobile”],“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: ← GET /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/contexts 200 53.264 ms - 117 {“status”:0,“value”:[“NATIVE_APP”,“WEBVIEW_com.appName.xmobile”],“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: → POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/context {“name”:“WEBVIEW_com.appName.xmobile”}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: C:\Sumitpal\Androidsdk\sdk\platform-tools\adb.exe -s 192.168.91.101:5555 shell “cat /proc/net/unix”
info: [debug] WEBVIEW_3851 mapped to pid 3851
info: [debug] Getting process name for webview
info: [debug] executing cmd: C:\Sumitpal\Androidsdk\sdk\platform-tools\adb.exe -s 192.168.91.101:5555 shell “ps”
info: [debug] Parsed pid: 3851 pkg: com.appName.xmobile
info: [debug] from: u0_a84,3851,95,1519808,135932,ffffffff,b750ed05,S,com.appName.xmobile
info: [debug] returning process name: com.appName.xmobile
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.appName.xmobile
info: [debug] [“WEBVIEW_com.appName.xmobile”]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.appName.xmobile
info: [debug] Connecting to chrome-backed webview
info: Chromedriver: Changed state to ‘starting’
info: Chromedriver: Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
info: Chromedriver: Killing any old chromedrivers, running: FOR /F “usebackq tokens=5” %a in (netstat -nao ^| findstr /R /C:"9515 ") do (FOR /F “usebackq” %b in (TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe) do (IF NOT %b==“” TASKKILL /F /PID %a))
info: Chromedriver: No old chromedrivers seemed to exist
info: Chromedriver: Spawning chromedriver with: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=9515
info: Chromedriver: [STDOUT] Starting ChromeDriver 2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a) on port 9515
Only local connections are allowed.
info: JSONWP Proxy: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“”,“status”:0,“value”:{“build”:{“version”:“alpha”},“os”:{“arch”:“x86_64”,“name”:“Windows NT”,“version”:“6.1 SP1”}}}
info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {“desiredCapabilities”:{“chromeOptions”:{“androidPackage”:“com.appName.xmobile”,“androidUseRunningApp”:true,“androidDeviceSerial”:“192.168.91.101:5555”}}}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“b44c5fa784ad7cc355dc095a5ab7e4a9”,“status”:0,“value”:{“acceptSslCerts”:true,“applicationCacheEnabled”:false,“browserConnectionEnabled”:false,“browserName”:“chrome”,“chrome”:{},“cssSelect…
info: Chromedriver: Changed state to ‘online’
info: [debug] Responding to client with success: {“status”:0,“value”:null,“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: ← POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/context 200 2053.556 ms - 76 {“status”:0,“value”:null,“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: → GET /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/context {}
info: [debug] Responding to client with success: {“status”:0,“value”:“WEBVIEW_com.appName.xmobile”,“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: ← GET /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/context 200 2.114 ms - 102 {“status”:0,“value”:“WEBVIEW_com.appName.xmobile”,“sessionId”:“6651ef1f-33c3-4f84-b78d-5b05834338c6”}
info: → POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element {“using”:“xpath”,“value”:”//div[@id=‘ContentPlaceHolder1_SignInPanel’]/input"}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element] to [POST http//127.0.0.1:9515/wd/hub/session/b44c5fa784ad7cc355dc095a5ab7e4a9/element] with body: {“using”:“xpath”,“value”:“//div[@id=‘ContentPlaceHolder1_SignInPanel’]/input”}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“b44c5fa784ad7cc355dc095a5ab7e4a9”,“status”:0,“value”:{“ELEMENT”:“0.31185677205212414-1”}}
info: JSONWP Proxy: Replacing sessionId b44c5fa784ad7cc355dc095a5ab7e4a9 with 6651ef1f-33c3-4f84-b78d-5b05834338c6
info: ← POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element 200 535.876 ms - 107
info: → POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element {“using”:“xpath”,“value”:“//div[@id=‘ContentPlaceHolder1_SignInPanel’]/input”}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element] to [POST http//127.0.0.1:9515/wd/hub/session/b44c5fa784ad7cc355dc095a5ab7e4a9/element] with body: {“using”:“xpath”,“value”:“//div[@id=‘ContentPlaceHolder1_SignInPanel’]/input”}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“b44c5fa784ad7cc355dc095a5ab7e4a9”,“status”:0,“value”:{“ELEMENT”:“0.31185677205212414-1”}}
info: JSONWP Proxy: Replacing sessionId b44c5fa784ad7cc355dc095a5ab7e4a9 with 6651ef1f-33c3-4f84-b78d-5b05834338c6
info: ← POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element 200 27.616 ms - 107
info: → POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element/0.31185677205212414-1/click {“id”:“0.31185677205212414-1”}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/6651ef1f-33c3-4f84-b78d-5b05834338c6/element/0.31185677205212414-1/click] to [POST http//127.0.0.1:9515/wd/hub/session/b44c5fa784ad7cc355dc095a5ab7e4a9/element/0.31185677205212414-1/click] with body: {“id”:“0.31185677205212414-1”}
info: [debug] Didn’t get a new command in 60 secs, shutting down…
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: C:\Sumitpal\Androidsdk\sdk\platform-tools\adb.exe -s 192.168.91.101:5555 shell “input keyevent 3”
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] Stopping chromedriver for context WEBVIEW_com.appName.xmobile
info: Chromedriver: Changed state to ‘stopping’
info: JSONWP Proxy: Proxying [DELETE /] to [DELETE http//127.0.0.1:9515/wd/hub/session/b44c5fa784ad7cc355dc095a5ab7e4a9] with no body

Hi Sumitpal

I came across same kind of scenario.
you can handle this scenario like this

// getting the Web View Context
String mainWindow = driver.getContext();

Set < String > allWindow = driver.getContextHandles();

for(String string : allWindow){
if(!string.equalsIgnoreCase(mainWindow))
driver.context(“string”);
}

// Do operations on Native View

// after that switch back to your web view.
driver.context(mainWindow);

I have used this on real devices and it working fine for me.

If you are using Emulators suggest you to give more implicit/explicit wait.

Hope this will help :smile:

Hi ravikr42,

Thanks for the reply.
My main window is NATIVE_APP and not WEBVIEW and also there is no problem in switching the context.
As per my code execution, app successfully switches to WEBVIEW and then when the following code is executed :
driver.findElement(continueToSignInXpath).click();
Aplplication clicks on the button and the authentication pop-up appears(which is again in NATIVE_APP context) but the execution does not move to the next line.

Note: In debug mode, when i execute this command, it continuously shows executing this command for more than 5 minutes and then directly goes to catch block.

Hi SumitPal

Have you tried this
driver.switchTo().defaultContent();

I think this will work.

-Ravi