Selenium TestNG中的java.lang.NullPointerException

时间:2015-07-10 08:18:46

标签: java selenium

我是学习Selenium WebDriver和Java的一周新手。

我尝试创建一个使用TestNG运行的测试脚本。

我有2个java文件和1个testng.xml,如下所示:

[1] adminBase.java

package automation;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class adminBase {
static WebDriver driver;
public static String appURL = "https://00.0.00.00:0000/admin/";
public static String testData = "C:\src\TestData.xls";

//Declare fields on pageObject that are webElements
@FindBy(id = "j_username")
static WebElement username;
@CacheLookup

@FindBy(id = "j_password")
static WebElement password;
@CacheLookup

@FindBy(id = "login")
static WebElement loginBtn;

@FindBy(xpath = ".//*[@id='LogoutForm']/li/span/a")
static WebElement logoutBtn;


//This method will take two arguments (Username and Password)
public void loginInfo(String sUsername, String sPassword){
    username.sendKeys(sUsername);
    password.sendKeys(sPassword);
    loginBtn.click();
    }

//To read Excel file
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();
    }
    catch (BiffException e){
        e.printStackTrace();
    }
    return arrayExcelData;
}
}

以及[2] adminLogin.java

package automation;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;

public class adminLogin {
WebDriver driver;
adminBase adminbase;
String testData;

@BeforeMethod
public void beforeTest() throws Exception {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //adminbase = PageFactory.initElements(driver, adminBase.class);
}

@DataProvider(name="loginInfo")
public Object[][] getData() throws Exception {
    Object[][] user = adminbase.getExcelData(testData, "adminLogin");
    return user;
}

@Test(dataProvider="loginInfo")
public void test(String appURL, String sUsername, String sPassword)
throws Exception {
    driver.get(appURL);
    adminbase.loginInfo(sUsername, sPassword);
}

@AfterMethod
public void afterTest() throws InterruptedException{
    Thread.sleep(3000);
    driver.quit();
}
}

[3] testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
  <classes>
    <class name = "automation.adminLogin"/>
  </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

然而,我得到了以下异常

java.lang.RuntimeException: java.lang.NullPointerException
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161)
at org.testng.internal.Parameters.handleParameters(Parameters.java:429)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
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)
Caused by: java.lang.NullPointerException
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at automation.adminBase.getExcelData(adminBase.java:48)
    at automation.adminLogin.getData(adminLogin.java:27)
    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:84)
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:135)
    ... 20 more

根据我的理解,行 automation.adminLogin.getData(adminLogin.java:27)

adminLogin.java文件中的第27行是这一行:

(Object[][] user = adminbase.getExcelData(testData, "adminLogin");

我理解错误可能是由于我传递的参数为null。但我只是不知道如何解决,因为我的文件中没有任何错误

我的代码真的很乱。因为我只是混搭。 我真的很抱歉看到我的代码让人头疼。 &GT;。&LT;

请评论并帮助我改进。 谢谢!

2 个答案:

答案 0 :(得分:0)

您似乎没有在AdminLogin类的任何位置设置adminbase变量。

只需替换:

adminBase adminbase;

使用:

adminBase adminbase = new adminBase();

如果你只想要一个adminBase类的实例。

同样按照惯例,建议使用大写字母开头的类名。 (即AdminBase和AdminLogin)

注意: 这个答案只是解决了你当前的问题。您的代码实际上可以用一些整体改进。如果您希望其他人详细审核您的代码并指导您如何改进代码,我建议您在CodeReview site

上提交

答案 1 :(得分:0)

您在FileInputStream fs = new FileInputStream(fileName);方法中getExcelData处有例外情况。

您正在getExcelData课程中调用adminLogin,如下所示:

Object[][] user = adminbase.getExcelData(testData, "adminLogin");

testData未初始化,因此最终您将null传递给FileInputStream

您还在testData课程中声明了adminBase。如果您想使用它,那么更改代码如下:

FileInputStream fs = new FileInputStream(testData);