简体   繁体   English

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

[英]how to stop and start execution of for loop

I am creating framework for our project, 我正在为我们的项目创建框架,

BEFOREMETHOD (reads first row using 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 methods 测试方法

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

What I want is, for each test method there is a BeforeMethod get executed at first right, my question is I have reading first row from Excel I placed it in Before method (I need it in test method), for second row from Excel I want to read to execute second test method how should I achieve that? 我想要的是,对于每个测试方法都有一个BeforeMethod get在第一个右边执行,我的问题是我从Excel读取第一行我把它放在Before方法中(我需要它在测试方法中),第二行来自Excel I想要读取执行第二个测试方法我应该如何实现?

If there any other way like using different loops please help me out. 如果有任何其他方式,如使用不同的循环,请帮助我。

change your iteration to the following: 将迭代更改为以下内容:

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

     }
        }

and adapt your Test method (PS: method and fieldnames should start with lower case!): 并调整你的测试方法(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);
    }
}

Also it seems that your object CellContent is a String object. 此外,您的对象CellContent似乎是一个String对象。 Maybe you could substitute CellContent with String. 也许你可以用String替换CellContent。

If you specificaly want to display the row and column of your cell extend the parameters of your Test method: 如果您特别想要显示单元格的行和列,请扩展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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM