I am trying to write a test case using selenium Java for a mobile app in Android device.I have to find an element which in web view of mobile application.I have inspect the application using Appium inspect window and get the element details as below.
index: 65
text:
class: android.widget.Button
content-desc: Take New Photo
package: com.abc.xyz
resource id:
checkable: false
checked: false
clickable: true
enabled: true
focusable: true
focused: false
scrollable: false
long-clickable: false
is password: false
selected: false
And I have util method to switch window from native view to we view.
public class Context {
public void switchContextToWebView(AppiumDriver<WebElement> driver)
throws InterruptedException {
Thread.sleep(Constants.DEFAULT_TIMEOUT);
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
if (contextName.contains("WEBVIEW")) {
driver.context(contextName);
}
}
}
public void switchContextToNativeView(AppiumDriver<WebElement> driver)
throws InterruptedException {
Thread.sleep(Constants.DEFAULT_TIMEOUT);
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
if (contextName.contains("NATIVE")) {
driver.context(contextName);
}
}
}
}
And in my test class I try to find this element by using below codes.
Context context = new Context();
context.switchContextToWebView(driver);
//WebElement pickCameraOption = driver.findElement(By.className("android.widget.Button"));
WebElement elem = driver.findElementByXPath("//android.widget.Button[@content-desc='Take New Photo']");
//WebElement elem = driver.findElementByXPath("//*[@class='android.widget.Button'and @content-desc='Take New Photo']");
wait.until(ExpectedConditions.visibilityOf(elem));
elem.click();
You can observe I have tried two ways to get this work done.But they are not working for me.
Can anyone help me to figure out the exact issue with this selenium finders?