问题描述
我正在尝试使用firebase ScreenShotter格式:
ScreenShotter.takeScreenshot("main_screen_2",this /* activity */);
我不确定我应该如何从Espresso测试中获取活动。目前,我的测试课程如下:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class OtherTests {
@Rule
// Replace 'MainActivity' with the value of android:name entry in
// <activity> in AndroidManifest.xml
public ActivityScenarioRule <MainActivity> mActivityRule = new ActivityScenarioRule<>(MainActivity.class);
@Test
public void getDeviceInfo() {
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
e.printstacktrace();
}
TestHelper.tap("APP_HEADER");
TestHelper.expect("TRUE_HOME_BUTTON",5000);
ScreenShotter.takeScreenshot("main_screen_2",this /* activity */);
}
}
解决方法
根据sample app provided by Google(在底部),您可以从ActivityInstrumentationTestCase2
继承并使用getActivity()
方法。
但是,如here所述,不推荐使用该类,而推荐使用ActivityTestRule
方法,该方法也具有getActivity()
方法。
public void testExample() {
// Take a screenshot when app becomes visible.
onView(isRoot());
ScreenShotter.takeScreenshot("main_screen_1",getActivity());
}
,
您必须获取scenario
,然后在传递给onActivity
的回调中运行您的代码
@Test
public void getDeviceInfo() {
...
mActivityRule.getScenario()
.onActivity(activity -> ScreenShotter.takeScreenshot("main_screen_2",activity));
}