Can't switch context to webview

Hello

I am testing a native app in appium and i’d like to test a part in the app where it redirects into a webview.
In the appium inspector i can see the objects in the webview but can’t click on them.
In the inspector i see only “no context” and can’t switch to a webview context like the tutorial explains.
When i try to use driver.getContextHandles() i get an exception.

What can i do in order to test the webview part in my app?

Still no progress with this issue.
any help ?

perhaps related WEBVIEW context is not found for iOS app

I would like also relate it to this thread Appium only show native app context, is does not recognises the webview context for android app

We have been looking for the answer for quite a while now. Still have no idea. Hope some one can help us go further.

have you tried starting Appium with

appium --full-reset

I was getting better results with that. Might have been false positive though… let me know :slight_smile:

I quickly tried it. No luck. I found several tickets on github for it alreay. This is one of them. https://github.com/appium/appium/issues/5089

Hope the issue can be fixed in next release

thanks for pointing that thread out. I thought it was related to simulators.

@Ag,

You can try below code-

driver = new AndroidDriver(new URL(“http://127.0.0.1:4725/wd/hub”), capabilities);

/* Code for context to find the View used in application */
Set contextNames = Appiumsetup.driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains(“WEBVIEW”)){
Appiumsetup.driver.context(contextName);
}
}

@Manish_Birthariya
The post i wrote initially relates to switching from native view to webview.Most of the test is executed on a native app when a certain action loads a webview.
Maybe you mean that when i open a web view i should start a second driver and close it when i am back to the native view? that’s interesting…Also , i don’t know if there is a difference but i am writing for iOS right now.

@Ag

I have used this code for Android, if you want to switch back to native view you simply type

Appiumsetup.driver.context(“NATIVE_APP”);

The issue is solved, at least for iOS, in Appium 1.4.3 https://github.com/appium/appium/issues/5222

unable to switch to Webview with appium version 1.4.11 or 1.4.12 when test with hybrid apps on Android 4.4.2 or 5.0.1
any idea ? is it the bug in this release?

info: [debug] Getting a list of available webviews
info: [debug] executing cmd: C:\Users\xxxx\AppData\Local\Android\sdk1\platform-tools\adb.exe -s
05e1839607a3b748 shell “cat /proc/net/unix”
info: [debug] Available contexts: NATIVE_APP
info: [debug] []
info: [debug] Available contexts: NATIVE_APP
info: [debug] Responding to client with error: {“status”:35,“value”:{“message”:“No such context foun
d.”,“origValue”:“Context ‘WEBVIEW_1’ does not exist”},“sessionId”:“7bf2552a-995f-402e-ab71-26d4e25ab
f95”}

Hello @maruko888 have you found any solution for your problem ?
I’m having the exact same issue.

@maruko888 @anapadinha

I am using appium version 1.4.14 to test a hybrid app developed by Ionic. The same error message shows up when switching to webview action is executed:

nfo: [debug] Getting a list of available webviews
info: [debug] executing cmd: /Users/gr_haonan/Library/Android/sdk/platform-tools/adb -s 73f50e0d shell “cat /proc/net/unix”
info: [debug] Available contexts: NATIVE_APP
info: [debug] []
info: [debug] Available contexts: NATIVE_APP
info: [debug] Responding to client with error: {“status”:35,“value”:{“message”:“No such context found.”,“origValue”:“Context ‘WEBVIEW_1’ does not exist”},“sessionId”:“0d4ff72b-8084-4546-ba8b-e2311ed05474”}

the code in python is in below as copied from the appium document:
context_name = “WEBVIEW_1”
driver.switch_to.context(context_name)

hi
Set contextNames = driver.getContextHandles();
for (String contextName : contextNames) {

	      System.out.println(contextNames);
	  }

this above line will o/p like[NATIVE_APP, WEBVIEW_com.xxx.xxxx.xxxx]

then you have switch the driver to needed one by using the line driver.context(“WEBVIEW_com.xxx.xxxx.xxxx”);

hi
Set contextNames = driver.getContextHandles();
for (String contextName : contextNames) {

      System.out.println(contextNames);
  }

this above line will give o/p like[NATIVE_APP, WEBVIEW_com.xxx.xxxx.xxxx]

then you have switch the driver to needed one by using the line driver.context(“WEBVIEW_com.xxx.xxxx.xxxx”);

Hi,
Please make sure that the version of selenium-server-standalone, selenium-java, java-client.
I used them as below:
selenium-server-standalone-2.48.2.jar
selenium-java-2.48.2.jar
java-client-3.2.0.jar

My code as below:
protected void switchToWebview() throws Exception {

	// E.G.['NATIVE_APP', 'WEBVIEW_1', ...]
	Set<String> contexts = driver.getContextHandles();

	// make sure we have something other than the native context
	// assertThat(contexts.size(), greaterThan(1));
	for (String context : contexts) {
		// System.out.println(contexts);
		if (!context.equals("NATIVE_APP")) {
			driver.context((String) contexts.toArray()[1]);
			break;
		}
	}
	System.out.println("execute context switch successfully!!!  "
			+ contexts.toArray()[1]);
}

@liucong_3689 I am also trying to automate hybrid iOS app build in ionic using appium only. Using c# for coding, I am able to switch the context but I am not able to identify elements after that. Have you got any solution for this or you made progress on this one.

Thanks in advance

I am using Appium 1.5 for Android.
I am able to successfully switch context to webview and interact with webview element ( my test broke previously when upgraded to 1.4 , 1.5 ) Heres the fixed snippet:

def switch_to_webview(driver):
	if not driver.context or driver.context == "NATIVE_APP":
		debug_log(“print your available contexts:  .................... %s”, driver.contexts)
		switchto = driver.contexts[0]    //point to the index of your webview
		driver.switch_to.context(switchto)
	else:
		debug_log("no need to switch")

self.webview = lambda: self.driver.find_element_by_id("webview")
self.webview()
switch_to_webview(self.driver)

I was automating an hybrid app, while switching, it only shows name of native context. WebView Context is shown as webview_undefined. So how can I get all the webview context names ? pls help soon