要读取excel的内容并使用Robotium测试用例中的值

时间:2014-02-17 15:56:43

标签: java android excel robotium jxl

我正在尝试编写一个代码,其中,我正在读取excel的内容,我必须使用excel中的值并将其传递给我的Robotium测试用例。

这是我的代码:

ReadExcel类://读取第3列第2行以及第4列和第2行的值

公共类ReadExcel {

private String user,pass;
public void read() throws BiffException, IOException
{
     Workbook wrk1 =  Workbook.getWorkbook(new File("D:/Robo/Book1.xls"));
     Sheet sheet1 = wrk1.getSheet(0);

     Cell username = sheet1.getCell(2,1);  // the username from the excel
     Cell password = sheet1.getCell(3,1);  // the password from the excel

     setUser(username.getContents());
     setPass(password.getContents()); 
}
public void setUser(String user) {
    this.user = user;
}
public String getUser() {
    return user;
}
public void setPass(String pass) {
    this.pass = pass;
}
public String getPass() {
    return pass;
}

}

这是使用Robotium的测试脚本

import android.test.ActivityInstrumentationTestCase2; import com.ifs.banking.fiid5015.test.ReadExcel;

import com.robotium.solo.Solo;

@SuppressWarnings( “rawtypes”) 公共类FSBTest扩展了ActivityInstrumentationTestCase2 {

private Solo solo;
private String user,pass;
private static final String TARGET_PACKAGE_ID = "com.ifs.banking.fiid5015";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.banking.activities.LoginActivity"; 
private static Class<?> launcherActivityClass;
static {
    try {
        launcherActivityClass = Class
        .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
    } catch (ClassNotFoundException e) {

        throw new RuntimeException(e);
    }
}

@SuppressWarnings({ "deprecation", "unchecked" })
public FSBTest() throws ClassNotFoundException {
    super(TARGET_PACKAGE_ID, launcherActivityClass);
}

@Override
protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}
public void testApp() {

    ReadExcel rd= new ReadExcel(); // creating the object for Read excel class
             rd.read();
    user = rd.getUser(); //passing the username
    pass = rd.getPass(); //passing the password

    // Login to the application
    solo.enterText(0,user); // Enter user name
    solo.sleep(1000); 
    solo.enterText(1, "dsf");
    solo.sleep(1000);
    solo.clickOnButton(1); // Try Login! (bad login)
    solo.sleep(10000);
    solo.clickOnButton("OK");
    solo.sleep(5000);

    solo.enterText(1,pass); // Enter Password (authentic password)
    solo.sleep(5000);
    solo.clickOnButton(1); // Try Login! (Valid login)
    solo.sleep(10000);

    // MFA
    solo.clickOnButton(0); // choose Text Me (xxx-xxx-3576)
    solo.sleep(20000);

    solo.clickOnButton(0);
    solo.sleep(20000);
}

@Override
public void tearDown() throws Exception {
    try {
        solo.finalize();
    } catch (Throwable e) {
        e.printStackTrace();
    }
    getActivity().finish();
    super.tearDown();
}

}

当我尝试运行我的项目时,遇到java.lang.FileNotFoundException,似乎我无法从excel中获取值。基本上我被困在这里。请帮助!

谢谢!

1 个答案:

答案 0 :(得分:2)

您必须将文件放入设备。你可以使用例如sdcard。

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "Book1.xls";

File f = new File(String.format("%s%s%s", baseDir, File.separator, fileName));