Espresso: Wait for the activity to complete the background task after pressing the button ()

I look around for some time about testing espresso and especially about user idle resources (I implemented a couple of working cases), but I came across a script that seems to be unable to solve.

I have an Espresso SearchUITest class that runs an activity to search for material. Before doing any search, I open SettingsActivity to make some changes to the application configuration. When the pressBack () method is run from the parameter activity to return to SearchActivity, a background task (not using AsyncTask) starts to update the configuration before returning to SearchActivity again.

Problem - This SearchActivity does not resume until the first call is made to interact with its interface. I get this exception (sometimes I get a NoActivityResumedException and some other NoMatchingViewException exceptions):

android.support.test.espresso.NoActivityResumedException: No
activities in stage RESUMED. Did you forget to launch the activity.
(test.getActivity() or similar)? ...

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching:

How can I make Espresso wait for SettingsActivity to complete its background job and open SearchActivity before launching the next test offer. I leave the code for further clarification (by the way, I run a parameterized test with espresso):

@RunWith(value = Parameterized.class)
public class SearchByCountryE2ETest {

private static List<Country> mCountriesList;
private SearchActivity mActivity;

private String countryID;
private String countryLocation;
private String countryLanguage;

public SearchByCountryE2ETest(String countryID,
                              String countryLocation,
                              String language) {
    this.countryID = countryID;
    this.countryLocation = countryLocation;
    this.countryLanguage = language;
}


@Parameterized.Parameters(name = "id {0}, language{2}")
public static Collection countryIDs() {
    return Arrays.asList(new Object[][]{
            {"001", "mad", "Español (España)"},
            {"002", "lon", "English (United Kingdom)"}
    });
}

@Rule
public ActivityTestRule<SearchActivity> mSearchActivityRule = new ActivityTestRule<>(
        SearchActivity.class);

@Before
public void setUp() throws Exception {
    mSearchActivityRule.getActivity();
    if (mCountriesList == null) {
        mCountriesList = SingletonCommon.getInstance().getCountries().getCountry();
    }
}


@Test
public void testEnd2EndWithCountryID() {
    String countryName = getCountryNameById(countryID);
    if (countryName != null) {
        setCountryAndLanguage(countryName, countryLanguage);
        performCompleteSearch();
    }
}


private void setCountryAndLanguage(String countryName, String countryLanguage) {
    openSettings();
    changeLanguage(countryLanguage);
    changeCountry(countryName);
    pressBack();
    waitMillis(3000); // this is where the settings background task to update config is started
}

public void performCompleteSearch() {
    setLocation();
    clickViewWithId(R.id.search_button);//Go to filters activity

    onView(withId(R.id.search_button)).check(matches(isClickable()));//Check if results != 0
    clickViewWithId(R.id.search_button);//Go to listing activity

    waitMillis(1000); // wait millis is just a call to SystemClock.sleep(millis);

    openListingDetailAtPosition(1);// Click on item an open detail view
    pressBack();
}

private void openListingDetailAtPosition(int position) {
    onView(withId(R.id.recycler_view))
            .perform(scrollToPosition(position));
    onRecyclerViewItemAtPosition(click(), R.id.recycler_view, position);
}

private void openSettings() {
    onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
    onData(anything()).inAdapterView(withId(R.id.drawer_list_view)).atPosition(SETTINGS_DRAWER_POSITION).perform(click());
}

private void changeLanguage(String countryLanguage){
    if (countryLanguage != null && !countryLanguage.isEmpty()) {
        clickOnSpinnerStringItem(R.id.spinner_list_language, countryLanguage);
    }
}

private void changeCountry(String countryName){
    clickOnSpinnerStringItem(R.id.spinner_list_countries, countryName);
}

private void setLocation() {
    clickViewWithId(R.id.geolocation_edit_text);
    enterTextIntoViewWithId(countryLocation, R.id.location_edit_text);
    waitMillis(500);
    onRecyclerViewItemAtPosition(click(), R.id.locations_list, DEFAULT_SELECTED_LOCATION_POSITION);
    onView(withId(R.id.geolocation_edit_text)).perform(closeSoftKeyboard());
}

}

waitMillis (millis), , , SearchActivity, , . , , ( , ).

, , . .

+4

Source: https://habr.com/ru/post/1621486/


All Articles