isDialog() 与 Espresso 中的对话框片段不匹配

isDialog() is not matching Dialog Fragment in Espresso

我有一套 Android UI 测试 运行 使用 Cucumber-JVM 和 Espresso。但是,我在与对话片段交互时遇到了问题。

另一个片段在顶部创建了一个带有一些视图元素的对话框片段。当我尝试使用这些 check/interact 时,Espresso 似乎根本没有在视图层次结构中找到它们。它在错误消息中显示的视图层次结构是底层片段的视图层次结构,而不是对话框片段的视图层次结构,这让我觉得它选择了一个不正确的根视图。

为了解决这个问题,我在语句中添加了 inRoot(isDialog()):

onView(withText("Ok")).inRoot(isDialog()).check(matches(isDisplayed()));

这会导致以下错误消息:

android.support.test.espresso.NoMatchingRootException: Matcher 'is dialog' did not match any of the following roots: [Root{application-window-token=android.view.ViewRootImpl$W@f99cfec, window-token=android.view.ViewRootImpl$W@f99cfec, has-window-focus=true, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#20 ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=1794, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}, Root{application-window-token=android.view.ViewRootImpl$W@e5f9fb5, window-token=android.view.ViewRootImpl$W@e5f9fb5, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#20 ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=INVISIBLE, width=1080, height=1794, has-focus=true, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}, Root{application-window-token=android.view.ViewRootImpl$W@c5e564a, window-token=android.view.ViewRootImpl$W@c5e564a, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=INVISIBLE, width=1080, height=1794, has-focus=true, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}, Root{application-window-token=android.view.ViewRootImpl$W@9b179bb, window-token=android.view.ViewRootImpl$W@9b179bb, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=INVISIBLE, width=1080, height=1794, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}]
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)

创建对话框的代码:

public class ServiceItemFragment extends Fragment {
    ...
    public void showASampleDialog() {
        DialogFragment newFragment = SampleDialogFragment.newInstance(
                R.string.dialog_title);
        newFragment.show(getFragmentManager(), "dialog");
    }
}

对话框片段代码:

public class SampleDialogFragment extends DialogFragment {

    public static SampleDialogFragment newInstance(int title) {
        SampleDialogFragment frag = new SampleDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        return new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                Log.i(getClass().getSimpleName(), "Positive click");
                            }
                        }
                )
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                Log.i(getClass().getSimpleName(), "Negative click");
                            }
                        }
                )
                .create();
    }
}

To try to fix this, I added inRoot(isDialog()) into the statement:

  onView(withText("Ok")).inRoot(isDialog()).check(matches(isDisplayed()));

如果您使用 AlertDialog 就可以,但不能使用 DialogFragment

为此,最好的解决方案是您的实际解决方法,我的意思是:

UIAutomator: UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject uiObject = device.findObject(new UiSelector().text("Ok"));
try {
    uiObject.click();
} catch (UiObjectNotFoundException e) {
    throw new RuntimeException("UI Object not found", e);
}

类似的 post 答案可以在这里找到:

也尝试忽略 inRoot() 匹配器并尽可能简单地编写测试,例如:

onView(withText("Ok")).check(matches(isDisplayed()));

这可能也有用:http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

您还可以匹配任何不是当前 activity:

的根

onView(withText("Ok")).inRoot(withDecorView(not(is(testRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed())

也可能是设备动画问题。确保在您设备的开发设置中禁用它。