我不知道如何更改Espresso测试中的开始活动。
应用程序中的第一个活动是SplashActivity。我想测试的活动是DepartmentsActivity。
这是我的测试文件:
@LargeTest
@RunWith(AndroidJUnit4.class)
public class DepartmentsActivityTest {
@Rule
public ActivityTestRule<DepartmentsActivity> activityTestRule = new ActivityTestRule<DepartmentsActivity>(DepartmentsActivity.class) {
@Override
protected Intent getActivityIntent(){
Context targetContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext();
return new Intent(targetContext, DepartmentsActivity.class);
}
};
@Test
public void departmentsActivityTest() throws InterruptedException {
onView(allOf(withText(getResString(R.string.my_departments))))
.check(matches(isDisplayed()));
onView(childAtPosition(
childAtPosition(withId(R.id.content_layout), 0), 1))
.check(matches(isDisplayed()));
onView(allOf(childAtPosition(
allOf(withId(R.id.content_layout), childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class),
0)), 5)))
.check(matches(isDisplayed()));
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
private String getResString(int id) {
Context targetContext = InstrumentationRegistry.getTargetContext();
return targetContext.getResources().getString(id);
}
}
无论我尝试什么,它总是从SplashActivity开始(而不是DepartmentsActivity)。
我还尝试了什么:
Init ActivityTestRule,如下所示:
@Rule
public ActivityTestRule<DepartmentsActivity> activityTestRule = new ActivityTestRule<>(DepartmentsActivity.class, true, true);
尝试启动精确测试的精确活动:
@Rule
public ActivityTestRule<DepartmentsActivity> activityTestRule = new ActivityTestRule<>(DepartmentsActivity.class, true, false);
@Test
public void departmentsActivityTest() throws InterruptedException {
Intent intent = new Intent();
activityRule.launchActivity(intent);
(...)
}
但是在每次尝试模拟器中总是以SplashActivity开始(在清单中设置为启动器活动)。我不想在清单中更改启动活动。
我正在使用这些浓缩咖啡版本:
androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'
我做错了什么?