I am writing some UI test cases using espresso. My app contains a webview and I am able to perform webClick on the button inside the webview using the following code that uses the element Id to find the element:
onWebView()
.withElement(findElement(Locator.ID, "expandbtn"))
.perform(webClick());
Lets say the webview has the button with text "Expand". Is it possible to perform the webClick using the text on the button instead of locator id?
Yes, there is a way to click on the web element using the text on the button. You can try to use Xpath expression instead of id to find the element.
onWebView().withElement(findElement(Locator.XPATH, xpath_expression).perform(webClick());
And xpath_expression is xpath to the element based on your html source.
"//button[contains(text(),'Expand')]"
For example, if the button's text is inside a child span element:
"//button[@type='%your button type%']/span[text()='Expand']/.."
But remember, xpath is one of the slower methods of finding items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With