我正在尝试编写从 excel 文件中读取数据的代码。excel 文件有 7 列,前 5 列由字符串类型数据组成,后 2 列由整数类型数据组成。现在我想将此数据发送到网页的形式。
我的代码有问题:我的代码将整个 excel 数据作为字符串返回,因为我只需要前 5 列作为字符串,最后 2 列作为整数。另外,虽然网页表单的所有字段都是文本字段,但我不确定为什么当我发送字符串数据时这些字段会抛出错误“格式无效”。您能否查看以下代码并帮助我解决此问题。
@Test public void SampleCustNumFormat() 抛出异常 {
String vURL;
String vEmail;
String vPswd;
String vCnfmPswd;
int vCustAccNum;
int vZipCode;
long iWait;
int Size;
// Read Test Data from Excel
String xlPath = "C:\\SCE docs\\Automation\\InputData_Registration.xls";
ArrayList<CustomerData> customerData = getExcelData(xlPath,"AccountHolderRegistration");
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
iWait=3000;
for (int k=1; k<xRows; k++)
{
driver.navigate().to(vURL);
driver.findElement(By.linkText("Register")).click();
driver.findElement(By.id("emailInputBox_input")).clear();
driver.findElement(By.id("emailInputBox_input")).sendKeys(vEmail);
driver.findElement(By.id("pwdInputBox_input")).clear();
driver.findElement(By.id("pwdInputBox_input")).sendKeys(vPswd);
driver.findElement(By.id("confirmpwd_input")).clear();
driver.findElement(By.id("confirmpwd_input")).sendKeys(vCnfmPswd);
driver.findElement(By.id("radio1")).click();
driver.findElement(By.id("accountInputBox_input")).sendKeys(Integer.toString(vCustAccNum));
driver.findElement(By.id("accountInputBox_input")).clear();
driver.findElement(By.id("zipcodeBox_input")).sendKeys(Integer.toString(vZipCode));
driver.findElement(By.id("zipcodeBox_input")).clear();
driver.findElement(By.id("terms3")).click();
/* driver.findElement(By.id("registerButton_label")).click();
driver.findElement(By.id("continueButton_label")).click();
Thread.sleep(iWait);
*/
}
}
// Custom Methods
public ArrayList<CustomerData> getExcelData(String Path, String shtName)
throws Exception {
ArrayList<CustomerData> customerDataList = new ArrayList<CustomerData>();
File myxl = new File(Path);
FileInputStream fi = new FileInputStream(myxl);
HSSFWorkbook myWB = new HSSFWorkbook(fi);
HSSFSheet mySheet = myWB.getSheet(shtName);
xRows = mySheet.getLastRowNum() + 1;
xCols = mySheet.getRow(0).getLastCellNum();
for (int i = 1; i < xRows; i++) {
HSSFRow row = mySheet.getRow(i);
CustomerData customerDetails = new CustomerData();
customerDetails.setvURL(row.getCell(1).getStringCellValue());
customerDetails.setvEmail(row.getCell(2).getStringCellValue());
customerDetails.setvPswd(row.getCell(3).getStringCellValue());
customerDetails.setvCnfmPswd(row.getCell(4).getStringCellValue());
customerDetails.setvCustAccNum((int) row.getCell(5).getNumericCellValue());
customerDetails.setvZipCode((int) row.getCell(6).getNumericCellValue());
customerDataList.add(customerDetails);
System.out.println("row 2: "+row.getCell(3));
// row.getCell(2).setCellValue(convertFormatCustNo(customerDetails.getCustNo()));
}
return customerDataList;
}