Espresso Toast 匹配仅适用于 Android 10(API 级别 29)

问题描述 投票:0回答:1

用浓咖啡测试时。 toast 匹配仅适用于 Android 10.

onView(withText(R.string.TOAST_STRING))
                .inRoot(withDecorView(not(getActivity().window.decorView)))
                .check(matches(isDisplayed()))

但是从android 11开始。这不行。对我来说它抛出 RootViewWithoutFocusException

androidx.test.espresso.base.RootViewPicker$RootViewWithoutFocusException: Waited for the root of the view hierarchy to have window focus and not request layout for 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Root:
    Root{application-window-token=android.view.ViewRootImpl$W@6aa9ae4, window-token=android.view.ViewRootImpl$W@6aa9ae4, has-window-focus=false, layout-params-type=1, layout-params-string={(0,0)(fillxfill) ty=BASE_APPLICATION wanim=0x10302fe
      fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
      pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND FIT_INSETS_CONTROLLED
      fitSides=}, decor-view-string=DecorView{id=-1, visibility=INVISIBLE, width=1080, height=2340, 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 wanim=0x10302fe
      fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
      pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND FIT_INSETS_CONTROLLED
      fitSides=}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}}
        at dalvik.system.VMStack.getThreadStackTrace(Native Method)
        at java.lang.Thread.getStackTrace(Thread.java:1736)
        at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:12)
        at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:7)
        at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:8)
        at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:12)
        at com.example.asapandroid.kbTests.SubCategoryPageTest.noNetworkToastShownWhenThereIsNoNetwork(SampleTest.kt:37)

请有人帮我解决这个问题。检查是否在 android 11 及更高版本上显示吐司。

android testing automated-tests android-espresso android-uiautomator
1个回答
-1
投票

我已经通过我自己在 Ultron 框架中的解决方案解决了这个问题(你可以在那里检查实现)。你可以在这里找到答案:https://github.com/open-tool/ultron/wiki/Avoiding-nontrivial-element-lookup-exceptions

© www.soinside.com 2019 - 2024. All rights reserved.