Java – how to stop and start executing a for loop
I'm creating a framework for our project,
Before method (use JXL to read the first line)
@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 method
@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 that for each test method, a beforemethod get is executed on the right of the first one. My problem is that I read the first line from excel and put it in the before method (I need it in the test method). The second line comes from excel I. how should I implement the second test method?
If there are any other ways, such as using different loops, please help me
Solution
Change the 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 adjust your test method (PS: method and field names should start with lowercase letters!):
@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); } }
In addition, your object cellcontent appears to be a string object Maybe you can replace cellcontent with string
If you especially want to display the rows and columns of cells, expand the parameters of the test method:
@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); } }