如何在浓缩咖啡测试中获得 Activity?
How do I get Activity within an espresso test?
我正在尝试使用 firebase ScreenShotter,其形式为:
ScreenShotter.takeScreenshot("main_screen_2", this /* activity */);
我不确定我应该如何从 Espresso 测试中获得 Activity。目前我的测试 class 看起来像:
@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 所解释的那样,class 已被弃用,而 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));
}
我正在尝试使用 firebase ScreenShotter,其形式为:
ScreenShotter.takeScreenshot("main_screen_2", this /* activity */);
我不确定我应该如何从 Espresso 测试中获得 Activity。目前我的测试 class 看起来像:
@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 所解释的那样,class 已被弃用,而 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));
}