Upgrade to Pro — share decks privately, control downloads, hide ads and more …

One way to organize UI tests (with Robots)

Daniel
September 06, 2017

One way to organize UI tests (with Robots)

Daniel

September 06, 2017

Transcript

17 slides
  1. A way to organize UI tests Daniel Gomez Rico Android

    Lead Developer at Barista Ventures @aquicaipivara With UI Robots…
  2. @Test
 fun test_login_showMainView() {
 Espresso.onView(ViewMatchers.withId(R.id.loginUsernameEditText))
 .perform(ViewActions.typeText("[email protected]"), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginPasswordEditText))
 .perform(ViewActions.typeText("12345678"),

    ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginSubmitButton)).perform(ViewActions.click())
 
 Espresso.onView(ViewMatchers.withText("Login"))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
 } Without robots
  3. @Test
 fun test_login_showMainView() {
 Espresso.onView(ViewMatchers.withId(R.id.loginUsernameEditText))
 .perform(ViewActions.typeText("[email protected]"), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginPasswordEditText))
 .perform(ViewActions.typeText("12345678"),

    ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginSubmitButton)) .perform(ViewActions.click())
 
 Espresso.onView(ViewMatchers.withText("Login"))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
 } Without robot Sample 1/3
  4. @Test
 fun test_login_showMainView() {
 Espresso.onView(ViewMatchers.withId(R.id.loginUsernameEditText))
 .perform(ViewActions.typeText("[email protected]"), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginPasswordEditText))
 .perform(ViewActions.typeText(“007"),

    ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginSubmitButton)) .perform(ViewActions.click())
 
 Espresso.onView(ViewMatchers.withText(“Main"))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
 } MainRobot Looks like actions from a Login View -> LoginRobot
  5. class LoginTests { @Test
 fun test_login_showMainView() {
 val mainRobot =

    loginRobot.writeEmail("[email protected]")
 .writePassword(“007")
 .clickLoginButton() // Returns MainRobot
 
 mainRobot.checkIsVisible()
 } } With robots… Sample 1/3
  6. class MainTests {
 
 @Test
 fun test_onLogout_showLoginView() {
 val mainRobot

    = loginRobot.performLoginFlow() //return MainRobot
 
 mainRobot.clickLogoutButton()
 
 loginRobot.checkIsVisible()
 } 
 } With robots… Sample 2/3
  7. class LoginTestsWithoutRobot {
 
 @Test
 fun test_login_showMainView() {
 Espresso.onView(ViewMatchers.withId(R.id.loginUsernameEditText))
 .perform(ViewActions.typeText("[email protected]"),

    ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginPasswordEditText))
 .perform(ViewActions.typeText("12345678"), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginSubmitButton)).perform(ViewActions.click())
 
 Espresso.onView(ViewMatchers.withText("Login"))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
 }
 
 @Test
 fun test_login_withEmptyEmail_showsError() {
 Espresso.onView(ViewMatchers.withId(R.id.loginUsernameEditText))
 .perform(ViewActions.typeText(""), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginPasswordEditText))
 .perform(ViewActions.typeText("12345678"), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginSubmitButton)).perform(ViewActions.click())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginUsernameEditText))
 .check(ViewAssertions.matches(CustomMatchers.withError()))
 }
 
 @Test
 fun test_login_withEmptyPassword_showsError() {
 Espresso.onView(ViewMatchers.withId(R.id.loginUsernameEditText))
 .perform(ViewActions.typeText("[email protected]"), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginPasswordEditText))
 .perform(ViewActions.typeText(""), ViewActions.closeSoftKeyboard())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginSubmitButton)).perform(ViewActions.click())
 
 Espresso.onView(ViewMatchers.withId(R.id.loginPasswordEditText))
 .check(ViewAssertions.matches(CustomMatchers.withError()))
 }
 
 } Without robot Sample 3/3
  8. @Test
 fun test_login_showMainView() {
 val mainRobot = loginRobot.writeEmail("[email protected]")
 .writePassword("007")
 .clickLoginButton()

    // Returns MainRobot
 
 mainRobot.checkIsVisible()
 }
 
 @Test
 fun test_login_withEmptyEmail_showsError() {
 loginRobot.writeEmail("")
 .writePassword("123")
 .clickLoginButton()
 
 loginRobot.checkEmailHaveError()
 }
 
 @Test
 fun test_login_withEmptyPassword_showsError() {
 loginRobot.writeEmail("[email protected]")
 .writePassword("")
 .clickLoginButton()
 
 loginRobot.checkPasswordHaveError()
 }
 With robots… Sample 3/3
  9. class NewQuestionActivityTest {
 
 @Test
 fun newQuestionTest() {
 val mainRobot

    = onBoardingRobot.clickLoginButton()
 .performLoginFlow()
 val newQuestionRobot = mainRobot.clickNewQuestionFab() 
 val creditCardRobot = newQuestionRobot.writeDescription(getRandomQuestion())
 .selectSubtopic(0)
 .selectExpert(0)
 .clickSubmitButton()
 .clickRushPaymentOption()
 .clickOkButton()
 
 val mainRobot = creditCardRobot.performSuccessFlow()
 
 mainRobot.validateIsVisible()
 }
 } With robots… “Big” test
  10. fun selectSubtopic(index: Int) {
 click(R.id.newQuestionSubtopicsEditText)
 clickListItemDialogItem(index)
 }
 fun click(@IdRes id:

    Int) {
 onView(withId(id)).perform(ViewActions.click())
 }
 private fun clickListItemDialogItem(index: Int) {
 val action = RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(index,
 ViewActions.click())
 Espresso.onView(ViewMatchers.withId(R.id.checkDialogRecyclerView)).perform(action)
 clickViewWithText("OK")
 }
 protected fun clickViewWithText(text: String) {
 screenshot("before_click")
 onView(allOf<View>(withText(text), isDisplayed())).perform(ViewActions.click())
 screenshot("click")
 }

  11. - https://academy.realm.io/posts/kau-jake-wharton-testing-robots/ - Android Dialogs (Sam Edwards: Espresso Robots +

    Screenshots): https://www.youtube.com/watch?v=vsrX0gGZp3o - Sample: https://github.com/caipivara/androidmeetup-ui-robots - Icons: https://www.iconfinder.com/icons/58995/ lionel_preacherbot_robot_icon References

More Decks by Daniel

Other Decks in Programming