gpt4 book ai didi

android - isDialog() 与 Espresso 中的对话框 fragment 不匹配

转载 作者:行者123 更新时间:2023-12-04 23:54:38 31 4
gpt4 key购买 nike

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

另一个 fragment 在顶部创建了一个带有一些 View 元素的对话框 fragment 。当我尝试检查/与这些进行交互时,Espresso 似乎根本没有在 View 层次结构中找到它们。它在错误消息中显示的 View 层次结构是底层 fragment 的 View 层次结构,而不是对话框 fragment 的 View 层次结构,这让我认为它选择了一个不正确的 Root View 。

为了尝试解决这个问题,我将 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");
}
}

对话 fragment 的代码:

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);
}

类似的帖子和答案可以在这里找到:Android UI testing with Espresso on an AlertDialog inner views

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

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

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

关于android - isDialog() 与 Espresso 中的对话框 fragment 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275101/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com