Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso: How to select element on webview using text

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?

like image 657
karma Avatar asked Feb 04 '16 23:02

karma


1 Answers

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.

like image 136
Jupiter Avatar answered Oct 19 '22 19:10

Jupiter