0

我的应用程序获取搜索结果并将其放在列表视图中,但我正在尝试测试它的结果。 这是搜索结果页面的截图。我的项目显示代码如下,以及我试图用来测试数据的代码。

final AlertDialog.Builder builder = new AlertDialog.Builder(SearchItemsActivity.this);
                        ListView listView = findViewById(R.id.listView);
                        listView.setAdapter(listViewAdapter);

onData(anything()).inAdapterView(withId(R.id.listView)).atPosition(1)
            .check(matches(withText("Harry Potter Books - "+"In Stock"+"\n"+"Email: test12345@test.com\n"+"Full hardcover series\n")));

我得到的错误代码是

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Harry Potter Books - In Stock\nEmail: test12345@test.com\nFull hardcover series\n"' doesn't match the selected view.                                                                                   
 Expected: with text: is "Harry Potter Books - In Stock\nEmail: test12345@test.com\nFull hardcover series\n"
 Got: "AppCompatTextView{id=16908308, res-name=text1, visibility=VISIBLE, width=1080, height=215, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.AbsListView$LayoutParams@ce20b7f, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Harry Potter Books - In Stock\nEmail: test12345@gmail.com\nFull hardcover series\n, input-type=0, ime-target=false, has-links=false}"
4

1 回答 1

0

使用 Barista 库进行 UI 测试,它是最简单的解决方案,让一切变得更容易。https://github.com/AdevintaSpain/Barista

使用 Barista,您可以这样做:

assertDisplayedAtPosition(R.id.list, 0, "text");
assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, "text");
assertDisplayedAtPosition(R.id.list, 0, R.string.hello_world);
assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, R.string.hello_world);
assertCustomAssertionAtPosition(R.id.list, 0, customViewAssertion);

你可以在 Github 页面上找到更多信息,它涵盖了你需要的一切。

于 2020-12-08T13:38:13.040 回答