【问题标题】:Test a Snackbar with UiAutomator, is there a way?用 UiAutomator 测试一个 Snackbar,有没有办法?
【发布时间】:2017-06-26 04:15:56
【问题描述】:

我开始研究 Android 上的 UIAutomator。 我创建了一个简单的 poc 项目,里面有很少的元素:一个按钮和一个 editText。 行为非常简单: 当我按下按钮时,editText 中写入的消息会出现在snackBar 中。

现在我想做两个简单的测试:

  1. 查看小吃栏是否正确显示
  2. 查看editTest消息是否 在快餐栏中正确报告

对于第一点,我以这种方式完成了:

 @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的动作相同的方式命名我会遇到问题

对于第二点,我没有找到测试它的方法。 我只能使用 uiAutomator 而不是 Espresso :)

谢谢大家:)

【问题讨论】:

  • 您能发布您的活动图像或布局层次结构吗?不就是出现uiautomatorviewer的小吃吧吗?
  • 它必须是 uiautomator 还是你也可以使用 espresso 检查?

标签: java android user-interface android-uiautomator


【解决方案1】:

我刚刚在一个新的 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();
        }
});

在我的测试中,我然后像这样测试它:

//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));

使用 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,如下所示:

//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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 2022-10-20
    • 2015-08-09
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多