在 Webdriver 中使用 Java 创建结果 Excel 文件时出现 java.lang.NullPointerException。
创建的结果excel文件中的第一行被创建,第二行在workbook.write()处给出“java.lang.NullPointerException”,即。wwb.write() 在我的代码中。
请找到我编写的代码和收到的错误。
public class AccProfile_Zipfield_Validations1 extends SiteSecurity_FieldValidations
{
String msg = null;
// Creating New Account Profile
@Test
public void AccountProfile_City_Validations() throws IOException, BiffException, InterruptedException, RowsExceededException, WriteException
//public static void main(String args[]) throws IOException, BiffException, InterruptedException, RowsExceededException, WriteException
{
// creating properties files storing the ID's and xpaths
APPLICATION_LOGS.debug("Starting the test suite");
APPLICATION_LOGS.debug("Loading config files");
config = new Properties();
//FileInputStream fp = new FileInputStream("./config.properties");
FileInputStream fp = new FileInputStream(System.getProperty("user.dir")+"\\src\\com\\ode\\utility\\config.properties");
config.load(fp);
APPLICATION_LOGS.debug("Loading Object XPATHS");
OR = new Properties();
//FileInputStream fp1 = new FileInputStream("./OR.properties");
FileInputStream fp1 = new FileInputStream(System.getProperty("user.dir")+"\\src\\com\\ode\\utility\\OR.properties");
OR.load(fp1);
//Sleep Times
long Admin_edit = Long.valueOf(config.getProperty("config_edit"));
long Admin_zipcode = Long.valueOf(config.getProperty("config_zipcode"));
long Admin_cityfield = Long.valueOf(config.getProperty("config_cityfield"));
long Admin_screenshot = Long.valueOf(config.getProperty("config_screenshot"));
long Admin_checkbox = Long.valueOf(config.getProperty("config_checkbox"));
long Admin_save = Long.valueOf(config.getProperty("config_save"));
//Take the data from input xls file
//FileInputStream fi =new FileInputStream("./Credit_Admin_Login.xls");
FileInputStream fi =new FileInputStream(System.getProperty("user.dir")+"\\src\\com\\ode\\resources\\Credit_AdminUser_Login.xls");
Workbook wb = Workbook.getWorkbook(fi);
Sheet sh = wb.getSheet("Credit_AdminUserLogin");
//Take the data from input xls file
//FileInputStream fi =new FileInputStream("./Credit_AccProfile_FV.xls");
FileInputStream fi1 =new FileInputStream(System.getProperty("user.dir")+"\\src\\com\\ode\\resources\\Credit_AccProfile_FV.xls");
Workbook wb1 = Workbook.getWorkbook(fi1);
Sheet sh1 = wb1.getSheet("700Credit_FieldValidations_City");
//Create the Result xls file
FileOutputStream fo = new FileOutputStream("./700Credit_AccProfile_FV_"+cdate+".xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet("700Credit_FV_City_Results", 0);
System.out.println(ws.getName());
Thread.sleep(Admin_screenshot);
System.out.println(" ******** City field validations ******** ");
System.out.println();
System.out.println("Random Number generated: "+Randnum);
System.out.println("Name for Account creation: "+AccName+"_City_"+Randnum);
System.out.println("Current Date generated: "+cdate);
APPLICATION_LOGS.debug("Starting the driver");
driver = new InternetExplorerDriver();
driver.get(config.getProperty("Testwebsite"));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for (int i = 1; i < sh.getRows(); i++)
{
driver.findElement(By.id(OR.getProperty("signIN_username_ID"))).clear();
driver.findElement(By.id(OR.getProperty("signIN_username_ID"))).sendKeys(sh.getCell(0, i).getContents());
driver.findElement(By.id(OR.getProperty("signIN_password_ID"))).clear();
driver.findElement(By.id(OR.getProperty("signIN_password_ID"))).sendKeys(sh.getCell(1, i).getContents());
driver.findElement(By.id(OR.getProperty("signIN_LogIN_ID"))).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
System.out.println("Logged in as Site Admin User");
//Click on Add New button
driver.findElement(By.name(OR.getProperty("AddNew_AccProf_ID"))).click();
Thread.sleep(Admin_edit);
System.out.println("Clicked on Add button for creating New Account");
//Entering data in the Account Information section
driver.findElement(By.name(OR.getProperty("AccName_Id"))).clear();
driver.findElement(By.name(OR.getProperty("AccName_Id"))).sendKeys(AccName+"_City_"+Randnum);
driver.findElement(By.name(OR.getProperty("AccDBA_ID"))).sendKeys("Kumar_DBA");
String accTaxId = OR.getProperty("AccTax_ID");
WebElement accTaxInput = driver.findElement(By.id(accTaxId));
accTaxInput.clear();
accTaxInput.sendKeys(String.valueOf(12345));
accTaxInput = driver.findElement(By.id(accTaxId));
driver.findElement(By.name(OR.getProperty("AccAddress_ID"))).sendKeys("124 - City Cross Roads");
String accZipID = OR.getProperty("AccZip_ID");
WebElement accZipInput = driver.findElement(By.id(accZipID));
accZipInput.clear();
accZipInput.sendKeys(String.valueOf(79081));
accZipInput = driver.findElement(By.id(accZipID));
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccZip_ID"))), "").perform();
Thread.sleep(Admin_zipcode);
//Entering data in the Main Contact Information section
driver.findElement(By.name(OR.getProperty("AccFName_ID"))).sendKeys("Peter");
Thread.sleep(Admin_cityfield);
for (int j=1; j<sh1.getRows(); j++)
{
System.out.println();
System.out.println("Test Scenario - "+sh1.getCell(3, j).getContents());
if(j==1)
{
//Sending Numeric values to City Field.
driver.findElement(By.id(OR.getProperty("AccCity_ID"))).clear();
driver.findElement(By.id(OR.getProperty("AccCity_ID"))).sendKeys(sh1.getCell(1, j).getContents());
Thread.sleep(Admin_zipcode);
//Switch the driver context to the alert
Alert alertDialog = driver.switchTo().alert();
//Get the alert text
String alertText = alertDialog.getText();
System.out.println("Message displayed on Popup Window when entered "+sh1.getCell(5, j).getContents()+" in City field: "+alertText);
//Click the OK button on the alert.
alertDialog.accept();
msg = alertText;
//Taking Screen shot of the Application
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./screenshots\\Admin_AccProfile_Field_Level_Validations_"+cdate+"\\Cityfield\\Admin_AccountProfile_"+sh1.getCell(5, j).getContents()+" in City field.jpg"));
Thread.sleep(Admin_checkbox);
}
else if (j==2)
{
driver.findElement(By.name(OR.getProperty("AccFName_ID"))).sendKeys("Peter");
driver.findElement(By.name(OR.getProperty("AccMName_ID"))).sendKeys("S");
driver.findElement(By.name(OR.getProperty("AccLName_ID"))).sendKeys("Mark");
driver.findElement(By.id(OR.getProperty("save_ID"))).click();
Thread.sleep(Admin_cityfield);
Alert alertDialog = driver.switchTo().alert();
//Get the alert text
String alertText = alertDialog.getText();
System.out.println("Message displayed on Popup Window when City field is left blank: "+alertText);
//Click the OK button on the alert.
alertDialog.accept();
msg = alertText;
//Taking Screen shot of the Application
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./screenshots\\Admin_AccProfile_Field_Level_Validations_"+cdate+"\\Cityfield\\Admin_AccountProfile_with_Cityfield_blank.jpg"));
Thread.sleep(Admin_zipcode);
driver.findElement(By.id(OR.getProperty("save_ID"))).click();
Thread.sleep(Admin_zipcode);
alertDialog = driver.switchTo().alert();
//Get the alert text
alertText = alertDialog.getText();
//Click the OK button on the alert.
alertDialog.accept();
//driver.findElement(By.id(OR.getProperty("AccCity_ID"))).sendKeys("GENERAL ELECTRIC");
driver.findElement(By.id(OR.getProperty("AccCity_ID"))).sendKeys(sh1.getCell(1, j).getContents());
//Taking Screen shot of the Application
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./screenshots\\Admin_AccProfile_Field_Level_Validations_"+cdate+"\\Cityfield\\Admin_AccountProfile_with_Cityfield_other city name.jpg"));
Thread.sleep(Admin_screenshot);
String accOffNoID = OR.getProperty("AccOffNo_ID");
WebElement accOffNoInput = driver.findElement(By.id(accOffNoID));
accOffNoInput.clear();
accOffNoInput.sendKeys(String.valueOf(9874651230L));
accOffNoInput = driver.findElement(By.id(accOffNoID));
String accCellNoID = OR.getProperty("AccCellNo_ID");
WebElement accCellNoInput = driver.findElement(By.id(accCellNoID));
accCellNoInput.clear();
accCellNoInput.sendKeys(String.valueOf(9632587110L));
accCellNoInput = driver.findElement(By.id(accCellNoID));
String accFaxNoID = OR.getProperty("AccFaxNo_ID");
WebElement accFaxNoInput = driver.findElement(By.id(accFaxNoID));
accFaxNoInput.clear();
accFaxNoInput.sendKeys(String.valueOf(9512368741L));
accFaxNoInput = driver.findElement(By.id(accFaxNoID));
driver.findElement(By.name(OR.getProperty("AccEmail_ID"))).sendKeys("abc@abc.com");
driver.findElement(By.id(OR.getProperty("txtNAD_ID"))).sendKeys("LE0472");
System.out.println("Entered all the required information for creating the Account");
driver.findElement(By.id(OR.getProperty("save_ID"))).click();
Thread.sleep(Admin_save);
System.out.println("Successfully created the Account: "+AccName+"_City_"+Randnum);
//Taking Screen shot of the Application
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./screenshots\\Admin_AccProfile_Field_Level_Validations_"+cdate+"\\Cityfield\\Admin_NewAccountCreated.jpg"));
Thread.sleep(Admin_screenshot);
System.out.println();
driver.close();
}
for (int k = 0; k < sh1.getColumns(); k++)
{
Label lab=new Label(k,j,sh1.getCell(k, j).getContents());
ws.addCell(lab);
Label result = new Label(2,j, msg);
ws.addCell(result);
String c2String = sh1.getCell(2, j).getContents().toString();
if(c2String.equalsIgnoreCase(msg))
{
Label status=new Label(5,j,"Pass");
ws.addCell(status);
}
else
{
Label status=new Label(5,j,"Fail");
ws.addCell(status);
}
}
//Add the 4 Labels
Label un= new Label(0,0,"Test Data");
Label pw= new Label(1,0,"City Code");
Label Desc= new Label(2,0,"Error Message");
Label st= new Label(3,0,"Scenario#");
Label tsd = new Label(4,0,"Test Scenario Description");
Label rs = new Label(5,0,"Results");
ws.addCell(un);
ws.addCell(pw);
ws.addCell(Desc);
ws.addCell(st);
ws.addCell(tsd);
ws.addCell(rs);
wwb.write();
wwb.close();
}
}
}
}
收到错误消息:
FAILED: AccountProfile_City_Validations
java.lang.NullPointerException
at jxl.write.biff.File.write(File.java:149)
at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:697)
at com.ode.Admin.AccProfile_Zipfield_Validations1.AccountProfile_City_Validations(AccProfile_Zipfield_Validations1.java:258)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.run(TestNG.java:1036)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
请帮我解决这个问题。帮助将不胜感激。