我正在尝试在selenium中构建一个虚拟框架,它从具有3个值的excel表中获取/读取: Excelsheet screenshot
现在我的代码以TestNg登录方法开始,该方法触发ReadExcelsheet方法:
public class LoginTest extends BaseTest {
@Test(dataProvider = "dataForSheet",dataProviderClass = ExcelRW.class)
public void testSeccussfulLogin(String Key, String Username, String Password) {
try{
if(Key.equalsIgnoreCase("valid")){
Reporter.log("=====Login test case strated=====", true);
logn.LoginWithUsername(Username, Password);
}
}catch(Exception e){
System.out.println("Exception Handled "+e);
}
}
}`
现在我正在阅读行并使用这些值执行登录操作 Excel阅读代码:
public class Dp_ExcelRW {
@DataProvider(name = "dataForSheet")
public static Iterator<String[]> loginData() throws Exception {
Iterator<String[]> arrayObject = Read("E:\\F\\Excel_Sheet\\Test.xlsx","Sheet2");
return arrayObject;
}
public static Iterator<String[]> Read(String SheetAddress,String Sheet) throws Exception {
FileInputStream fis = new FileInputStream(SheetAddress);
Workbook wb= WorkbookFactory.create(fis);
Sheet sh =wb.getSheet(Sheet);
int rowCount=sh.getLastRowNum();
int columnCount=1;sh.getRow(0).getPhysicalNumberOfCells();
//create list
List<String []> lst=new ArrayList<String[]>();
for (int i= 1 ; i <= rowCount; i++) {
//array new of size equal to no. of column
String[] arr=new String[columnCount];
for (int j=0; j < columnCount; j++) {
try{
Row row=sh.getRow(i);
if(row == null){
row = sh.createRow(i);
}
arr[0]=row.getCell(j).getStringCellValue();
}
//add to list
catch(Exception e){
System.out.println("Row culdn't be read since:"+ e);
}
}
lst.add(arr);
}
return lst.iterator();
}
但问题是,这段代码甚至也取出了无效的关键字行,因为有一个baseTest类,它有@BeforeMethod,它启动浏览器并导航到URL。 如何根据给定的关键字只读取特定行。请建议我。
答案 0 :(得分:0)
您好我已经对您的代码进行了一些更改,如果有帮助请告诉我。
package com.read.files;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.testng.annotations.DataProvider;
public class StackOverFlowQuest {
@DataProvider(name = "dataForSheet")
public static Iterator<String[]> loginData() throws Exception {
Iterator<String[]> arrayObject = Read(System.getProperty("user.dir") + "\\needful\\Book1.xlsx","Sheet3");
return arrayObject;
}
public static Iterator<String[]> Read(String SheetAddress,String Sheet) throws Exception {
FileInputStream fis = new FileInputStream(SheetAddress);
Workbook wb= WorkbookFactory.create(fis);
Sheet sh =wb.getSheet(Sheet);
int rowCount=sh.getLastRowNum();
int columnCount=1;sh.getRow(0).getPhysicalNumberOfCells();
int arrint=0;
//create list
List<String []> lst=new ArrayList<String[]>();
for (int i= 1 ; i <= rowCount; i++) {
//array new of size equal to no. of column
String[] arr=new String[rowCount];
for (int j=0; j < columnCount; j=j+2) {
try{
Row row=sh.getRow(i);
if(row == null){
row = sh.createRow(i);
}
String testValue = row.getCell(j).getStringCellValue();
if(testValue.equalsIgnoreCase("Valid")){
arr[arrint]=row.getCell(j).getStringCellValue();
arr[arrint++]=row.getCell(j+1).getStringCellValue();
arr[arrint++]=row.getCell(j+2).getStringCellValue();
arrint = 0;
}
}
//add to list
catch(Exception e){
System.out.println("Row couldn't be read since:"+ e);
}
}
if(arr[0]!=null)
lst.add(arr);
}
return lst.iterator();
}
}