如何重新启动使用Robotium solo.goBack()
结束的活动?
以下内容不会重新启动活动:(测试结束)
solo.goBack();
try {
// recreate activity here
runTestOnUiThread(new Runnable() {
public void run() {
getInstrumentation().callActivityOnCreate(getActivity(),
null);
getInstrumentation().callActivityOnStart(getActivity());
getInstrumentation().callActivityOnResume(getActivity());
}});
}
如何重新启动以Solo.goBack()
结束的活动?
要重现这样的最小测试,请创建一个项目及其测试项目:
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
:
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);
}
}
}
在tests文件夹中,执行
ant debug install
adb shell am instrument -w -e class com.testRestart.testactivityTest com.testRestart.tests/android.test.InstrumentationTestRunner
再次提出问题:如何重新启动以Solo.goBack()
结束的活动?
答案 0 :(得分:2)
正如@IHeartAndroid在his answer中对this robotium question说的那样(之前我还没有看过,@Flavio Capaccio在对#34; related question&的评论中有一个链接#34):
launchActivity("com.testRestart", testactivity.class, null);
的工作原理。这是InstrumentationTestCase中的函数。
(如果你想提出这个答案,请同时提升his answer)