我一直在寻找关于espresso测试的一段时间,特别是关于自定义闲置资源(我已经实现了几个工作案例),但我遇到了一个我似乎无法解决的问题。
我有一个Espresso SearchUITest类,它启动一个搜索内容的活动。在进行任何搜索之前,我打开SettingsActivity对应用程序配置进行一些更改。从设置活动运行pressBack()方法以返回到SearchActivity时,将启动后台任务(不使用AsyncTask)以更新配置,然后再返回到SearchActivity。
问题是在完成第一次与其UI交互的调用之前未恢复SearchActivity。我得到了这个异常(有时我得到NoActivityResumedException和其他一些NoMatchingViewException):
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:
如何让Espresso等待SettingsActivity完成后台任务并在运行下一个测试句之前打开SearchActivity。我留下了一些代码以便进一步澄清(顺便说一句,我正在使用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打开的活动的后台任务的加载,因为我无法为在一个之后打开的活动创建和注册空闲资源开始测试。如果有一个关于如何执行此操作的完整示例(为执行测试时打开的不同活动注册空闲资源),我也会非常感激。
如果需要进一步澄清,请告诉我。在此先感谢您的帮助。