ACTION_VIEW
的意图打开 WebView。我试图重写一些我们已有的自动化测试(在此切换之前我们使用本机登录)。 WebView 正确打开,但是当我尝试使用
androidx.test.espresso:espresso-web
和代码
onWebView().forceJavascriptEnabled()
与它交互时 我收到以下错误:
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)
如果我不使用 forceJavascriptEnabled()
,并直接尝试使用
onWebView().withElement()
访问 WebView,则会收到错误:
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}
我认为,由于它是使用 ACTION_VIEW
打开的,因此我们超出了测试活动(ActivityRule -
composeTestRule
),因此框架无法识别我们上方确实有一个包含 WebView 的活动。有人遇到过类似的问题吗?你是怎么解决这个问题的?
🙏🏼非常感谢!
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()
}
我看到你有一篇关于此的中等文章,你也可以在那里提出建议。