Espresso Testing Framework - View Assertions



As discussed earlier, view assertion is used to assert that both the actual view (found using view matchers) and expected views are the same. A sample code is as follows,

onView(withId(R.id.my_view)) .check(matches(withText("Hello")))

Here,

  • onView() returns ViewInteration object corresponding to matched view. ViewInteraction is used to interact with matched view.

  • withId(R.id.my_view) returns a view matcher that will match with the view (actual) having id attributes equals to my_view.

  • withText(“Hello”) also returns a view matcher that will match with the view (expected) having text attributes equals to Hello.

  • check is a method which accepts an argument of type ViewAssertion and do assertion using passed in ViewAssertion object.

  • matches(withText(“Hello”)) returns a view assertion, which will do the real job of asserting that both actual view (found using withId) and expected view (found using withText) are one and the same.

Let us learn some of the methods provided by espresso testing framework to assert view objects.

doesNotExist()

Returns a view assertion, which ensures that the view matcher does not find any matching view.

onView(withText("Hello")) .check(doesNotExist());

Here, the test case ensures that there is no view with text Hello.

matches()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and matches with the view matched by the target view matcher.

onView(withId(R.id.textView_hello)) .check(matches(withText("Hello World!")));

Here, the test case ensures that the view having id, R.id.textView_hello exists and matches with the target view with text Hello World!

isBottomAlignedWith()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is bottom aligned with the target view matcher.

onView(withId(R.id.view)) .check(isBottomAlignedWith(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is bottom aligned with view having id, R.id.target_view.

isCompletelyAbove()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned completely above the target view matcher.

onView(withId(R.id.view)) .check(isCompletelyAbove(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned completely above the view having id, R.id.target_view

isCompletelyBelow()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned completely below the target view matcher.

onView(withId(R.id.view)) .check(isCompletelyBelow(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned completely below the view having id, R.id.target_view.

isCompletelyLeftOf()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned completely left of the target view matcher.

onView(withId(R.id.view)) .check(isCompletelyLeftOf(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned completely left of view having id, R.id.target_view

isCompletelyRightOf()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned completely right of the target view matcher.

onView(withId(R.id.view)) .check(isCompletelyRightOf(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned completely right of the view having id, R.id.target_view.

isLeftAlignedWith()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is left aligned with the target view matcher.

onView(withId(R.id.view)) .check(isLeftAlignedWith(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is left aligned with view having id, R.id.target_view

isPartiallyAbove()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned partially above the target view matcher.

onView(withId(R.id.view)) .check(isPartiallyAbove(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned partially above the view having id, R.id.target_view

isPartiallyBelow()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned partially below the target view matcher.

onView(withId(R.id.view)) .check(isPartiallyBelow(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned partially below the view having id, R.id.target_view.

isPartiallyLeftOf()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned partially left of the target view matcher.

onView(withId(R.id.view)) .check(isPartiallyLeftOf(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned partially left of view having id, R.id.target_view.

isPartiallyRightOf()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is positioned partially right of the target view matcher

onView(withId(R.id.view)) .check(isPartiallyRightOf(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is positioned partially right of view having id, R.id.target_view.

isRightAlignedWith()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is right aligned with the target view matcher.

onView(withId(R.id.view)) .check(isRightAlignedWith(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is right aligned with view having id, R.id.target_view.

isTopAlignedWith()

Accepts a target view matcher and returns a view assertion, which ensures that the view matcher (actual) exists and is top aligned with the target view matcher.

onView(withId(R.id.view)) .check(isTopAlignedWith(withId(R.id.target_view)))

Here, the test case ensures that the view having id, R.id.view exists and is top aligned with view having id, R.id.target_view

noEllipsizedText()

Returns a view assertion, which ensures that the view hierarchy does not contain ellipsized or cut off text views.

onView(withId(R.id.view)) .check(noEllipsizedText());

noMultilineButtons()

Returns a view assertion, which ensures that the view hierarchy does not contain multi line buttons.

onView(withId(R.id.view)) .check(noMultilineButtons());

noOverlaps()

Returns a view assertion, which ensures that the descendant object assignable to TextView or ImageView does not overlap each other. It has another option, which accepts a target view matcher and returns a view assertion, which ensures that the descendant view matching the target view do not overlap.

Advertisements