vendredi 26 février 2016

Espresso, testing happy path with login screen

I am trying to test my Happy path based on Espresso. My flow is like this: SplashActivity -> Activity_1 -> Activity_2 -> Activity_3 -> Activity_4. There is a button in Activity_1 that directs user to Activity_2 if user has not logged in otherwise Activity_3 for Booking something.

My tests passes if app goes in this direction SplashActivity -> Activity_1 -> Activity_3 ->.... However I'm getting exception, android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: when user has not logged in and therefore app goes in this way SplashActivity -> Activity_1 -> Activity_2 ->....

This is obvious because my test expects Activity_3 while Activity_2 is visible.

This is my test:

@RunWith(AndroidJUnit4.class)
public class MinHappyPathTest
{
    @Rule
    public ActivityTestRule<Activity_1> mActivityTestRule = new ActivityTestRule<>(Activity_1.class);

    private Activity_1 mActivity_1;

    @Before
    public void setup()
    {
        mActivity_1 = mActivityTestRule.getActivity();
    }

    @Test
    public void HappyPathMinimumTest() throws InterruptedException
    {    
        // Wait to everything settles down (few animations there)
        Thread.sleep(2000);

        // On mActivity_1 press the button
        onView(withId(R.id.btnNext)).perform(click());

        // On mActivity_3        onView(withId(R.id.editText)).perform(typeText(destination_short_name), ViewActions.closeSoftKeyboard());
        Thread.sleep(1000); // to results displays
        onView(allOf(withId(R.id.recycler_view), isDisplayed()))
                .perform(RecyclerViewActions.actionOnItemAtPosition(2, click()));

        // Other tests...
    }
}

Two questions I have:

  1. How to put if statement based on the activity is visible after Activity_1?
  2. Based on what I have found unlike Unit Testing that you are able to pick a class and perform testing against it, it is not possible to do same thing in Espresso. For example I directly run mActivity_4 and test it since when app launches mActivity_1 displays by default and I get NoMatchingViewException. Am I right? I actually tested and looks like that.

Aucun commentaire:

Enregistrer un commentaire