使用 Espresso,我尝试测试使用“主页”按钮将活动发送到后台,然后再次将其放在前台进行一些检查:
@EspressoTest
public void test() {
onSomeView().check(matches(isDisplayed()));
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
Context context = getInstrumentation().getTargetContext();
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
onSomeView().check(matches(isDisplayed()));
}
我必须使用
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
这是由异常建议的,但除此之外我还测试了,将其启动为启动器活动,或使用
FLAG_ACTIVITY_REORDER_TO_FRONT
,但视图不可见。即使测试通过了。
请考虑此回复,因为当您想要时它 100% 有效 goBackgroundByClickingHome然后返回应用程序。
UiDevice device = UiDevice.getInstance(getInstrumentation());
device.pressHome();
device.pressRecentApps();
device.findObject(new UiSelector().text(getTargetContext().getString(getTargetContext().getApplicationInfo().labelRes)))
.click();
好吧,我明白了。首先,需要使用 getActivity 提供的 Activity 上下文,然后我可以使用 HOME 类别和 FLAG_ACTIVITY_REORDER_TO_FRONT 在后台和前台发送 Activity 的意图:
private void goHome(Activity activity) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
activity.startActivity(intent);
}
private void bringToForeground(Activity activity) {
Intent intent = new Intent(activity, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
activity.startActivity(intent);
}
@EspressoTest
public void test() {
//do some ui stuff to ensure the activity is up and running
goHome(getActivity());
// eventually sleep, or implement an idling resource
bringToForeground(getActivity());
// do some ui tests
}
您可以使用 UI Automator 来完成此操作(您可以同时使用 espresso 和 UI Automator)。
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
device.pressHome()
device.pressRecentApps()
val selector = UiSelector()
val recentApp = device.findObject(selector.descriptionContains("App Description"))
recentApp.click()
这使我的应用程序进入后台并将其带到前台
在我的用例中,我需要对某些活动进行压力测试,运行多次“转到 BG,然后到 FG,然后到 BG 和 FG ....”。 最终使用了这段代码:
@Test
fun moveAppToBgAndFg_multipleTimes() {
val instrumentation = InstrumentationRegistry.getInstrumentation()
val device = UiDevice.getInstance(instrumentation)
launchActivity<MyActivity>().use {
// ... prepare the activity if you need...
// when ready, start to throw the app to BG and FG!
repeat(100) {
// throw app to BG
device.pressHome() // this also adds appropriate "Thread.sleep()" delay
// show recent apps
device.pressRecentApps()
Thread.sleep(1000L) // account for "recent apps" enter-animation
// click screen's center, my app is the last recent app so it's shown there
device.click(device.displayWidth / 2, device.displayHeight / 2)
Thread.sleep(500L) // account for app's enter-animation
}
}
}
对我来说,在将其发送到后台后通过检查标签来查找应用程序不是一个选项,因为并非所有设备都在最近的应用程序视图中提供此标签,导致测试失败。
为了解决这个问题,我使用了以下代码:
val instrumentation = InstrumentationRegistry.getInstrumentation()
val device = UiDevice.getInstance(instrumentation)
device.pressHome()
Thread.sleep(1500)
device.pressRecentApps()
Thread.sleep(1500)
device.findObject(UiSelector().index(0)).click()
这可以确保最后使用的应用程序(正在测试的应用程序)始终位于位置 0。
P.S.:我发现有必要包含
Thread.sleep
调用,以便在选择应用程序之前给设备足够的时间达到正确的状态。