本文介绍了使用 Robotium 进行生命周期测试:终止和重新启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重新启动使用 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()?

  • 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 进行生命周期测试:终止和重新启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 10:33