Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save venkata-kari/9a62d3b83bb525dff9bf1bacaaebfa83 to your computer and use it in GitHub Desktop.

Select an option

Save venkata-kari/9a62d3b83bb525dff9bf1bacaaebfa83 to your computer and use it in GitHub Desktop.

Revisions

  1. @adavis adavis revised this gist Apr 18, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions BeforeLoginActivityTest.java
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,8 @@ public class BeforeLoginActivityTest {

    @Test
    public void shouldProceedLoginWhenValid () {
    onView( withId( R.id.login_username_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_password_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_username_entry ) ).perform( typeText( TEST_EMAIL ) );
    onView( withId( R.id.login_password_entry ) ).perform( typeText( TEST_PASSWORD ) );
    onView( withId( R.id.login_button ) ).perform( click() );

    intended( hasComponent( hasShortClassName( PACKAGE_NAME ) ) );
  2. @adavis adavis revised this gist Apr 18, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions BeforeLoginActivityTest.java
    Original file line number Diff line number Diff line change
    @@ -28,10 +28,10 @@ public class BeforeLoginActivityTest {

    @Test
    public void shouldProceedLoginWhenValid () {
    onView( withId( R.id.login_username_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_password_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_button ) ).perform( click() );
    onView( withId( R.id.login_username_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_password_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_button ) ).perform( click() );

    intended( hasComponent( hasShortClassName( PACKAGE_NAME ) ) );
    intended( hasComponent( hasShortClassName( PACKAGE_NAME ) ) );
    }
    }
  3. @adavis adavis revised this gist Apr 18, 2016. 1 changed file with 37 additions and 0 deletions.
    37 changes: 37 additions & 0 deletions BeforeLoginActivityTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    package <your_package>;

    import android.support.test.espresso.intent.rule.IntentsTestRule;
    import android.support.test.runner.AndroidJUnit4;
    import android.test.suitebuilder.annotation.LargeTest;

    import org.junit.After;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.runner.RunWith;

    import static android.support.test.espresso.intent.Intents.intended;
    import static android.support.test.espresso.intent.matcher.ComponentNameMatchers.hasShortClassName;
    import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
    import static org.junit.Assert.assertNotNull;

    @RunWith( AndroidJUnit4.class )
    @LargeTest
    public class BeforeLoginActivityTest {

    private static final String TEST_EMAIL = "[email protected]";
    private static final String TEST_PASSWORD = "123456";
    private static final String PACKAGE_NAME = "my.package";

    @Rule
    public IntentsTestRule<LoginActivity> activityRule =
    new IntentsTestRule<>( LoginActivity.class );

    @Test
    public void shouldProceedLoginWhenValid () {
    onView( withId( R.id.login_username_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_password_entry ) ).perform( typeText( text ) );
    onView( withId( R.id.login_button ) ).perform( click() );

    intended( hasComponent( hasShortClassName( PACKAGE_NAME ) ) );
    }
    }
  4. @adavis adavis revised this gist Apr 15, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LoginActivityTest.java
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ public void shouldHaveUsernameAndPasswordHeader () {
    }

    @Test
    public void shouldNotAllowLoginWhenInValid () {
    public void shouldNotAllowLoginWhenInvalid () {
    withRobot( LoginScreenRobot.class )
    .provideActivityContext( activityRule.getActivity() )
    .callLogin( "", "" )
  5. @adavis adavis revised this gist Apr 15, 2016. 1 changed file with 72 additions and 0 deletions.
    72 changes: 72 additions & 0 deletions LoginActivityTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    package <your_package>;

    import android.support.test.espresso.intent.rule.IntentsTestRule;
    import android.support.test.runner.AndroidJUnit4;
    import android.test.suitebuilder.annotation.LargeTest;

    import org.junit.After;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.runner.RunWith;

    import static android.support.test.espresso.intent.Intents.intended;
    import static android.support.test.espresso.intent.matcher.ComponentNameMatchers.hasShortClassName;
    import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
    import static <your_package>.ScreenRobot.withRobot;
    import static org.junit.Assert.assertNotNull;

    @RunWith( AndroidJUnit4.class )
    @LargeTest
    public class LoginActivityTest {

    private static final String TEST_EMAIL = "[email protected]";
    private static final String TEST_PASSWORD = "123456";
    private static final String PACKAGE_NAME = "my.package";

    @Rule
    public IntentsTestRule<LoginActivity> activityRule =
    new IntentsTestRule<>( LoginActivity.class );

    @Test
    public void shouldHaveUsernameAndPasswordHeader () {
    withRobot( LoginScreenRobot.class )
    .checkAreHeadersDisplayed();
    }

    @Test
    public void shouldNotAllowLoginWhenInValid () {
    withRobot( LoginScreenRobot.class )
    .provideActivityContext( activityRule.getActivity() )
    .callLogin( "", "" )
    .checkIsInErrorState();
    }

    @Test
    public void shouldProceedLoginWhenValid () {
    withRobot( LoginScreenRobot.class )
    .callLogin( TEST_EMAIL, TEST_PASSWORD )
    .checkIsLoggedIn();
    }

    public static class LoginScreenRobot extends ScreenRobot<LoginScreenRobot> {

    public LoginScreenRobot checkIsLoggedIn () {
    intended( hasComponent( hasShortClassName( PACKAGE_NAME ) ) );
    return this;
    }

    public LoginScreenRobot checkIsInErrorState () {
    return checkDialogWithTextIsDisplayed( R.string.login_error );
    }

    public LoginScreenRobot checkAreHeadersDisplayed () {
    return checkIsDisplayed( R.id.login_username_header, R.id.login_password_header );
    }

    public LoginScreenRobot callLogin (String username, String password) {
    return enterTextIntoView ( R.id.login_username_entry, username )
    .enterTextIntoView ( R.id.login_password_entry, password )
    .clickOkOnView( R.id.login_button );
    }
    }
    }
  6. @adavis adavis created this gist Apr 15, 2016.
    95 changes: 95 additions & 0 deletions ScreenRobot.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    package <your_package>;

    import android.app.Activity;
    import android.support.annotation.IdRes;
    import android.support.annotation.StringRes;

    import static android.support.test.espresso.Espresso.onView;
    import static android.support.test.espresso.action.ViewActions.click;
    import static android.support.test.espresso.action.ViewActions.swipeLeft;
    import static android.support.test.espresso.action.ViewActions.typeText;
    import static android.support.test.espresso.assertion.ViewAssertions.matches;
    import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
    import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
    import static android.support.test.espresso.matcher.ViewMatchers.withHint;
    import static android.support.test.espresso.matcher.ViewMatchers.withId;
    import static android.support.test.espresso.matcher.ViewMatchers.withText;
    import static org.hamcrest.Matchers.not;

    @SuppressWarnings("unchecked")
    public abstract class ScreenRobot<T extends ScreenRobot> {

    private Activity activityContext; // Only required for some calls

    public static <T extends ScreenRobot> T withRobot (Class<T> screenRobotClass) {
    if ( screenRobotClass == null ) {
    throw new IllegalArgumentException( "instance class == null" );
    }

    try {
    return screenRobotClass.newInstance();
    }
    catch ( IllegalAccessException iae ) {
    throw new RuntimeException( "IllegalAccessException", iae );
    }
    catch ( InstantiationException ie ) {
    throw new RuntimeException( "InstantiationException", ie );
    }
    }

    public T checkIsDisplayed (@IdRes int... viewIds) {
    for ( int viewId : viewIds ) {
    onView( withId( viewId ) ).check( matches( isDisplayed() ) );
    }
    return (T) this;
    }

    public T checkIsHidden (@IdRes int... viewIds) {
    for ( int viewId : viewIds ) {
    onView( withId( viewId ) ).check( matches( not( isDisplayed() ) ) );
    }
    return (T) this;
    }

    public T checkViewHasText (@IdRes int viewId, String expected) {
    onView( withId( viewId ) ).check( matches( withText( expected ) ) );
    return (T) this;
    }

    public T checkViewHasText (@IdRes int viewId, @StringRes int messageResId) {
    onView( withId( viewId ) ).check( matches( withText( messageResId ) ) );
    return (T) this;
    }

    public T checkViewHasHint (@IdRes int viewId, @StringRes int messageResId) {
    onView( withId( viewId ) ).check( matches( withHint( messageResId ) ) );
    return (T) this;
    }

    public T clickOkOnView (@IdRes int viewId) {
    onView( withId( viewId ) ).perform( click() );
    return (T) this;
    }

    public T enterTextIntoView (@IdRes int viewId, String text) {
    onView( withId( viewId ) ).perform( typeText( text ) );
    return (T) this;
    }

    public T provideActivityContext (Activity activityContext) {
    this.activityContext = activityContext;
    return (T) this;
    }

    public T checkDialogWithTextIsDisplayed (@StringRes int messageResId) {
    onView( withText( messageResId ) )
    .inRoot( withDecorView( not( activityContext.getWindow().getDecorView() ) ) )
    .check( matches( isDisplayed() ) );
    return (T) this;
    }

    public T swipeLeftOnView (@IdRes int viewId) {
    onView( withId( viewId ) ).perform( swipeLeft() );
    return (T) this;
    }
    }