简体   繁体   中英

restart app and follow the same steps Robotium

I am learning to use robotium and I am trying to relaunch the application and do the same steps 5 time. I know to put for loop, but how do I relaunch application? I was using robotium recorder to do some of it, but it's easier to edit the script manually instead of recording again so I am trying to figure this out.

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

I can suggest to create private helper method with test logic and 5 different test methods which call the helper. Before every test method there is setUp and after there is tearDown so your application will be restarted. Your class can look like:

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

Another way is to create own test suite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM