0

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?

1 Answer 1

0

I know this is a very late answer. But it can help someone who is facing the same kind of problem

  1. If you are switching from native view to webview and you are using the native locators.

  2. If your context is native then use the locators as xpath= //className[@attribute='attributeValue']. It is good to use webview locators if you are trying to locate a web page.

  3. To Switch to webview from native view, you have to use web locators. Connect the debug app you are trying to automate to Chrome browser and enter the following URL chrome://inspect#devices and then inspect. you will get the HTML elements. from here you will get all the locators.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.