如何停止并开始执行for循环

时间:2016-02-17 10:53:10

标签: java selenium-webdriver jxl

我正在为我们的项目创建框架,

BEFOREMETHOD (使用JXl读取第一行)

@BeforeMethod
public void Test2() throws BiffException, IOException{
    FileInputStream filepath = new FileInputStream("D://Selenium//Project_Stage//AddGroup//inputtoAddGroup.xls");
    Workbook wb = Workbook.getWorkbook(filepath);
    Sheet sh = wb.getSheet(0);
    int totalNoOfRows = sh.getRows(); 
    int rows;
    for (rows=1 ; rows < totalNoOfRows;) {
        System.out.println("BeforeMethod Executed"+rows);
        CellContent1 = sh.getCell(0, rows).getContents().toUpperCase();
        System.out.println("CellContent is: "+CellContent1);
        CellContent2 = sh.getCell(1, rows).getContents();
        CellContent3 = sh.getCell(2, rows).getContents();
        CellContent4 = sh.getCell(3, rows).getContents();
        CellContent5 = sh.getCell(4, rows).getContents();
        rows=rows+1;
    }
}

测试方法

@Test(priority=0)
public void Test(){
    System.out.println("Test Is Executed1");
    if (CellContent1.matches("TRUE")) {
        System.out.println("p=0 Cell content is : "+CellContent5);
        cells.add(CellContent5);
    }
}

我想要的是,对于每个测试方法都有一个BeforeMethod get在第一个右边执行,我的问题是我从Excel读取第一行我把它放在Before方法中(我需要它在测试方法中),第二行从Excel我想阅读执行第二个测试方法我应该如何实现?

如果有其他方式使用不同的循环,请帮助我。

1 个答案:

答案 0 :(得分:0)

将您的迭代更改为以下内容:

     for (rows = 0 ; rows < totalNoOfRows; rows++) {
        for (columns = 0; columns <= 4; columns++){
            System.out.println("BeforeMethod Executed "+ rows + " " + columns);
            CellContent content =sh.getCell(columns, rows).getContents().toUpperCase();
            Test(content);

     }
        }

并调整你的测试方法(PS:方法和字段名称应以小写字母开头!):

@Test(priority=0)
public void Test(CellContent content){
    System.out.println("Test Is Executed");
    if (content.matches("TRUE")) {
        System.out.println("Cell content is : "+ content);
        cells.add(content);
    }
}

您的对象CellContent似乎也是一个String对象。也许你可以用String替换CellContent。

如果您特别想要显示单元格的行和列,请扩展Test方法的参数:

@Test(priority=0)
    public void Test(CellContent content, int column, int row){
        System.out.println("Test Is Executed");
        if (content.matches("TRUE")) {
            System.out.println("Cell content of cell " + column + "," + row + " is : "+ content);
            cells.add(content);
        }
    }
相关问题