I am using the following code to access the webview but it always returns NATIVE_APP. Am I missing anything. Any suggestions are appreciated. Platform version 4.4, API level 19
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW")){
driver.context(contextName);
}
}
Yes call to getContextHandles only returns NATIVE_APP. My app opens the facebook login page in a browser. Do I need to change any setting in phone. I am using android phone and I have selected stay awake and use debugging options.
Ah, webviews inside your app are different from opening up the browser while your app is running. Unless you open the browser using appium, it isnât set up to automate the new browser activity.
Itâs not that itâs a hybrid app, appium handles hybrid apps fine. The issue is you are opening the Facebook app, which we cannot automate since it wasnât started by appium.
Ok.May be confusion in my end.
When I view it in Appium inspector Facebook page is opened in webkit.webview. Is it a different application?
In IOS facebook is opened in UIWebview and I could able to get the contexts without any issues.
wd.findElement(By.id("com.hathy.fblogin:id/login_button")).click();
Switch to webview context :
//here we getting the list of context
Set<String> contextHandles = wd.getContextHandles();
for (String s : contextHandles) {
System.out.println("Context : " + s);
//if context contains webview then set the webview context
if (s.contains("WEBVIEW")) {
wd.context(s);
}
}
Do Facebook Login Flow in webview :
wd.findElement(By.xpath()).sendKeys("//input[@name='email']""<your_emailid>");
wd"//input[@name='pass']"".findElement(By.xpath()).sendKeys(<your_password>"); // test password
wd.findElement(By.xpath("//button[@name='login']")).click();
WebDriverWait driverWait = new WebDriverWait(wd, 10);
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@name='__CONFIRM__']"))); // waiting for the element to be clickable
System.out.println(wd.getPageSource()); // get the page source
wd.findElement(By.pathx("//button[@name='__CONFIRM__']")).click(); // this step login process is done.
Come back to Native view :
// here i need to get the context again
Set<String> contextHandles2 = wd.getContextHandles();
for (String s : contextHandles2) {
System.out.println("Context : " + s);
if (s.contains("NATIVE_APP")) {
wd.context(s);
}
}