gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:48 25 4
gpt4 key购买 nike

使用 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;
}
}

}

更新了底部的两个代码以反射(reflect)我的项目。

最佳答案

不要创建名为 testLoggingin()测试用例。相反,创建一个具有函数login()。这样,只要需要,您就可以导入它并调用该函数来登录。您可以使用 assert 检查条件。

关于java - 如何使用robotium调用另一个类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18095998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com