我正在使用从 Excel 获取的用户名和密码启动应用程序。我在
.sendKeys()
中遇到错误。
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Readdata {
public FileInputStream fileInput;
static String path;
public File testdatafile;
public XSSFWorkbook workbook;
public XSSFSheet sheet;
public XSSFRow Row;
Readdata(String path) {
this.path = path;
}
public static int getRowcount(String sheetname) throws IOException {
File testDataFile = new File(path);
FileInputStream fileInput = new FileInputStream(testDataFile);
XSSFWorkbook workbook = new XSSFWorkbook(fileInput);
XSSFSheet sheet = workbook.getSheet(sheetname);
Row row = sheet.getRow(0);
int rowCount = sheet.getLastRowNum();
workbook.close();
fileInput.close();
return rowCount;
}
public static int getCellCount(String sheetname, int rownum) throws IOException {
File testDataFile = new File(path);
FileInputStream fileInput = new FileInputStream(testDataFile);
XSSFWorkbook workbook = new XSSFWorkbook(fileInput);
XSSFSheet sheet = workbook.getSheet(sheetname);
XSSFRow Row = sheet.getRow(rownum);
int cellcount = Row.getLastCellNum();
workbook.close();
return cellcount;
}
public static String getCelldata(String sheetname, int rownum, int colnum) throws IOException {
File testDataFile = new File(path);
FileInputStream fileInput = new FileInputStream(testDataFile);
XSSFWorkbook workbook = new XSSFWorkbook(fileInput);
XSSFSheet sheet = workbook.getSheet(sheetname);
XSSFRow Row = sheet.getRow(rownum);
XSSFCell cell = Row.getCell(colnum);
DataFormatter fm = new DataFormatter();
String data;
try {
data = fm.formatCellValue(cell);
} catch (Exception e) {
data = "";
}
workbook.close();
fileInput.close();
return data;
}
}
启动浏览器并从 Excel 文件读取数据。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.time.Duration;
public class Excelreader {
@Test(dataProvider = "DataValidate")
public static void Admin() throws IOException, InterruptedException, IllegalArgumentException {
String Un = null;
String Password = null;
String exp = null;
WebDriver driver = new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
Thread.sleep(1000);
driver.findElement(By.xpath("//input[@name='username']")).clear();
Thread.sleep(1000);
WebElement user = driver.findElement(By.xpath("//input[@name='username']"));
user.sendKeys(Un);
Thread.sleep(1000);
driver.findElement(By.xpath("//input[@name='password']")).clear();
Thread.sleep(1000);
WebElement pass = driver.findElement(By.xpath("//input[@name='password']"));
pass.sendKeys(Password);
Thread.sleep(1000);
driver.findElement(By.xpath("//button[@type='submit']")).click();
String exp_title = "https://opensource-demo.orangehrmlive.com/web/index.php/admin/viewSystemUsers";
String act_title = driver.getTitle();
if (exp.equals("Valid")) {
if (exp_title.equals(act_title)) {
driver.findElement(By.xpath("//*[@id='app']/div[1]/div[1]/header/div[1]/div[2]/ul/li/span/p")).click();
driver.findElement(By.xpath("//*[@id='app']/div[1]/div[1]/header/div[1]/div[2]/ul/li/ul/li[4]/a")).click();
Assert.assertTrue(true);
}
} else if (exp.equals("Invalid")) {
if (exp_title.equals(act_title)) {
driver.findElement(By.xpath("//*[@id='app']/div[1]/div[1]/header/div[1]/div[2]/ul/li/span/p")).click();
driver.findElement(By.xpath("//*[@id='app']/div[1]/div[1]/header/div[1]/div[2]/ul/li/ul/li[4]/a")).click();
Assert.assertTrue(false);
}
}
}
@DataProvider(name = "DataValidate")
public Object[][] getdata() throws IOException {
String path = "src/main/resources/Excelfile.xlsx";
Readdata dt = new Readdata(path);
int totalrowc = dt.getRowcount("Login");
int totalc = dt.getCellCount("Login", 1);
String logindata[][] = new String[totalrowc][totalc];
for (int i = 1; i < totalrowc; i++) {
for (int j = 1; j < totalc; j++) {
logindata[i][j] = dt.getCelldata("Login", i, j);
}
}
return logindata;
}
}
我尝试了上面的代码,但它在第 26 行给出了错误,即
WebElement user = driver.findElement(By.xpath("//input[@name='username']"));
user.sendKeys(un);
WebElement pass = driver.findElement(By.xpath("//input[@name='password']"));
pass.sendKeys(pw);
您收到的错误(
java.lang.IllegalArgumentException: Keys to send should be a not null CharSequence
)来自这一行:
user.sendKeys(Un);
它告诉你
Un
是null
。
仔细看你的代码,不难看出确实是这样的。代码中唯一提到变量
Un
的另一行是这一行:
String Un = null;
这声明了变量并将其值设置为
null
。当您到达user.sendKeys(Un)
行时,该变量的值没有任何变化,因此出现异常。
如果您希望将从 Excel 电子表格读取的值传递到您的测试方法中,那么您需要将它们指定为方法参数:
public static void Admin(String Un, String Password, String exp) throws IOException, InterruptedException, IllegalArgumentException {
// declarations of variables Un, Password and exp removed as they are now parameters
WebDriver driver = new ChromeDriver();
// rest of method ...
最后,您可能需要调整
getdata()
方法以忽略电子表格的标题行。