Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find multiple elements in web view with espresso

I'm testing a hybrid app, where each view has a web view.
In one of these web views I have a list of elements with the same attribute. They have the same xpath locator that is something like:

//h4[contains(@data-role, 'product-name')]

I want to create a list of these elements and iterate through them, count them, get their attributes.

In the documentation, I found two similar methods:

findElement(locator, value)

and

findMultipleElements(locator, value)

Though it's totally unclear to me how to use it. I tried to find examples on it but with no success.

Could someone help me with this?

like image 329
Jenny Avatar asked Aug 20 '26 14:08

Jenny


1 Answers

Here is the solution that I have found.

@kaqqao is right that findMultipleItems call returns Atom<List<ElementReference>> that is not usable with onWebView() because there you have only withElement() that accepts either Atom<ElementReference> or just ElementReference

What you can do though is perform your action that find multiple items and just get results from your Atom. This is how it works internally if you check the source of doEval method inside Web.java for espresso.

val elements = with(AtomAction(findMultipleElements(
                Locator.XPATH,
                "YOUR_COMPLEX_XPATH"
        ), null, null)) {
            onView(ViewMatchers.isAssignableFrom(WebView::class.java)).perform(this)
            this.get()
        }

This code will give you List<ElementMatcher>.

Then just run it as

elements.forEach {          
  onWebView().forceJavascriptEnabled().withElement(it).perform(webClick())
}
like image 140
AAverin Avatar answered Aug 23 '26 16:08

AAverin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!