我有一个贯穿整个应用程序的测试。现在,我想把所有的东西都拍下来。因为它有2个活动和许多片段,我不能使它工作,因为它只需要每个活动的第一个片段。
我怎样才能把每个片段都拍下来?
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HearthBeatUITest {
private final int MILISECONDS_TIMEOUT = 700;
@Rule
public ActivityTestRule<IntroActivity> mActivityRule = new ActivityTestRule<>(IntroActivity.class);
@Rule
public ActivityTestRule<LoginActivity> mLoginActivityRule = new ActivityTestRule<>(LoginActivity.class);
@Test
/**
* Testing all the screens on the application if they are actually there
*/
public void startTest() {
Session.clear();
Spoon.screenshot(mActivityRule.getActivity(), "initial_state");
threadSleep(MILISECONDS_TIMEOUT);
onView(withId(R.id.button_register)).check(matches(isDisplayed())).perform(click());
threadSleep(MILISECONDS_TIMEOUT);
Spoon.screenshot(mLoginActivityRule.getActivity(), "register_intro");
onView(withId(R.id.register_with_email)).check(matches(isDisplayed())).perform(click());
threadSleep(MILISECONDS_TIMEOUT);
Spoon.screenshot(mLoginActivityRule.getActivity(), "register_detailed");
onView(withId(R.id.image_left_button)).check(matches(isDisplayed())).perform(click());
threadSleep(MILISECONDS_TIMEOUT);
Spoon.screenshot(mLoginActivityRule.getActivity(), "register_intro");
onView(withId(R.id.image_left_button)).check(matches(isDisplayed())).perform(click());
threadSleep(MILISECONDS_TIMEOUT);
Spoon.screenshot(mActivityRule.getActivity(), "initial_state");
onView(withId(R.id.button_signin)).check(matches(isDisplayed())).perform(click());
threadSleep(MILISECONDS_TIMEOUT);
Spoon.screenshot(mLoginActivityRule.getActivity(), "login_intro");
onView(withId(R.id.sign_in_emal)).check(matches(isDisplayed())).perform(click());
threadSleep(MILISECONDS_TIMEOUT);
Spoon.screenshot(mLoginActivityRule.getActivity(), "login_detailed");
}
}
首先,您只需要一个规则:
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HearthBeatUITest {
private final int MILISECONDS_TIMEOUT = 300;
@Rule
public IntentsTestRule<IntroActivity> mActivityRule = new IntentsTestRule<>(IntroActivity.class);
private Activity currentActivity;
}
现在,当您需要创建屏幕截图的上下文时,您将调用这个方法:
private Activity getActivityInstance() {
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED);
if (resumedActivities.iterator().hasNext()) {
currentActivity = (Activity) resumedActivities.iterator().next();
}
}
});
return currentActivity;
}
它将得到你当前的活动,你将能够采取截图。提示一下,你不能截取对话框的截图