Robotium:让Solo在本地化构建中点击DatePicker



>我正在通过 Robotium 在 DatePickerDialog 中单击"设置"按钮

solo.clickOnButton("Set"); 

如果我现在将测试设备的语言更改为其他语言,Robotium 将无法找到按钮,因为文本不再是"设置"而是翻译的单词。

是否有可能以不同的方式访问选取器中的按钮?

就像在Jelly Bean中一样,DatePicker丢失了"取消"按钮,我无法使用clickOnButton(int index)方法。

我唯一的想法是使用 DatePickerDialog 上的 setButton 来访问按钮文本的本地化字符串资源或保留对按钮的引用。但也许有人知道一种更好的方法来获得访问权限,而无需自定义按钮文本。

问候金

如果你有权访问源代码,你可以同时使用 getString() 和 getView():

Button button = (Button) solo.getView(R.id.x);

solo.clickOnView(button);

还有solo.getString(R.string.x),非常适合用于本地化构建。

我知道

这不是最好的解决方案,但它对我有用:

solo.clickOnButton(0);
这是我

的建议(假设您通过DialogFragment显示对话框):我有一个具有独特TAGSelectDateDialogFragment和一个创建DatePickerDialogonCreateDialog()方法。然后,我通过selectDateDialogfragment.show(getFragmentManager(), SelectDateDialogFragment.TAG)显示对话框。在 Robotium 测试中,我使用如下所示的代码来单击对话框的按钮:

solo.clickOnView(editDateButton);
solo.waitForFragmentByTag(SelectDateDialogFragment.TAG);
solo.setDatePicker(0, 2000, 1, 1);
SelectDateDialogFragment dialogFragment = (SelectDateDialogFragment) activity.getFragmentManager()
        .findFragmentByTag(SelectDateDialogFragment.TAG);
DatePickerDialog dialog = (DatePickerDialog) dialogFragment.getDialog();
Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
solo.clickOnView(okButton);
solo.waitForDialogToClose();
我想

与您分享一些细节。

第一:

solo.clickOnButton(0);

一段时间对我来说效果很好。但是由于新对话框没有"设置"和"取消"按钮,而是"取消"和"确定",因此此解决方案现在将在较新的设备上选择取消按钮,同时只需切换到

solo.clickOnButton(1);

会破坏旧设备的测试。

因此,我通过两个修改迁移到csoltenborn的解决方案:

  • 因为我想与旧设备保持兼容,所以我使用SupportFragmentManager
  • 由于我的片段嵌套在另一个片段中,具体取决于设备及其方向,因此我有时必须访问某个片段 ChildFragmentManager。

这是我的解决方案,也许它可以增加csoltenborn的好答案:

DialogFragment dialogFrag;
Fragment outerFragment = getActivity().getSupportFragmentManager().findFragmentByTag("outerFragmentTAG");
    if (outerFragment == null) {
        dialogFrag = (DialogFragment)getActivity().getSupportFragmentManager().findFragmentByTag("datePicker");
    } else {
        dialogFrag = (DialogFragment)outerFragment.getChildFragmentManager().findFragmentByTag("datePicker");
    }
    Button okButton = ((DatePickerDialog)dialogFrag.getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
    solo.clickOnView(okButton);

最新更新