gpt4 book ai didi

excel - 如何在 Selenium WebDriver 中使用 Java 和 TestNG 中的 DataProvider 读取 excel 表中的多组值

转载 作者:行者123 更新时间:2023-12-04 21:05:06 26 4
gpt4 key购买 nike

我对 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");
}

}

最佳答案

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

您的 Excel 工作表应该类似于以下内容:
enter image description here

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

关于excel - 如何在 Selenium WebDriver 中使用 Java 和 TestNG 中的 DataProvider 读取 excel 表中的多组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24136081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com