Skip to content

PoC to demonstrate instrumented tests using RobotPattern

Notifications You must be signed in to change notification settings

jonvieira/robot-pattern-ui-tests

Repository files navigation

Robot Pattern UI Test

PoC to demonstrate instrumented tests using RobotPattern. This PoC has only a login screen with local validation, an initial view and a fictitious account creation view, just to demonstrate the tests with navigation between the views.

Screenshot

Code sample

LoginActivityUITest

    @Test
    fun loginSuccessfully() {
        login {
            typeEmail(validEmail)
            typePassword(validPassword)
            clickLoginButton()
            Intents.intended(hasComponent(DashboardActivity::class.java.name))
        }
    }

    @Test
    fun loginWithWrongEmail() {
        login {
            typeEmail(invalidEmail)
            typePassword(validPassword)
            clickLoginButton()
            checkAlertDialog(R.string.attention)
        }
    }

    @Test
    fun loginWithWrongPassword() {
        login {
            typeEmail(validEmail)
            typePassword(invalidPassword)
            clickLoginButton()
            checkAlertDialog(R.string.attention)
        }
    }

    @Test
    fun checkIfGoToCreateAccount() {
        login {
            clickCreateAccountButton()
            Intents.intended(hasComponent(CreateAccountActivity::class.java.name))
        }
    }

LoginRobot

    fun login(func: LoginRobot.() -> Unit) = LoginRobot().apply { func() }

    class LoginRobot : BaseUITest() {
        fun typeEmail(email: String) = fillEditText(R.id.editTextEmail, email)
        fun typePassword(pass: String) = fillEditText(R.id.editTextPassword, pass)
        fun clickLoginButton() = clickButton(R.id.buttonLogin)
        fun clickCreateAccountButton() = clickButton(R.id.textViewNewAccount)
        fun checkAlertDialog(@IdRes dialogTitle: Int) = alertDialog(dialogTitle)
    }

BaseUITest

    open class BaseUITest {
        fun fillEditText(@IdRes idRes: Int, text: String): ViewInteraction =
            onView(withId(idRes))
                .perform(typeText(text), closeSoftKeyboard())

        fun clickButton(@IdRes idRes: Int): ViewInteraction =
            onView(withId(idRes))
                .perform(click())

    fun alertDialog(@IdRes idRes: Int): ViewInteraction =
        onView(withText(idRes))
            .inRoot(isDialog())
            .check(matches(isDisplayed()))
}

About

PoC to demonstrate instrumented tests using RobotPattern

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages