Android - Espresso testing - Close and app and then re-open it?

北城余情 提交于 2020-02-24 05:32:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!