Espresso 自动化测试(九)-inRoot 使用

来源:互联网 发布:windows安装ruby环境 编辑:程序博客网 时间:2024/06/03 16:48

在日常的测试中,我们还可能碰到许许多多的测试点,如Toast内容的验证,AutoCompleteText的选择等。 这些测试项都有一个共同的特点。即不在主UI布局的结构(layout,及其include的layout)之中,是不能直接定位的。 所以这里就需要使用inRoot( ) 了。

Toast 测试

众所周知UiAutomator是不支持Toast的验证的,为此很多人都想了很多的办法,结果当然都是有些无奈的。 Espresso 很简单的就解决了此问题。

我们还是继续使用之前的栗子,只是说我们点击书籍的时候不进入到bookDetailActivity界面,而是弹出对应bookTitle的Toast。 代码不贴了,很简单。我们看看要如何验证Toast的内容呢。

onData(withBookTitle("Effective Java ")).inAdapterView(allOf(withId(R.id.list),isDisplayed())).perform(click());onView(withText("Effective Java ")).inRoot(withDecorView(not(mRules.getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));

以上就是测试的代码了。很简单。这个代码还是有点不太明白,后续会跟进下。

AutoComplete 测试

这个测试项 可以参考 MultiWindowSample

 @Test    public void autoCompleteTextView_oneSuggestion() {        // Type "South" to trigger one suggestion.        onView(withId(R.id.auto_complete_text_view))                .perform(typeTextIntoFocusedView("South "), closeSoftKeyboard());        // Should be displayed        onView(withText("South China Sea"))                .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView()))))                .check(matches(isDisplayed()));        // Should not be displayed.        onView(withText("Southern Ocean"))                .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView()))))                .check(doesNotExist());    }

栗子都已经很详细了。这里就不说了。

结束语

Toast的验证解决。。。

0 0
原创粉丝点击