【发布时间】:2016-04-28 04:00:30
【问题描述】:
当我尝试与蓝牙设备配对时,会出现带有 PIN 码的系统确认对话框。有“取消”和“确定”按钮。但我不能用 Robotium 点击它们。如何在 Robotium 中使用 Android 操作系统对话框? 谢谢。
【问题讨论】:
-
系统对话框不是应用程序窗口或视图层次结构的一部分。
当我尝试与蓝牙设备配对时,会出现带有 PIN 码的系统确认对话框。有“取消”和“确定”按钮。但我不能用 Robotium 点击它们。如何在 Robotium 中使用 Android 操作系统对话框? 谢谢。
【问题讨论】:
这对我有用:
solo.clickOnView(solo.getView(android.R.id.button1));
“正”按钮是 android.R.id.button1,“负”按钮是 android.R.id.button2,“中性”按钮是 android.R.id.button3。
【讨论】:
不可能编写跨越 2 个应用程序的测试用例。
但是,如果它是同一个应用程序的一部分,那么您可以使用solo.clickOnText("Cancel")。同样的方法,您可以通过点击其他按钮的文本来点击它们。
【讨论】:
Robotium 在涉及到 Android 系统对话框按钮时可能会很困难,但是有一个解决方案。
Found the answer on this post on stack
// Set this dependency in your app's gradle file
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
并在您的测试项目中使用此代码 sn-p:
// Initialize UiDevice instance
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Search for correct button in the dialog.
UiObject button = uiDevice.findObject(new UiSelector().text("ButtonTest"));
if (button.waitForExists(5000)) {
button.click();
}
【讨论】:
正如@kamal_prd 所说,你不能因为对话框不是同一个应用程序的一部分。 也许你可以使用
clickOnScreen(float x, float y) //点击指定坐标
我知道使用不同的屏幕分辨率/尺寸很难管理,但我也用它来测试我的应用程序。
【讨论】:
你可以使用solo.clickOnView(solo.getView(buttonId))
【讨论】: