简体   繁体   English

使用UiAutomator测试Snackbar,有办法吗?

[英]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 : 现在我想做两个简单的测试:

  1. see if the snackbar correctly appears 看看小吃吧是否正确出现
  2. see if the editTest message is correctly reported in the snackbar 查看是否在快餐栏中正确报告了editTest消息

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.

相关问题 如何测试android中是否显示Snackbar - How to test if Snackbar is shown in android uiautomator的运行测试期间未找到类 - class not found during run test with uiautomator 将uiautomator测试用例与Android中的应用程序代码集成 - Integrating uiautomator test case with application code in Android 如何在硒运行测试时显示Snackbar? - How to display Snackbar while selenium is running test? "有没有办法在android中删除\/透明小吃店的背景颜色?" - Is there a way to remove/transparent background color of a snackbar in android? 以一种不错的方式使用 ConstraintLayout 时,如何在顶部的 Android 中显示 Snackbar - How to show a Snackbar in Android on the top when using ConstraintLayout in a nice way Appium-UIAutomator2-Java:相同的代码直接在测试中工作,在测试中调用的 function 中不起作用 - Appium-UIAutomator2-Java: the same code works directly from test and doesn't work from a function called in test recyclerview 中的小吃店 - Snackbar in recyclerview Uncaught Exception Handler - UiAutomator - Uncaught Exception Handler - UiAutomator Android:uiautomator中的外部文件 - Android: External file in uiautomator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM