Cha*_*ham 38 java android android-testing android-espresso
我正在尝试检查Espresso是否显示视图.这是一些伪代码来显示我正在尝试的内容:
if (!Espresso.onView(withId(R.id.someID)).check(doesNotExist()){
// then do something
} else {
// do nothing, or what have you
}
Run Code Online (Sandbox Code Playgroud)
但我的问题是.check(doesNotExist())不返回布尔值.这只是一个断言.使用UiAutomator,我能够做到这样的事情:
if (UiAutomator.getbyId(SomeId).exists()){
.....
}
Run Code Online (Sandbox Code Playgroud)
Val*_*rov 79
测试中的条件逻辑是不合需要的.考虑到这一点,Espresso的API旨在引导测试作者远离它(通过明确的测试操作和断言).
话虽如此,您仍然可以通过实现自己的ViewAction并将isDisplayed检查(在perform方法内)捕获到AtomicBoolean来实现上述目的.
另一个不太优雅的选项 - 捕获失败检查引发的异常:
try {
onView(withText("my button")).check(matches(isDisplayed()));
//view is displayed logic
} catch (NoMatchingViewException e) {
//view not displayed logic
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我们需要这个功能,最后我在下面实现它:
https://github.com/marcosdiez/espresso_clone
if(onView(withText("click OK to Continue")).exists()){
doSomething();
} else {
doSomethingElse();
}
Run Code Online (Sandbox Code Playgroud)
我希望它对你有用.
小智 9
您也可以使用以下代码进行检查.如果显示视图,它将单击否则将传递.
onView(withText("OK")).withFailureHandler(new FailureHandler() {
@Override
public void handle(Throwable error, Matcher<View> viewMatcher){
}
}).check(matches(isDisplayed())).perform(customClick());
Run Code Online (Sandbox Code Playgroud)
我认为模仿UIAutomator你可以这样做:
(.虽然,我建议重新考虑你的方法有没有条件)
ViewInteraction view = onView(withBlah(...)); // supports .inRoot(...) as well
if (exists(view)) {
view.perform(...);
}
@CheckResult
public static boolean exists(ViewInteraction interaction) {
try {
interaction.perform(new ViewAction() {
@Override public Matcher<View> getConstraints() {
return any(View.class);
}
@Override public String getDescription() {
return "check for existence";
}
@Override public void perform(UiController uiController, View view) {
// no op, if this is run, then the execution will continue after .perform(...)
}
});
return true;
} catch (AmbiguousViewMatcherException ex) {
// if there's any interaction later with the same matcher, that'll fail anyway
return true; // we found more than one
} catch (NoMatchingViewException ex) {
return false;
} catch (NoMatchingRootException ex) {
// optional depending on what you think "exists" means
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
也exists没有分支可以实现非常简单:
onView(withBlah()).check(exists()); // the opposite of doesNotExist()
public static ViewAssertion exists() {
return matches(anything());
}
Run Code Online (Sandbox Code Playgroud)
虽然大部分时间都值得检查matches(isDisplayed()).
为什么没有人提到:
onView(withId(R.id.some_view_id)).check(matches(not(doesNotExist())))
Run Code Online (Sandbox Code Playgroud)
not只需在 doesNotExist 之前添加即可。但如果您经常使用此逻辑,最好使用自定义匹配器。
基于Dhiren Mudgil的回答,我最终编写了以下方法:
public static boolean viewIsDisplayed(int viewId)
{
final boolean[] isDisplayed = {true};
onView(withId(viewId)).withFailureHandler(new FailureHandler()
{
@Override
public void handle(Throwable error, Matcher<View> viewMatcher)
{
isDisplayed[0] = false;
}
}).check(matches(isDisplayed()));
return isDisplayed[0];
}
Run Code Online (Sandbox Code Playgroud)
我正在使用它来帮助确定当前在ViewFlipper中显示哪个视图。
| 归档时间: |
|
| 查看次数: |
29742 次 |
| 最近记录: |