Wednesday, September 19, 2018

findElement after switching webviews not working

Leave a Comment

I have a simple Espresso test for Android that works okay until the Activity is switched. The test runs through a login, selecting elements from a webview by class and then after a successful login, the activity switches. Once in the new Activity, I am unable to find anything in the new webview, either by class, css selector or by x_path.

I know this is buried in the html, I can see it and search for it in the chrome insepctor:

<div _ngcontent-c9="" class="tile-title" title="Pay">Pay </div> 

Yet I can't find it with this CSS_SELECTOR:

onWebView(allOf<View>(isDisplayed(), isJavascriptEnabled()))                     .withElement(findElement(Locator.CSS_SELECTOR, "div[title=Pay]")) 

Similarly, I have also tried with an X_PATH string and even by class name (which is not unique) but it still fails.

We do have multiple webviews created, but only one is visible.

What can I do to shed more light on this and figure out why I can't select the div in question?

1 Answers

Answers 1

you need to call .reset() at the end of the first interaction;

else you won't be able to access (or match) that reference anymore.

onWebView(allOf<View>(isDisplayed(), isJavascriptEnabled()))     .withElement(findElement(Locator.CSS_SELECTOR, "div[title=Pay]"))     .perform( ... )     .reset(); 

as the documentation for Espresso Web states:

reset() reverts the WebView to its initial state. This is necessary when a prior action, such as a click, introduces a navigation change that makes ElementReference and WindowReference objects inaccessible.

then matching again, will provide a new reference, which can be accessed.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment