Java 用UiAutomator测试Snackbar,有办法吗?
我开始学习Android上的UIAutomator。 我创建了一个简单的poc项目,里面有几个元素:一个按钮和一个编辑文本。 行为非常简单: 当我按下按钮时,编辑文本中的信息会出现在一个快捷键中 现在我想做两个简单的测试:Java 用UiAutomator测试Snackbar,有办法吗?,java,android,user-interface,android-uiautomator,Java,Android,User Interface,Android Uiautomator,我开始学习Android上的UIAutomator。 我创建了一个简单的poc项目,里面有几个元素:一个按钮和一个编辑文本。 行为非常简单: 当我按下按钮时,编辑文本中的信息会出现在一个快捷键中 现在我想做两个简单的测试: 查看snackbar是否正确显示 查看editTest消息是否正确 在snackbar中正确报告 对于第一点,我是这样做的: @Test public void pressEmailButton() throws UiObjectNotFoundException {
@Test
public void pressEmailButton() throws UiObjectNotFoundException {
mDevice.findObject( By.res(POC_PACKAGE,"fab") ).click();
// wait for the snackBar appear
UiObject snackBar2 = new UiObject (new UiSelector().text("Send"));
// Verify the snackBarIsShowed is displayed in the Ui
assertTrue("Timeout while snackbar", snackBar2.waitForExists(1000));
}
也就是说,我正在观察小吃店的动作,以检查小吃店是否正确打开。有更好的方法吗?这样,如果有更多的元素以与snackbar操作相同的方式命名,我将遇到问题
对于第二点,我没有找到一种方法来测试它。
我只能用自动咖啡机,不能用浓缩咖啡:)
感谢大家:)我刚刚在一个新的android项目中试用过:我在主布局中有一个按钮,并在按钮点击时显示snackbar,如下所示:
button = (Button) findViewById(R.id.button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentLayout = findViewById(R.id.root_layout);
Snackbar.make(parentLayout, "Snackbar Text", Snackbar.LENGTH_LONG)
.show();
}
});
//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));
//replace the package name in the next line with your package name
UiObject snackbarTextView = mDevice.findObject(new UiSelector().resourceId("com.example.testespressoapplication:id/snackbar_text"));
在我的测试中,我会这样测试:
button = (Button) findViewById(R.id.button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentLayout = findViewById(R.id.root_layout);
Snackbar.make(parentLayout, "Snackbar Text", Snackbar.LENGTH_LONG)
.show();
}
});
//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));
//replace the package name in the next line with your package name
UiObject snackbarTextView = mDevice.findObject(new UiSelector().resourceId("com.example.testespressoapplication:id/snackbar_text"));
与使用UI Automator相同:
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject snackbarTextView = mDevice.findObject(new UiSelector().text("Snackbar Text"));
if(!snackbarTextView.getText().equals("Snackbar Text")) {
throw new RuntimeException("No snackbar!");
}
两个测试都很好!
选择Snackbar文本的另一种方法是通过如下所示的资源id:
button = (Button) findViewById(R.id.button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentLayout = findViewById(R.id.root_layout);
Snackbar.make(parentLayout, "Snackbar Text", Snackbar.LENGTH_LONG)
.show();
}
});
//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));
//replace the package name in the next line with your package name
UiObject snackbarTextView = mDevice.findObject(new UiSelector().resourceId("com.example.testespressoapplication:id/snackbar_text"));
您这样做的方式也可以,但已被弃用,因此我不会使用它,正如文档所述:
* @deprecated Use {@link UiDevice#findObject(UiSelector)} instead. This version hides
* UiObject's dependency on UiDevice and is prone to misuse.
你能把你的活动图片或版式上传给我吗?这不是出现在uiautomatorviewer上的小便条吗?它必须是uiautomator还是您也可以使用浓缩咖啡支票?