Issue
I have an Activity which has two Views - which are shown depending on the network condition.
LinearLayout recyclerView = (LinearLayout) findViewById(R.id.recycler_view);
LinearLayout errorParent = (LinearLayout) findViewById(R.id.error_parent);
if(Utils.isNetworkAvailable()){
recyclerView.setVisibility(View.VISIBLE);
errorParent.setVisibility(View.GONE);
}else{
recyclerView.setVisibility(View.GONE);
errorParent.setVisibility(View.VISIBLE);
}
I have written an Espresso test for testing Network conditions. Before running the test, I manually switch off the Internet.
ViewInteraction viewInteraction = onView(withId(R.id.recycler_view));
viewInteraction.check(matches(isDisplayed()));
Since there is no Internet, the recycler_view is GONE, and thus it is not present in the view hierarchy. But when running the code I am not getting NoMatchingViewException. Ideally when the view is not present in the hierarchy, I should get the above exception. Instead I am getting AssertionFailedError.
What is the reason for this behavior?
Solution
The best solution here is to look into source code of Espresso. Let's trace down, what is happening.
When calling check(final ViewAssertion viewAssert) in this line, the view is checked whether it is in the hierarchy. The exception NoMatchingViewException is not caught, because viewFinder finds the view in the hierarchy. Then the code goes to assertion. So the code goes funther to ViewAssertion's function: matches(final Matcher<? super View> viewMatcher) with missingViewException being null.
In this line noViewException is null, therefore code goes to assertThat. The assertion is isDisplayed(), which measures global effective visibility of the view. This is where AssertionFailedError is thrown.
For asserting wherther the view is not in visible in the current layout another construction should be used:
onView({Matcher<View>}).check(doesNotExist());
or
onView({Matcher<View>}).check(matches(not(isDisplayed())));
More explanation about it here
Answered By - R. Zagórski
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.