isDialog()在Espresso中不匹配Dialog Fragment



我有一套使用Cucumber-JVM和Espresso运行的Android UI测试。然而,我面临着与对话片段交互的问题。

另一个片段在顶部创建一个带有一些视图元素的对话片段。当我试图检查/与这些交互时,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();
    }
}
为了解决这个问题,我在语句中添加了inRoot(isDialog()):
  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);
}

类似的帖子和答案可以在这里找到:在AlertDialog内部视图上使用Espresso进行Android UI测试

也尝试提交inRoot()匹配器,并编写测试尽可能简单,如:

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

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

您还可以匹配任何不是当前活动的根:

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

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

相关内容

  • 没有找到相关文章

最新更新