加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:13:12 所属栏目:Java 来源:网络整理
导读:我正在为我们的项目创建框架, BEFOREMETHOD(使用JXl读取第一行) @BeforeMethodpublic void Test2() throws BiffException,IOException{ FileInputStream filepath = new FileInputStream("D://Selenium//Project_Stage//AddGroup//inputtoAddGroup.xls"); Wo
我正在为我们的项目创建框架,

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 I想要读取执行第二个测试方法我应该如何实现?

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

解决方法

将迭代更改为以下内容:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读