问题描述
如何重新启动使用 Robotium 的 solo.goBack()
结束的活动?
How is it possible to restart an activity that was ended using Robotium's solo.goBack()
?
以下不会重新启动活动:(测试完成)
The following does not restart the activity: (the test finishes ok)
solo.goBack();
try {
// recreate activity here
runTestOnUiThread(new Runnable() {
public void run() {
getInstrumentation().callActivityOnCreate(getActivity(),
null);
getInstrumentation().callActivityOnStart(getActivity());
getInstrumentation().callActivityOnResume(getActivity());
}});
}
如何重新启动由 Solo.goBack()
结束的 Activity?
How do you restart an Activity that was ended by Solo.goBack()
?
- 使用机器人测试活动流程解决了 Robotium 中两个活动之间的变化,而不是销毁和重新启动.
- 模拟安卓查杀并重启服务处理一项服务,而不是一项活动(并且没有得到答复)
- 在使用 Robotium 的不同测试中,Activity 不会重新启动询问如何手动重新启动活动,但以不同的方式回答
- Testing activity flow with robotiumaddresses changing between two activities in Robotium, not destoying and restarting.
- Simulate Android killing and restart servicedeals with a service, not an activity (and is unanswered)
- Activity doesn't restart in different tests with Robotiumasks how to restart the activity manually, but is answered in a different way
要重现这样的最小测试,请创建一个项目及其测试项目:
To reproduce a minimal test like this, create a project and its test project:
android create project -t 1 -p testRestart -k com.testRestart -a testactivity
cd testRestart
mkdir tests
cd tests
android create test-project -m .. -p .
将 Robotium jar 复制到 tests/libs
文件夹.将此代码粘贴到文件 testactivityTest.java
中:
Copy the Robotium jar to the tests/libs
folder.Paste this code inside the file testactivityTest.java
:
package com.testRestart;
import android.test.ActivityInstrumentationTestCase2;
import com.robotium.solo.Solo;
public class testactivityTest extends ActivityInstrumentationTestCase2<testactivity> {
private Solo solo;
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
public testactivityTest() {
super("com.testRestart", testactivity.class);
}
public void testDestroyAndRestart() {
solo.goBack();
try {
// recreate activity here
runTestOnUiThread(new Runnable() {
public void run() {
getInstrumentation().callActivityOnCreate(getActivity(),
null);
getInstrumentation().callActivityOnStart(getActivity());
getInstrumentation().callActivityOnResume(getActivity());
}});
} catch ( Throwable t ) {
throw new RuntimeException(t);
}
}
}
在测试文件夹中,做一个
Inside the tests folder, do a
ant debug install
adb shell am instrument -w -e class com.testRestart.testactivityTest com.testRestart.tests/android.test.InstrumentationTestRunner
再次提问:如何重新启动由 Solo.goBack()
结束的活动?
The question again: how can you restart an activity that was ended by Solo.goBack()
?
推荐答案
正如@IHeartAndroid 在他的回答中对这个机器人问题(我没看过之前,@Flavio Capaccio 在相关问题"):
As @IHeartAndroid said in his answer to this robotium question (I had not seen it before, there was a link by @Flavio Capaccio in a comment to a "related question"):
launchActivity("com.testRestart", testactivity.class, null);
有效.这是InstrumentationTestCase中的一个函数.
works. This is a function in InstrumentationTestCase.
(如果你想为这个答案点赞,也请点赞他的答案)
(If you want to upvote this answer, upvote his answer as well)
这篇关于使用 Robotium 进行生命周期测试:终止和重新启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!