Android Espresso testing WebView opened from an SDK using ACTION_VIEW
Asked Answered
P

1

7

In our code, we are using an external SDK to log in users (Auth0). This SDK opens a WebView using intent with ACTION_VIEW.

I was trying to re-write some automation tests we had in place (we used native login before this switch). The WebView opens correctly, but when I try to interact with it using androidx.test.espresso:espresso-web and the code onWebView().forceJavascriptEnabled() I get the following error:

androidx.test.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1841)
at androidx.test.espresso.base.EspressoExceptionHandler.handleSafely(EspressoExceptionHandler.java:2)
at androidx.test.espresso.base.EspressoExceptionHandler.handleSafely(EspressoExceptionHandler.java:1)
at androidx.test.espresso.base.DefaultFailureHandler$TypedFailureHandler.handle(DefaultFailureHandler.java:4)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:5)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:8)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:11)
at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:8)
at androidx.test.espresso.web.sugar.Web$WebInteraction.forceJavascriptEnabled(Web.java:1)

If, instead, I don't use the forceJavascriptEnabled(), and directly try to access the WebView with onWebView().withElement(), I get the error:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: an instance of android.webkit.WebView and webView.getSettings().getJavaScriptEnabled() is <true>

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=2400, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION fmt=TRANSPARENT wanim=0x1030000
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED
pfl=NO_MOVE_ANIMATION FORCE_DRAW_STATUS_BAR_BACKGROUND FIT_INSETS_CONTROLLED
bhv=DEFAULT
fitSides=}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}

I think that, since it's opened using ACTION_VIEW, we're outside of our testing activity (ActivityRule - composeTestRule), and so the framework is unable to recognise that there is indeed an activity above ours, containing a WebView.

Has anyone ever faced a similar problem? How did you solve this?

🙏🏼 Many thanks!

Parmesan answered 16/3, 2023 at 15:3 Comment(0)
H
1

You can now do it like this:

fun logInWebView() {
    val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
    val username = device.findObject(UiSelector().resourceId("email"))
    val password = device.findObject(UiSelector().resourceId("password"))
    val signInButton = device.findObject(UiSelector().className("android.widget.Button"))
    username.setText("...")
    password.setText("...")
    signInButton.click()
}

I saw you have an medium article about this, you can give it as а suggestion there too.

Heuser answered 8/8 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.