Espresso Testing Framework - View Actions


Advertisements

As learned earlier, view actions automate all the possible actions performable by users in an android application. Espresso onView and “onData” provides the perform method, which accepts view actions and invokes/automates the corresponding user actions in the test environment. For example, “click()” is a view action, which when passed to the onView(R.id.myButton).perform(click()) method, will fire the click event of the button (with id: “myButton”) in the testing environment.

In this chapter, let us learn about the view actions provided by espresso testing framework.

typeText()

typeText() accepts one argument (text) of type String and returns a view action. The returned view action types the provided text into the view. Before placing the text, it taps the view once. The content may be placed at arbitrary position if it contains text already.

onView(withId(R.id.text_view)).perform(typeText("Hello World!"))

typeTextIntoFocusedView()

typeTextIntoFocusedView() is similar to typeText() except that it places the text right next to the cursor position in the view.

onView(withId(R.id.text_view)).perform(typeTextIntoFocusedView("Hello World!"))

replaceText()

replaceText() is similar to typeText() except that it replaces the content of the view.

onView(withId(R.id.text_view)).perform(typeTextIntoFocusedView("Hello World!"))

clearText()

clearText() has no arguments and returns a view action, which will clear the text in the view.

onView(withId(R.id.text_view)).perform(clearText())

pressKey()

pressKey() accepts key code (e.g KeyEvent.KEYCODE_ENTER) and returns a view action, which will press the key corresponds to the key code.

onView(withId(R.id.text_view)).perform(typeText(
   "Hello World!", pressKey(KeyEvent.KEYCODE_ENTER))

pressMenuKey()

pressMenuKey() has no arguments and returns a view action, which will press the hardware menu key.

onView(withId(R.id.text_view)).perform(typeText(
   "Hello World!", pressKey(KeyEvent.KEYCODE_ENTER), pressMenuKey())

closeSoftKeyboard()

closeSoftKeyboard() has no arguments and returns a view action, which will close the keyboard, if one is opened.

onView(withId(R.id.text_view)).perform(typeText(
   "Hello World!", closeSoftKeyboard())

click()

click() has no arguments and returns a view action, which will invoke the click action of the view.

onView(withId(R.id.button)).perform(click())

doubleClick()

doubleClick() has no arguments and returns a view action, which will invoke the double click action of the view.

onView(withId(R.id.button)).perform(doubleClick())

longClick()

longClick() has no arguments and returns a view action, which will invoke the long click action of the view.

onView(withId(R.id.button)).perform(longClick())

pressBack()

pressBack() has no arguments and returns a view action, which will click the back button.

onView(withId(R.id.button)).perform(pressBack())

pressBackUnconditionally()

pressBackUnconditionally() has no arguments and returns a view action, which will click the back button and does not throw an exception if the back button action exits the application itself.

onView(withId(R.id.button)).perform(pressBack())

openLink()

openLink() has two arguments. The first argument (link text) is of type Matcher and refers the text of the HTML anchor tag. The second argument (url) is of the type Matcher and refers the url of the HTML anchor tag. It is applicable for TextView only. It returns a view action, which collects all the HTML anchor tags available in the content of the text view, finds the anchor tag matching the first argument (link text) and the second argument (url) and finally opens the corresponding url. Let us consider a text view having the content as −

<a href="http://www.google.com/">copyright</a>

Then, the link can be opened and tested using the below test case,

onView(withId(R.id.text_view)).perform(openLink(is("copyright"),
   is(Uri.parse("http://www.google.com/"))))

Here, openLink will get the content of the text view, find the link having copyright as text, www.google.com as url and open the url in a browser.

openLinkWithText()

openLinkWithText() has one argument, which may be either of type **String* or Matcher. It is simply a short cut to the openLink *method.

onView(withId(R.id.text_view)).perform(openLinkWithText("copyright"))

openLinkWithUri()

openLinkWithUri() has one argument, which may be either of type String or Matcher. It is simply a short cut to the openLink* method.

onView(withId(R.id.text_view)).perform(openLinkWithUri("http://www.google.com/"))

pressImeActionButton()

pressImeActionButton() has no arguments and returns a view action, which will execute the action set in android:imeOptions configuration. For example, if the android:imeOptions equals actionNext, this will move the cursor to next possible EditText view in the screen.

onView(withId(R.id.text_view)).perform(pressImeActionButton())

scrollTo()

scrollTo() has no arguments and returns a view action, which will scroll the matched scrollView on the screen.

onView(withId(R.id.scrollView)).perform(scrollTo())

swipeDown()

swipeDown() has no arguments and returns a view action, which will fire swipe down action on the screen.

onView(withId(R.id.root)).perform(swipeDown())

swipeUp()

swipeUp() has no arguments and returns a view action, which will fire swipe up action on the screen.

onView(withId(R.id.root)).perform(swipeUp())

swipeRight()

swipeRight() has no arguments and returns a view action, which will fire swipe right action on the screen.

onView(withId(R.id.root)).perform(swipeRight())

swipeLeft()

swipeLeft() has no arguments and returns a view action, which will fire swipe left action on the screen.

onView(withId(R.id.root)).perform(swipeLeft())
Advertisements