Appium 1.6 can't find element on Android webview or gives to me WindowException

Hi.
On Appium version 1.6.3 sometimes I have some problem with the Android WebView.

The Following is the steps to reproduce the problem :

  1. switch to webview context
  2. try to find an WebElement on the webview.
    Example :
    WebDriverWait wait = (new WebDriverWait(mDriver, 60));
    WebElement nextButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(“goToNextPippo”)));

And at this point I encounter in one of the following issues :

  1. Sometimes i get the Exception
    " org.openqa.selenium.nosuchwindowexception window not found. the browser window may have been closed" and the test fails
  2. Some times test remain blocked when I try to switch to webview context driver.context(Webview context) . Similar to the issue described
    in this link : https://github.com/appium/appium/issues/7816

Then I have supposed that I have to switch to the correct windows (how reported in this issues post), but in this way or i get the second problem reported above or however appium can’t find the element in the WebView.

All of this problem is appeared on 1.6 version whereas on 1.4 there aren’t those problems.

The following is the code that I use to switch context and to switch to current windows :

public static void switchToWebViewContext(AppiumDriver driver) {
		/* Code for context to find the View used in application */
		
		//Inutility for loop to print all the context
		Set<String> contextNames = driver.getContextHandles();
		
		for (String contextName : contextNames) {
			System.out.println("Stampo tutti i context : " + contextName);
		}
		
		for (String contextName : contextNames) {
			System.out.println("Context " + contextName);
			if (!contextName.equals("NATIVE_APP")) {
				System.out.println("Switch to " + contextName + " Context");
				driver.context(contextName);
				break;
			}
		} 
		
		System.out.println(" !!! Windows Log !!! ");
		windowsLog(driver);
	}
	
	private static void windowsLog(AppiumDriver driver)
	{
		String winHandleBefore = driver.getWindowHandle();
		 Set<String> tmp = driver.getWindowHandles();
		 System.out.print("Finestre :  ");
		 for(String window : tmp)
		 {
			 System.out.print("  "+ window+ " ; ");
		 }
		
		 System.out.println("\n Finestra corrente : "+winHandleBefore);
		 String newWindows = (String)tmp.toArray()[tmp.size()-1];
		 System.out.println("Finestra to switch : "+newWindows);
		 driver.switchTo().window(newWindows);
	}

Environment :

  • Android Device
  • Appium 1.6.3
  • Java Client Beta 3
  • Selenium 3.0.1

EDIT : I add this capabilities capabilities.setCapability(“recreateChromeDriverSessions”, true); And now the problems seems disappeared … Do you know why ? Moreover I have removed the windows switch method.

In what way set the capabilitie “recreateChromeDriverSessions” to true may have resolved the following issues :

  1. appium does not return response after switching to webview by executing driver.context() similar to the issue described
    in this link : https://github.com/appium/appium/issues/7816
  2. org.openqa.selenium.nosuchwindowexception window not found. the browser window may have been closed

And if there weren’t the above issues , the following problem was still there
3. appium doesn’t find the element in the page.

I thought the second and third problems was appening because appium was testing the wrong window and for that I used the following code :

private static void windowsLog(AppiumDriver driver)
{
	String winHandleBefore = driver.getWindowHandle();
	 Set<String> tmp = driver.getWindowHandles();
	 System.out.print("Finestre :  ");
	 for(String window : tmp)
	 {
		 System.out.print("  "+ window+ " ; ");
	 }
	
	 System.out.println("\n Finestra corrente : "+winHandleBefore);
	 String newWindows = (String)tmp.toArray()[tmp.size()-1];
	 System.out.println("Finestra to switch : "+newWindows);
	 driver.switchTo().window(newWindows);
}

But with the recreateChromeDriverSessions capabilities the problems is disappeared.