Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium:Selenium:如何基于给定关键字(例如:valid/invalid)从Excel工作表中仅读取特定行_Excel_Selenium Webdriver_Testng - Fatal编程技术网

Selenium:Selenium:如何基于给定关键字(例如:valid/invalid)从Excel工作表中仅读取特定行

Selenium:Selenium:如何基于给定关键字(例如:valid/invalid)从Excel工作表中仅读取特定行,excel,selenium-webdriver,testng,Excel,Selenium Webdriver,Testng,我试图在selenium中构建一个虚拟框架,该框架从excel工作表中获取/读取,该工作表有3个值: 现在,我的代码以TestNg登录方法开始,该方法触发ReadExcelsheet方法: public class LoginTest extends BaseTest { @Test(dataProvider = "dataForSheet",dataProviderClass = ExcelRW.class) public void testSeccussfulLogin(String K

我试图在selenium中构建一个虚拟框架,该框架从excel工作表中获取/读取,该工作表有3个值:

现在,我的代码以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();
}
公共类Dp\u ExcelRW{
@数据提供者(name=“dataForSheet”)
公共静态迭代器loginData()引发异常{
迭代器arrayObject=Read(“E:\\F\\Excel\u Sheet\\Test.xlsx”,“Sheet2”);
返回arrayObject;
}
公共静态迭代器读取(字符串表地址、字符串表)引发异常{
FileInputStream fis=新的FileInputStream(SheetAddress);
工作簿wb=WorkbookFactory.create(fis);
表sh=wb.getSheet(表);
int rowCount=sh.getLastRowNum();
int columnCount=1;sh.getRow(0.getPhysicalNumberOfCells();
//创建列表
List lst=new ArrayList();

对于(inti=1;iHi),我在您的代码中做了一些更改,如果有帮助,请告诉我

    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();
}
}
package com.read.files;
导入java.io.FileInputStream;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.usermodel.WorkbookFactory;
导入org.testng.annotations.DataProvider;
公共类StackOverFlowQuest{
@数据提供者(name=“dataForSheet”)
公共静态迭代器loginData()引发异常{
迭代器arrayObject=Read(System.getProperty(“user.dir”)+“\\needful\\Book1.xlsx”,“Sheet3”);
返回arrayObject;
}
公共静态迭代器读取(字符串表地址、字符串表)引发异常{
FileInputStream fis=新的FileInputStream(SheetAddress);
工作簿wb=WorkbookFactory.create(fis);
表sh=wb.getSheet(表);
int rowCount=sh.getLastRowNum();
int columnCount=1;sh.getRow(0.getPhysicalNumberOfCells();
int-arrint=0;
//创建列表
List lst=new ArrayList();

对于(int i=1;i No)这没有。它仍然读取3个值,因此运行TestSeccusfullLogin()3次。对于无效的情况,它启动浏览器并导航到URL,因为@BeforeMethod每次都在TestSeccusfullLogin()之前运行。