[英]Test a Snackbar with UiAutomator, is there a way?
I'm starting to study UIAutomator on Android. 我开始在Android上学习UIAutomator。 I created a simple poc project, inside this there are a very few elements : one button and one editText.
我创建了一个简单的poc项目,里面有很少的元素:一个按钮和一个editText。 The behaviour is quite simple: When I push the botton the message written in the editText appears in a snackBar.
行为很简单:当我按下按钮时,editText中写入的消息出现在snackBar中。
Now I want to make two simple test : 现在我想做两个简单的测试:
For point one I have done in this way : 对于第一点我用这种方式做了:
@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));
}
That is i'm watching the snackbar's action to check if the snack bar is correctly opened . 那就是我正在观看小吃店检查小吃店是否正确打开的动作。 Are there the better ways to do that?
有更好的方法吗? In this way if there are more elements named in the same way of the snackbar's action I will have a problem
通过这种方式,如果有更多的元素以与snackbar的动作相同的方式命名,我将遇到问题
For the second point I don't find a way to test it. 对于第二点,我找不到测试它的方法。 I have to use only uiAutomator and not Espresso :)
我只能使用uiAutomator而不是Espresso :)
Thanks to everyone :) 谢谢大家 :)
I just tried it in a new android project: I have one Button in the main layout and show the snackbar on button click like this: 我刚试了一个新的android项目:我在主布局中有一个Button,并按下按钮点击快餐栏,如下所示:
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();
}
});
In my test I then test it like this: 在我的测试中,我然后测试它:
//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));
and the same with using UI Automator: 与使用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!");
}
Both tests are working just fine! 两个测试都运行得很好! Another way to select the Snackbar text would be via the resource id like this:
选择Snackbar文本的另一种方法是通过资源ID,如下所示:
//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"));
The way you did it also works but is deprecated so I would not use it, as the documentation says: 你做它的方式也有效但不推荐使用,所以我不会使用它,因为文档说:
* @deprecated Use {@link UiDevice#findObject(UiSelector)} instead. This version hides
* UiObject's dependency on UiDevice and is prone to misuse.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.