【问题标题】:How to read multiple sets of value in the excel sheet using DataProvider in Selenium WebDriver with Java and TestNG如何在 Selenium WebDriver 中使用 Java 和 TestNG 中的 DataProvider 读取 excel 表中的多组值
【发布时间】:2014-06-10 08:17:56
【问题描述】:

我对 Selenium WebDriver 还很陌生。我编写了代码来使用数据提供程序从 excel 表中读取登录凭据和值。 它贯穿第一个设置数据(登录功能),完美地给了我绿色状态栏。

在我的应用程序中,登录后,我想通过从同一个 Excel 表中发送索引和选择(在管理方法中)来选择值,但我未能读取值。 对于硬编码值,它工作正常。

谁能告诉我如何写它。 用过的 Excel 表:

下面是我的代码:

import java.io.File;

import jxl.Sheet;
import jxl.Workbook;

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.AfterClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.testng.annotations.BeforeClass;

public class TestCase {

String[][] tabArray = null;
Workbook workbk;
Sheet sheet;
int rowCount, colCount;
String sheetPath = "test/Resources/Data/Auto_Increment.xls";
WebDriver login;
//int eRow, eCol, sRow = 0, sCol = 0;


@BeforeSuite
public void setUp(){
    login = new FirefoxDriver();
    login.get("http://etazo.tangosoftware.com");
    System.out.println("select the etazo web link..");
}

@DataProvider
public Object[][] getLoginData() throws Exception {
    Object[][] retObjArr = getExcelData(sheetPath, "Sheet1");
    System.out.println("getData function executed!!");
    return retObjArr;
}

//  Excel API to read test data from excel workbook
public String[][] getExcelData(String xlPath, String shtName)
        throws Exception {
    Workbook workbk = Workbook.getWorkbook(new File(xlPath));
    Sheet sht = workbk.getSheet(shtName);
    rowCount = sht.getRows();
    colCount = sht.getColumns();
    tabArray = new String[rowCount][colCount - 2];
    System.out.println("erow: " + rowCount);
    System.out.println("ecol: " + colCount);
    for (int i = 0; i < rowCount; i++) {
        for (int j = 0; j < 3; j++) {
            tabArray[i][j] = sht.getCell(j, i).getContents();
        }
    }
    return (tabArray);
}

@Test(dataProvider = "getLoginData")
public void LoginData(String distID, String asmtId, String studID)
        throws InterruptedException, BiffException, IOException {
    Administartion(distID, asmtId, studID);
}
public void Administartion(String distID, String asmtId, String studID)
        throws BiffException, IOException {
    Workbook workbk = Workbook.getWorkbook(new File(sheetPath));
    Sheet sht = workbk.getSheet("Sheet1");
    int currRow = sht.findCell(studID).getRow();
    //login.findElement(By.xpath("//*[@id='question-"+sIndex+"']/bubbles/circle["+sValue+"]")).click();     
    System.out.println(sht.getCell(3, currRow).getContents() + " Index ");
    System.out.println(sht.getCell(4, currRow).getContents() + " Answer selection");
}

}

【问题讨论】:

  • 尝试将“distID,”asmtId,“studID”作为参数传递给“Administration()”函数..
  • 您好 Praveen,我在管理功能中参数化了“distId”、“asmtID”、“StudID”。它取值但登录后不选择任何值。我已删除 LoginData 方法,但它对我不起作用。
  • 嗨,你是对的。当您调用“Adminstration()”时,将相同的参数传递给它,例如“Administration(distId, asmtId, studId)”。这将使变量即使在“administration()”中也可用。
  • 我只做了完全相同的事情。但我担心的是登录后我必须选择一些值,但我无法选择这些值。它适用于登录功能。 Bz 在 getExcelMethod() 中只发送登录参数。我没有得到我必须参数索引和值的确切位置。请帮我解决这个问题。
  • 当然,我必须复制它才能理解/评论它。你能分享/展示你的 excel 表的样子吗?

