Context WebView is not working on Android real device for Hybrid app

Hi Guys,

I am working on automating hybrid app developed using Cordova,
I have used this code to find the context handlers details

Set s=driver.getContextHandles();

	 		for(String handle : s)

	 		{

	 		System.out.println(handle);

	 		}
	 		Thread.sleep(3000);

and found
NATIVE_APP
WEBVIEW_com.abc.scro

however when, I am trying to switch to WebView using following code
driver.context(“WEBVIEW_com.abc.scro”);

Switch is not working.
Kindly help me, to fix this issue as we have 3 hybrid app and same issue I’m facing on all apps.
Please let me know if you want some additional information.

Thanks in advance !!
Ranjan

Why is it not working?
Whats happening?

Can you provide the error stack trace or the server logs?

Thanks a lot @derunewige for your quick reply, I hope your great knowledge in appium may help me in resolving this issue. Kindly find error stack from appium, I am not getting any error in eclipse.

MJSONWP] Calling AppiumDriver.getContexts() with args: ["fcb6c567-9280-4388-8542-2…

[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“cat”,"/proc/net/unix"]

[debug] [AndroidDriver] WEBVIEW_18194 mapped to pid 18194

[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Getting connected devices…

[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“ps”]

[debug] [AndroidDriver] Parsed pid: 18194 pkg: com.abc.scro!

[debug] [AndroidDriver] from: u0_a225,18194,3099,3466020,278572,0000000000,R,com.abc.scro
[debug] [AndroidDriver] returning process name: com.abc.scro
[debug] [AndroidDriver] Found webviews: [“WEBVIEW_com.abc.scro”]

[debug] [AndroidDriver] Available contexts: [“NATIVE_APP”,“WEBVIEW_com.abc.scro”]
[MJSONWP] Responding to client with driver.getContexts() result: [“NATIVE_APP”,"WEBVIEW_com…
[HTTP] <-- GET /wd/hub/session/fcb6c567-9280-4388-8542-2a607828e7b0/contexts 200 246 ms - 122

[HTTP] --> POST /wd/hub/session/fcb6c567-9280-4388-8542-2a607828e7b0/context {“name”:“WEBVIEW_com.abc.scro”}

[MJSONWP] Calling AppiumDriver.setContext() with args: ["WEBVIEW_com.abc.sc…

[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“cat”,"/proc/net/unix"]

[debug] [AndroidDriver] WEBVIEW_18194 mapped to pid 18194
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“ps”]

[debug] [AndroidDriver] Parsed pid: 18194 pkg: com.abc.scro!

[debug] [AndroidDriver] from: u0_a225,18194,3099,3457812,267880,SyS_epoll_,0000000000,S,com.abc.scro
[debug] [AndroidDriver] returning process name: com.abc.scro
[debug] [AndroidDriver] Found webviews: [“WEBVIEW_com.abc.scro”]

[debug] [AndroidDriver] Available contexts: [“NATIVE_APP”,“WEBVIEW_com.abc.scro”]
[debug] [AndroidDriver] Connecting to chrome-backed webview context ‘WEBVIEW_com.abc.scro’
[debug] [Chromedriver] Changed state to ‘starting’
[Chromedriver] Set chromedriver binary as: /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver
[Chromedriver] Killing any old chromedrivers, running: pkill -15 -f “/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver.*–port=9515”

[Chromedriver] No old chromedrivers seemed to exist

[Chromedriver] Spawning chromedriver with: /Applications/Appium.app/Contents/Resources/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”:”","stat…

[JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {“desiredCapabilities”:{"ch…

[HTTP] --> GET /wd/hub/status {}

[MJSONWP] Calling AppiumDriver.getStatus() with args: []

[MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.5.3”…
[HTTP] <-- GET /wd/hub/status 200 9 ms - 83

[JSONWP Proxy] Got response with status 200: {“sessionId”:"eee79b2431640…

[debug] [Chromedriver] Changed state to ‘online’

[MJSONWP] Responding to client with driver.setContext() result: null
[HTTP] <-- POST /wd/hub/session/fcb6c567-9280-4388-8542-2a607828e7b0/context 200 61410 ms - 76

[HTTP] --> POST /wd/hub/session/fcb6c567-9280-4388-8542-2a607828e7b0/element {“using”:“xpath”,“value”:"//*[@class=‘list’]/div[1]//a"}

[MJSONWP] Driver proxy active, passing request on via HTTP proxy

[JSONWP Proxy] Proxying [POST /wd/hub/session/fcb6c567-9280-4388-8542-2a607828e7b0/element] to [POST http://127.0.0.1:9515/wd/hub/session/eee79b24316402ec57afb5f6dd8ed73e/element] with body: {“using”:“xpath”,“value”:"/…
[JSONWP Proxy] Got response with status 200: {“sessionId”:"eee79b2431640…
[JSONWP Proxy] Replacing sessionId eee79b24316402ec57afb5f6dd8ed73e with fcb6c567-9280-4388-8542-2a607828e7b0
[HTTP] <-- POST /wd/hub/session/fcb6c567-9280-4388-8542-2a607828e7b0/element 200 9 ms - 220

[HTTP] --> GET /wd/hub/status {}

[MJSONWP] Calling AppiumDriver.getStatus() with args: []

[MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.5.3”…
[HTTP] <-- GET /wd/hub/status 200 10 ms - 83

[BaseDriver] Shutting down because we waited 60 seconds for a command

[debug] [AndroidDriver] Shutting down Android driver
[Appium] Closing session, cause was ‘New Command Timeout of 60 seconds expired. Try customizing the timeout using the ‘newCommandTimeout’ desired capability’
[Appium] Removing session fcb6c567-9280-4388-8542-2a607828e7b0 from our master session list
[debug] [AndroidDriver] Stopping chromedriver for context WEBVIEW_com.abc.scro
[debug] [Chromedriver] Changed state to ‘stopping’
[JSONWP Proxy] Proxying [DELETE /] to [DELETE http://127.0.0.1:9515/wd/hub/session/eee79b24316402ec57afb5f6dd8ed73e] with no body
[JSONWP Proxy] Got response with status 200: "{“sessionId”:"eee79b243…
[debug] [Chromedriver] Changed state to ‘stopped’
[debug] [ADB] Getting connected devices…

[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“am”,“force-stop”,“com.abc.scro”]

[debug] [ADB] Getting connected devices…

[debug] [ADB] 1 device(s) connected

[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“am”,“force-stop”,“io.appium.unlock”]

[debug] [ADB] Pressing the HOME button

[debug] [ADB] Getting connected devices…

[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“input”,“keyevent”,3]

[debug] [Logcat] Stopping logcat capture

[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“shutdown”}

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“shutdown”}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:“OK, shutting down”}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Closed client connection
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: numtests=1
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: stream=.
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: current=1
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS_CODE: 0
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: stream=
[debug] [AndroidBootstrap] [UIAUTO STDOUT] Test results for WatcherResultPrinter=.
[debug] [AndroidBootstrap] [UIAUTO STDOUT] Time: 203.838
[debug] [AndroidBootstrap] [UIAUTO STDOUT] OK (1 test)
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS_CODE: -1
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [UiAutomator] Shutting down UiAutomator
[debug] [UiAutomator] Moving to state ‘stopping’
[debug] [UiAutomator] UiAutomator shut down normally
[debug] [UiAutomator] Moving to state ‘stopped’
[debug] [ADB] Attempting to kill all uiautomator processes
[debug] [ADB] Getting all processes with uiautomator
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/expert/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“0715f78904dc2636”,“shell”,“ps”,“uiautomator”]

[ADB] No uiautomator process found to kill, continuing…

[debug] [UiAutomator] Moving to state ‘stopped’

So why do you know it is not working?
What do you expect and what is happening?

I am trying to change the context view to web, and perform the WebView operation.
However even after changing to WebContext it is not performing the click.

I am sharing the script which I have used,

package test;

import static org.junit.Assert.*;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Set;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

public class testCheck {

@SuppressWarnings("rawtypes")
@Test

public void test() throws MalformedURLException, InterruptedException {
	
	File appDir = new File("/Users/expert/Desktop/Builds folder/Pk/sample");
	   
    File app = new File(appDir, "Pk.apk");
    AppiumDriver driver;
    
    DesiredCapabilities capabilities = new DesiredCapabilities();
		
       
  			capabilities.setCapability("platformName","Android");
  			
  			capabilities.setCapability("platformVersion","6.0.1");
  			capabilities.setCapability("deviceName","0715f78904dc56536");
  			capabilities.setCapability("udid","0715f78904dc56536");
  			//capabilities.setCapability("browserName","Chrome"); 
  			capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); 
	           System.out.println("Creating Appium session driver...");
	           driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	   		   System.out.println("Driver is created, App is launching...");
	   		   
	   		   Thread.sleep(5000);
	   		System.out.println("App is Loading...");
	   		
	 		driver.findElementByClassName("android.widget.Button").click();
	 		
	 		Thread.sleep(30000);
	 		int size=driver.findElements(By.className("android.widget.EditText")).size();
	 		 System.out.println(size);
	 		 
	 		 List<WebElement>EditBox=driver.findElements(By.className("android.widget.EditText"));
	 		 Thread.sleep(3000);
	 		 EditBox.get(0).clear();
	 		 EditBox.get(0).sendKeys("[email protected]");
	 		 Thread.sleep(3000);
	 		 
	 		 EditBox.get(1).clear();
	 		 EditBox.get(1).click();
	 		 EditBox.get(1).sendKeys("prrocks");
	 		 Thread.sleep(3000);
	 		
	 		 driver.findElementByClassName("android.widget.Button").click();
	 		 Thread.sleep(8000);
	 		
	 		Set<String> s=driver.getContextHandles();

	 		for(String handle : s)

	 		{

	 		System.out.println(handle);

	 		}
	 		Thread.sleep(3000);
	 		
	 		driver.context("WEBVIEW_com.abc.scro");
	 		Thread.sleep(2000);

// This click is not getting performed

	 		driver.findElement(By.xpath("//*[@class='list']/div[1]//a")).click();

}
}

You should use WebDriverWait instead of this explicit waits!!

Are you sure, the webview context has always exact this name?
Are you sure there is a clickable element and it is found?

This is the result and output I’m getting from eclipse,

Creating Appium session driver…
Driver is created, App is launching…
App is Loading…
2
NATIVE_APP
WEBVIEW_com.abc.scro

So webview context has always exact same name.
and yes we have a clickable element on this xpath
//*[@class=‘list’]/div[1]//a

even used id still not working.

@willosser You have helped many users to resolve their issues, and your comments also helped me a lot. Can you plz look into this issue, hope you may come across with this type of problem. Your help will make me kick-start my pending hybrid automation.

@Priyaranjan_dwivedi, I have not delved into webview contexts, so there is little I can offer in terms of advice I can offer directly. In terms of debugging this, I would start by dumping the list of visible elements and seeing if I can find them. For example, is the find_element call returning a valid element? Can you access the element properties?

Wish I could be of more help.

HI Willosser, Sorry for late reply. I tried doing it but failed to get result. :sweat:

Hi @derunewige,

If you can share you mail account, I will share apk file. If you could help me in automating it.

hi Priyaranjan_dwivedi,

in your cordova app do you use project crosswalk ?

Hi @elishka,

No, It’s increase size of the app thats you we are not using it.

there was an issue with Cordova hybrid apps and Webview context in the past.

maybe it has connection to your case.

I have used this code to find the web context view

System.out.println(“Creating Appium session driver…”);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(“Driver is created, App is launching…”);

	   		System.out.println("App is Loading...");
	   		
	 		driver.findElementByClassName("android.widget.Button").click();
	 		Thread.sleep(3000);
	 		
	 		Set<String> s=driver.getContextHandles();

	 		for(String handle : s)

	 		{

	 		System.out.println(handle);

	 		}

	 		Thread.sleep(3000);

And I found out put as

Creating Appium session driver…
Driver is created, App is launching…
App is Loading…
NATIVE_APP
WEBVIEW_me.fresco.Prudence

It means Web context view is getting listed here.

do you have this line in you capabilities ?

                cap.SetCapability("autoWebview", true); 

or you can try to set the webView like this:

                driver.Context = driver.Contexts[1];

Ok let me try this anyway I have user this

System.out.println(driver.getContextHandles());

Set handles=driver.getContextHandles();

Iteratoritr=handles.iterator();

String nativecon=itr.next();

String webcon=itr.next();

WebDriver d=driver.context(webcon);

System.out.println(d.getPageSource());

Tried didn’t worked :frowning:

If you want to give a try can share apk file with you.

I can try, give me your e-mail.