如何使用robotium调用另一个类中的方法

时间:2013-08-07 06:19:58

标签: java robotium qa

将Robotium用于我的Android自动化我发现自己为每个测试用例创建了相同的步骤。

我总是需要“登录”和“注销”,我一直在尝试创建一个FunctionsTestClass,所以我可以简单地调用rLogin();和rLogout();

以下是一个例子:

添加我的完整文件。

'package com.myproject.mobile.test;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;
import com.jayway.android.robotium.solo.By;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

'public class Logout extends ActivityInstrumentationTestCase2<Activity> {

private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.myproject.mobile.MainActivity";
private static Class<?> launchActivityClass;
static {
    try {
        launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
private static ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps
                        // collecting better test execution data during test
                        // runs

@SuppressWarnings("unchecked")
public Logout() {
    super((Class<Activity>) launchActivityClass);
}

@Override
public void setUp() throws Exception {
    super.setUp();
    solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
            .getCanonicalName(), getName());
}

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


public static void logginin() throws Exception {
    try {
    //enter username
        solo.sleep(17000);

        throw e;
    } catch (Exception e) {
        solo.fail(
                "com.myproject.mobile.test.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    }
}

}'

添加我的第二个文件

package com.mypackage.mobile.test;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;
import com.jayway.android.robotium.solo.By;
import com.mypackage.mobile.test.*;


import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

'public class Test extends ActivityInstrumentationTestCase2<Activity> {

private static final String LAUNCHER_ACTIVITY_CLASSNAME =     "com.mypackage.mobile.MainActivity";
private static Class<?> launchActivityClass;

static {
    try {
        launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
private static ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps
                        // collecting better test execution data during test
                        // runs

@SuppressWarnings("unchecked")
public Test() {
    super((Class<Activity>) launchActivityClass);
}

@Override
public void setUp() throws Exception {
    super.setUp();
    solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
            .getCanonicalName(), getName());
}

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



public void testRecorded() throws Exception {
    try {

        Logout.logginin();


    } catch (AssertionFailedError e) {
        solo.fail(
                "com.mypackage.name.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    } catch (Exception e) {
        solo.fail(
                            "com.mypackage.name.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    }
}

    }

更新了底部的两个代码以反映我的项目。

1 个答案:

答案 0 :(得分:1)

请勿使用名称testLoggingin()创建测试用例。而不是该创建具有功能 login()。因此,无论何时需要,您都可以导入它并可以调用该函数进行登录。您可以使用assert检查条件。