Espresso - activity becomes closed after each test method. Is there a way that we cannot stop the closing activity and start other testing methods

spresso - activity closes after each testing method. Is it possible that we will not be able to stop the closing action and launch other testing methods.

+4
source share
1 answer

If I understand what you mean, you can make one of these decisions:

  • Write down one big test to check all the required views without any interruption:

    @Test
    public void checkIfMainActivityViewsAreProperlyDisplayed() {
    
        //Check if activity name is visible on Toolbar
        onView(withText(R.string.action_main)).check(matches(withParent(withId(R.id.toolbar))));
    
        //Check if ListView is visible at least 50 percent 
        onView(withId(R.id.mListView)).check(matches(isDisplayingAtLeast(50)));
    
        //Check if order Checkbox is checked. Perform check.
        onView(withId(R.id.mCheckBox))
                .check(matches(isNotChecked()))
                .perform(ViewActions.click())
                .check(matches(isChecked()));
    }
    

Use comments to make your code more readable to others.

  • , :

     private void checkifActivityNameIsVisibleOnToolbar() {
          onView(withText(R.string.action_main)).check(matches(withParent(withId(R.id.toolbar))));
     }
    
     private void checkifListViewIsVisibleAtLeastFiftyPercent() {
          onView(withId(R.id.mListView)).check(matches(isDisplayingAtLeast(50)));
     }
    
     private void performClickOnOrderCheckBoxToFindIfStateChanged() {
        onView(withId(R.id.mOrder))
            .check(matches(isNotChecked()))
            .perform(ViewActions.click())
            .check(matches(isChecked()));
     }
    
    @Test
    public void checkIfMainActivityViewsAreProperlyDisplayed() throws InterruptedException {
          checkifActivityNameIsVisibleOnToolbar();
          checkifListViewIsVisibleAtLeastFiftyPercent();
          performClickOnOrderCheckBoxToFindIfStateChanged();
    }
    

, .

,

+2

All Articles