重新启动应用程序并按照相同的步骤Robotium

时间:2014-08-06 17:18:31

标签: robotium relaunch

我正在学习使用robotium,我正在尝试重新启动应用程序并执行相同的步骤5次。我知道要循环,但我该如何重新启动应用程序?我使用机器人记录器做了一些,但是手动编辑脚本更容易,而不是再次记录,所以我试图解决这个问题。

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"));
    }
}

1 个答案:

答案 0 :(得分: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"));
    }
}

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