标签: excel selenium-webdriver testng testng-dataprovider


【解决方案1】:

编辑了代码。现在这将获取所有记录。

您的 Excel 工作表应类似于以下内容:

import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClass {
    String[][] tabArray = null;
    Workbook workbk;
    Sheet sheet;
    int rowCount, colCount;
    String sheetPath = "D:\\Download\\Auto_Increment.xls";

    // Excel API to read test data from excel workbook
    public String[][] getExcelData(String xlPath, String shtName)
            throws Exception {
        Workbook workbk = Workbook.getWorkbook(new File(xlPath));
        Sheet sht = workbk.getSheet(shtName);
        rowCount = sht.getRows();
        colCount = sht.getColumns();
        tabArray = new String[rowCount][colCount - 2];
        System.out.println("erow: " + rowCount);
        System.out.println("ecol: " + colCount);
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < 3; j++) {
                tabArray[i][j] = sht.getCell(j, i).getContents();
            }
        }
        return (tabArray);
    }

    @DataProvider
    public Object[][] getLoginData() throws Exception {
        Object[][] retObjArr = getExcelData(sheetPath, "Sheet1");
        System.out.println("getData function executed!!");
        return retObjArr;
    }

    @Test(dataProvider = "getLoginData")
    public void LoginData(String distID, String asmtId, String studID)
            throws InterruptedException, BiffException, IOException {
        Administartion(distID, asmtId, studID);
    }

    public void Administartion(String distID, String asmtId, String studID)
            throws BiffException, IOException {
        Workbook workbk = Workbook.getWorkbook(new File(sheetPath));
        Sheet sht = workbk.getSheet("Sheet1");
        int currRow = sht.findCell(studID).getRow();
        System.out.println(sht.getCell(3, currRow).getContents() + " Index ");
        System.out.println(sht.getCell(4, currRow).getContents() + " Answer selection");
    }
}

【讨论】:

    【解决方案2】:

    使用的依赖项:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.13</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.13</version>
    </dependency>
    
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        <scope>test</scope>
    </dependency>
    

    代码在这里读取 xlsx 文件:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    
    public class TestDataProvider {
        static File resultFile = new File(“PATH OF THE EXCEL”);
        public static DataFormatter formatter = new DataFormatter();
    
        @Test(dataProvider = "readExcelFile")
        public static void test1(String slNum, String name, String Address, String email) throws  IOException {
            System.out.println(slNum+" "+ name+" "+Address + " "+email);
    
        FURTHER CODE GOES HERE
    
    
        }
    
        @DataProvider
        public static Object[][] readExcelFile() throws InvalidFormatException, IOException {
            FileInputStream fis = new FileInputStream(resultFile);
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            XSSFSheet sh = wb.getSheet(“SHEET NAME”);
    
            System.out.println(sh.getPhysicalNumberOfRows());
            System.out.println(sh.getRow(0).getPhysicalNumberOfCells());
            int RowNum = sh.getPhysicalNumberOfRows();
            int ColNum = sh.getRow(0).getPhysicalNumberOfCells();
    
            String[][] xlData = new String[RowNum-1][ColNum];
    
            for (int i = 0; i < RowNum - 1; i++) 
            {
                XSSFRow row = sh.getRow(i + 1);
                for (int j = 0; j < ColNum; j++) 
                {
                    if (row == null)
                        xlData[i][j] = "";
                    else {
                        XSSFCell cell = row.getCell(j);                 
                        if (cell == null)
                            xlData[i][j] = ""; 
                        else {
                            String value = formatter.formatCellValue(cell);
                            xlData[i][j] = value.trim();                        
                        }
                    }
                }
            }       
            return xlData;
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2016-03-20
      • 1970-01-01
      • 2015-12-30
      • 2016-12-24
      相关资源
      最近更新 更多