问题
In my espresso test, I close the app by using "device.pressBack()". I then want to re-open the app, in a certain Activity, but I'm not quite sure how to do that, since I don't even have a context at this point. Does anybody have an idea?
回答1:
OK, got it. You define a rule in your test class:
@Rule
public ActivityTestRule<MyActivity> myActivityTestRule = new ActivityTestRule<>(MyActivity.class, true, false);
Then, after you used device.pressback(), you can use this to open that specific Activity in your app:
myActivityTestRule.launchActivity(null);
回答2:
After you close your app, a NoActivityResumedException
exception is thrown so you will have to catch it.
After that, relaunch your activity by using an ActivityTestRule
of type MyActivity
as Cookienator have said.
@Rule
public ActivityTestRule<MyActivity> myActivityTestRule = new ActivityTestRule<>(MyActivity.class, true, false)
myActivityTestRule.launchActivity(null);
来源:https://stackoverflow.com/questions/40508113/android-espresso-testing-close-and-app-and-then-re-open-it