Java 将数据从Excel工作表传递到DataProvider
我正在做一个测试多个凭证的项目,所以我写了一个脚本来访问excel文件,然后逐个检查。 下面的代码用于检查SeleniumWeb驱动程序的登录状态。问题是,当我添加SeleniumWeb驱动程序jar时,它们没有添加测试库问题 用excel工作表测试多次登录是否有其他方法 提前谢谢Java 将数据从Excel工作表传递到DataProvider,java,excel,selenium,selenium-webdriver,Java,Excel,Selenium,Selenium Webdriver,我正在做一个测试多个凭证的项目,所以我写了一个脚本来访问excel文件,然后逐个检查。 下面的代码用于检查SeleniumWeb驱动程序的登录状态。问题是,当我添加SeleniumWeb驱动程序jar时,它们没有添加测试库问题 用excel工作表测试多次登录是否有其他方法 提前谢谢 import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
**import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;**here is some issue this lib is not added in selenium jar.
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcelDataProvider {
public WebDriver driver;
public WebDriverWait wait;
String appURL = "https://www.linkedin.com/";
//Locators
private By byEmail = By.id("session_key-login");
private By byPassword = By.id("session_password-login");
private By bySubmit = By.id("signin");
private By byError = By.id("global-alert-queue");
@BeforeClass
public void testSetup() {
driver=new FirefoxDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 5);
}
@Test(dataProvider="empLogin")
public void VerifyInvalidLogin(String userName, String password) {
driver.navigate().to(appURL);
driver.findElement(byEmail).sendKeys(userName);
driver.findElement(byPassword).sendKeys(password);
//wait for element to be visible and perform click
wait.until(ExpectedConditions.visibilityOfElementLocated(bySubmit));
driver.findElement(bySubmit).click();
//Check for error message
wait.until(ExpectedConditions.presenceOfElementLocated(byError));
String actualErrorDisplayed = driver.findElement(byError).getText();
String requiredErrorMessage = "Please correct the marked field(s) below.";
Assert.assertEquals(requiredErrorMessage, actualErrorDisplayed);
}
@DataProvider(name="empLogin")
public Object[][] loginData() {
Object[][] arrayObject = getExcelData("D:/sampledoc.xls","Sheet1");
return arrayObject;
}
/**
* @param File Name
* @param Sheet Name
* @return
*/
public String[][] getExcelData(String fileName, String sheetName) {
String[][] arrayExcelData = null;
try {
FileInputStream fs = new FileInputStream(fileName);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet(sheetName);
int totalNoOfCols = sh.getColumns();
int totalNoOfRows = sh.getRows();
arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];
for (int i= 1 ; i < totalNoOfRows; i++) {
for (int j=0; j < totalNoOfCols; j++) {
arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
return arrayExcelData;
}
@Test
public void tearDown() {
driver.quit();
}
}
import java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入org.testng.Assert;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
**导入org.testng.annotations.BeforeClass;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Test**这里有一个问题,这个库没有添加到selenium jar中。
进口jxl.Sheet;
导入jxl.工作簿;
导入jxl.read.biff.BiffException;
公共类ReadExcelDataProvider{
公共网络驱动程序;
公共网络驱动器,它等待;
字符串appURL=”https://www.linkedin.com/";
//定位器
私人通过电子邮件=通过.id(“会话_密钥登录”);
私用byPassword=By.id(“会话_密码登录”);
私人bySubmit=By.id(“签名”);
private By byError=By.id(“全局警报队列”);
@课前
公共void testSetup(){
驱动程序=新的FirefoxDriver();
driver.manage().window().maximize();
等待=新的WebDriverWait(驱动程序,5);
}
@测试(dataProvider=“empLogin”)
public void VerifyInvalidLogin(字符串用户名、字符串密码){
driver.navigate().到(appURL);
driver.findElement(通过电子邮件)、sendKeys(用户名);
driver.findElement(绕过Word).sendKeys(密码);
//等待元素可见并执行单击
等待。直到(预期条件。通过Submit定位的元素的可视性);
driver.findElement(bySubmit.click();
//检查错误消息
等待。直到(预期条件。元素存在已定位(错误));
字符串actualErrorDisplayed=driver.findElement(byError.getText();
String requiredErrorMessage=“请更正下面标记的字段。”;
Assert.assertEquals(requiredErrorMessage,actualErrorDisplayed);
}
@数据提供者(name=“empLogin”)
公共对象[][]登录数据(){
对象[][]arrayObject=getExcelData(“D:/sampledoc.xls”,“Sheet1”);
返回arrayObject;
}
/**
*@param文件名
*@param表名
*@返回
*/
公共字符串[][]getExcelData(字符串文件名、字符串名称){
字符串[][]arrayExcelData=null;
试一试{
FileInputStream fs=新的FileInputStream(文件名);
工作簿wb=Workbook.getWorkbook(fs);
Sheet sh=wb.getSheet(sheetName);
int totalNoOfCols=sh.getColumns();
int totalNoOfRows=sh.getRows();
arrayExcelData=新字符串[totalNoOfRows-1][totalNoOfCols];
for(inti=1;i
您添加了哪个Jar?selenium-java-3.5.2这一个您可以从构建路径添加Jar。我可以发送如何添加吗?是的,我在它的构建路径上添加了,然后在lib中添加了jar,这很好,但是它们不起作用。请显示错误您添加了哪个jar?selenium-java-3.5.2这一个您可以从构建路径添加jar。我可以发送如何添加吗?是的,我在它的构建路径上添加了lib,然后添加jar,这很好,但它们不起作用请显示错误