1

我正在学习使用机器人,我正在尝试重新启动应用程序并执行相同的步骤 5 次。我知道要放 for 循环,但是如何重新启动应用程序?我正在使用robotium recorder来做一些事情,但是手动编辑脚本而不是再次录制更容易,所以我试图弄清楚这一点。

import com.robotium.solo.*;
import android.test.ActivityInstrumentationTestCase2;


@SuppressWarnings("rawtypes")
public class explore extends ActivityInstrumentationTestCase2 {
    private Solo solo;

    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.application.calc.android.main.CGabboMainActivity";

    private static Class<?> launcherActivityClass;
    static{
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
           throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public explore() throws ClassNotFoundException {
        super(launcherActivityClass);
    }

    public void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation());
        getActivity();
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
        super.tearDown();
    }


    public void testRun() {

        // Wait for activity: 'com.application.calc.android.main.CGabboMainActivity';
        solo.waitForActivity("CGabboMainActivity", 2000);
        // Sleep for 10211 milliseconds
        solo.sleep(5000);
        // Click on source_internet_radio
        solo.clickOnWebElement(By.id("handle_name"));
        //Sleep for 5697 milliseconds
        solo.clickOnWebElement(By.id("source_help"));
        solo.clickOnWebElement(By.id("nav_item_1"));
        //solo.finishOpenedActivities();
        //solo.waitForActivity("CGabboMainActivity", 2000);
        //this.launchActivity(LAUNCHER_ACTIVITY_FULL_CLASSNAME, launcherActivityClass,null);
        //solo.clickOnWebElement(By.xpath(".//*[@id='nav_panel_0']/div[1]/div/div[2]"));
        //solo.sleep(15211);
        //solo.clickOnWebElement(By.id("handle_name"));
    }
}
4

1 回答 1

2

我可以建议创建带有测试逻辑的私有辅助方法和 5 种不同的调用辅助方法的测试方法。在每个测试方法之前有 setUp 和之后有 tearDown 所以你的应用程序将重新启动。您的课程可能如下所示:

import com.robotium.solo.*;
import android.test.ActivityInstrumentationTestCase2;

@SuppressWarnings("rawtypes")
public class explore extends ActivityInstrumentationTestCase2 {
    private Solo solo;

    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.application.calc.android.main.CGabboMainActivity";

    private static Class<?> launcherActivityClass;
    static{
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
           throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public explore() throws ClassNotFoundException {
        super(launcherActivityClass);
    }

    public void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation());
        getActivity();
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
        super.tearDown();
    }

    public void testRun1() {
        helper();
    }

    public void testRun2() {
        helper();
    }

    public void testRun3() {
        helper();
    }

    public void testRun4() {
        helper();
    }

    public void testRun5() {
        helper();
    }

    private void helper() {
        // Wait for activity: 'com.application.calc.android.main.CGabboMainActivity';
        solo.waitForActivity("CGabboMainActivity", 2000);
        // Sleep for 10211 milliseconds
        solo.sleep(5000);
        // Click on source_internet_radio
        solo.clickOnWebElement(By.id("handle_name"));
        //Sleep for 5697 milliseconds
        solo.clickOnWebElement(By.id("source_help"));
        solo.clickOnWebElement(By.id("nav_item_1"));
        //solo.finishOpenedActivities();
        //solo.waitForActivity("CGabboMainActivity", 2000);
        //this.launchActivity(LAUNCHER_ACTIVITY_FULL_CLASSNAME, launcherActivityClass,null);
        //solo.clickOnWebElement(By.xpath(".//*[@id='nav_panel_0']/div[1]/div/div[2]"));
        //solo.sleep(15211);
        //solo.clickOnWebElement(By.id("handle_name"));
    }
}

另一种方法是创建自己的测试套件。

于 2014-08-06T22:42:00.967 回答