如何重新启动使用Robotium结束的活动solo.goBack()?
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()?
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:
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); } } }
在测试文件夹中,执行
ant debug install adb shell am instrument -w -e class com.testRestart.testactivityTest com.testRestart.tests/android.test.InstrumentationTestRunner
再次提出问题:如何重新启动以结尾的活动Solo.goBack()?
正如@IHeartAndroid在他对这个机器人问题的回答中所说(我以前从未见过,@ Flavio Capaccio在一个“ 相关问题” 的评论中有一个链接):
launchActivity("com.testRestart", testactivity.class, null);
作品。这是InstrumentationTestCase中的一个函数。