Grz*_*ski 11 java android android-espresso
我在YT Advanced Android Espresso上找到了很棒的乐器测试教程.我从那里拿了代码,对我的需求进行了小调整.
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withChild;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;
...
@Test
public void checkToolbarTitle() {
String toolbarTitile = getInstrumentation().getTargetContext().getString(R.string.my_bus_stops);
onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))).check(matches(withText(toolbarTitile)));
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它对我不起作用.测试失败:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (is assignable from class: class android.widget.TextView and has parent matching: is assignable from class: class android.widget.Toolbar)
Run Code Online (Sandbox Code Playgroud)
这有什么问题?我该如何以其他方式测试?
den*_*nys 23
这对我有用:
onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar))))
.check(matches(withText("toolbarTitile")));
Run Code Online (Sandbox Code Playgroud)
解
方法很好.正如Chiu-Ki Chan在她的教程中所写,你可以"找出一个特定的观点". 但是你必须确保你导入了适当的工具栏:
import android.support.v7.widget.Toolbar;
Run Code Online (Sandbox Code Playgroud)
代替:
import android.widget.Toolbar;
Run Code Online (Sandbox Code Playgroud)
这是另一种选择(和更惯用的方法):
onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText(toolbarTitle))))
Run Code Online (Sandbox Code Playgroud)
我不记得这是我自己写的,还是我在某处找到的,但这是我检查工具栏标题的方式:
public static Matcher<View> withToolbarTitle(CharSequence title) {
return withToolbarTitle(is(title));
}
public static Matcher<View> withToolbarTitle(final Matcher<CharSequence> textMatcher) {
return new BoundedMatcher<View, Toolbar>(Toolbar.class) {
@Override
public boolean matchesSafely(Toolbar toolbar) {
return textMatcher.matches(toolbar.getTitle());
}
@Override
public void describeTo(Description description) {
description.appendText("with toolbar title: ");
textMatcher.describeTo(description);
}
};
}
Run Code Online (Sandbox Code Playgroud)
这适用于所有情况。示例断言:onView(withId(R.id.toolbar)).check(matches(withToolbarTitle("title")));
如果您使用的是ActionBar,而不是工具栏,请使用以下命令:
onView(allOf(instanceOf(TextView.class),
withParent(withResourceName("action_bar"))))
.check(matches(withText("My ActionBar title")));
Run Code Online (Sandbox Code Playgroud)
注意:要快速添加这些方法的导入,请将闪烁的光标放在未解决的方法上,然后执行Android Studio?救命 ?寻找动作?寻找"show intention action"?单击结果选项?将会出现一个弹出窗口?点击"Import static method ..."。您也可以将键盘快捷方式分配给“显示意图动作”。更多信息在这里。另一种方法是"Add unambiguous imports on the fly"在“设置”中启用。
| 归档时间: |
|
| 查看次数: |
4773 次 |
| 最近记录: |