我想使用 Espresso 按下面的按钮,但我不确定如何操作。我应该获取资源 ID 吗?或者如何给AlertDialog设置一个ID??
@RunWith(AndroidJUnit4.class)
public class ApplicationTest {
@Rule
public ActivityTestRule<LoadingActivity> mActivityRule =
new ActivityTestRule<>(LoadingActivity.class);
@Test
public void loginClickMarker() {
//Doesn't work:
onView(withText("GA NAAR INSTELLINGEN")).perform(click());
}
}
public class PopupDialog {
public static void showGPSIsDisabled(Context context, String msg, final PopupDialogCallback popupDialogCallback) {
new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.location_turned_off))
.setMessage(msg)
.setPositiveButton(context.getString(R.string.go_to_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
popupDialogCallback.hasClicked();
}
}).show();
}
}
android.support.test.espresso.NoMatchingViewException:在层次结构中找不到匹配的 View :文本:是“GA NAAR INSTELLINGEN”
最佳答案
根据 StackOverflow
类似的问题:Check if a dialog is displayed with Espresso
你应该改变你的代码:
onView(withText("GA NAAR INSTELLINGEN")).perform(click());
到
onView(withText("GA NAAR INSTELLINGEN")))
.inRoot(isDialog()) // <---
.check(matches(isDisplayed()))
.perform(click());
如果它不起作用,请不要费心使用 long 和 Espresso
另一个很棒的 Google 仪器测试,称为 uiatomator
。
检查:http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html
示例代码:
// Initialize UiDevice instance
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Search for correct button in the dialog.
UiObject button = uiDevice.findObject(new UiSelector().text("GA NAAR INSTELLINGEN"));
if (button.exists() && button.isEnabled()) {
button.click();
}
希望对你有帮助
关于android - 如何使用 espresso 按下 AlertDialog 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39376856/