findElement after switching webviews not working

Viewed 1300

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?

Update

It appears I had missed something in the the log that revealed part of my problem. I was seeing a AmbiguousViewMatcherException in the log.

Looking at the code, we have a FrameLayout with the name web_container and attach 3 webviews to the view with an addView() call. So this component ends up having 3 children.

I changed the test to use withParent() and isDisplayed() to match the webview. This call was throwing the exception before, but is now passing.

onWebView(allOf(withParent(withId(R.id.web_container)), isDisplayed())).forceJavascriptEnabled()

Given this success, I still can't find anything inside this webview, here is the modified onWebVeiw statement:

onWebView(allOf(withParent(withId(R.id.web_container)), isDisplayed())).forceJavascriptEnabled()

 Log.d("FeatureTest", "passed forceJavaScriptEnabled()")    
onWebView(allOf(withParent(withId(R.id.web_container)), isDisplayed()))
                        .withElement(findElement(Locator.CSS_SELECTOR, "div[title=Pay]")).perform(webClick())

The error I see in the logs:

09-21 09:19:19.490  9481  9529 D FeatureTest: Key: Mobile, with visibility: 0
09-21 09:19:19.490  9481  9529 D FeatureTest: Key: RDBX, with visibility: 4
09-21 09:19:19.490  9481  9529 D FeatureTest: Key: DEFAULT, with visibility: 4
09-21 09:19:19.496  9481  9481 I ViewInteraction: Performing 'Forcibly enable javascript.' action on view (has parent matching: with id: com.package.android.debug:id/web_container and is displayed on the screen to the user)
09-21 09:19:19.496  9481  9529 D FeatureTest: passed forceJavaScriptEnabled()
09-21 09:19:19.499  9481  9481 I ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.webdriver.DriverAtoms$FindElementTransformingAtom@7c99c3e in window: null with element: null' action on view (has parent matching: with id: com.package.android.debug:id/web_container and is displayed on the screen to the user)
0

09-21 09:19:29.505  9481  9481 I ViewInteraction: Performing 'Propagate: java.lang.RuntimeException: java.util.concurrent.TimeoutException: Waited 10 seconds for android.support.test.espresso.web.internal.deps.guava.util.concurrent.AbstractTransformFuture$TransformFuture@8c677ec[status=PENDING, info=[inputFuture=[android.support.test.espresso.web.internal.deps.guava.util.concurrent.SettableFuture@19327b5[status=PENDING]], function=[android.support.test.espresso.web.action.AtomAction$3@cbf3e4a]]]' action on view (has parent matching: with id: com.package.android.debug:id/web_container and is displayed on the screen to the user)
09-21 09:19:29.515  9481  9529 D FeatureTest: java.lang.RuntimeException@89926d8[cause=java.util.concurrent.TimeoutException: Waited 10 seconds for android.support.test.espresso.web.internal.deps.guava.util.concurrent.AbstractTransformFuture$TransformFuture@8c677ec[status=PENDING, info=[inputFuture=[android.support.test.espresso.web.internal.deps.guava.util.concurrent.SettableFuture@19327b5[status=PENDING]], function=[android.support.test.espresso.web.action.AtomAction$3@cbf3e4a]]],detailMessage=java.util.concurrent.TimeoutException: Waited 10 seconds for android.support.test.espresso.web.internal.deps.guava.util.concurrent.AbstractTransformFuture$TransformFuture@8c677ec[status=PENDING, info=[inputFuture=[android.support.test.espresso.web.internal.deps.guava.util.concurrent.SettableFuture@19327b5[status=PENDING]], function=[android.support.test.espresso.web.action.AtomAction$3@cbf3e4a]]],stackTrace={},suppressedExceptions=[]]
09-21 09:19:29.516  9481  9481 W e.android.debu: Accessing hidden field Landroid/app/Activity;->mResultCode:I (light greylist, reflection)
09-21 09:19:29.517  9481  9481 W e.android.debu: Accessing hidden field Landroid/app/Activity;->mResultData:Landroid/content/Intent; (light greylist, reflection)
09-21 09:19:29.517  9481  9529 I TestRunner: failed: testFeature(com.package.android.ui.FeatureTest)
1 Answers

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.

Related