无法实例化类异常TestNG

时间:2016-10-03 16:10:05

标签: testng

我在使用带有TestNG的java在selenium webdriver中运行我的一个测试用例时遇到了“无法实例化类”错误。我正在尝试使用excel在selenium webdriver中使用参数化。

以下是测试功能的类

package parameterizationExcel;

import org.testng.annotations.Test;

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

import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;

public class ReadExcelDataProvider {

public WebDriver driver;
public WebDriverWait myWaitVar = new WebDriverWait(driver, 30);

//Locators
private By byPickupLoc = By.id("pickupLoc");
//private By byPassword = By.id("session_password-login");
private By bySearch = By.cssSelector("div.ui.blue.large.submit.button.ng-isolate-scope");
private By byError = By.cssSelector("div.ui.error.message");

  @BeforeTest
  public void beforeTest() {

      System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
  } 

  @Test(dataProvider="searchLocation")
  public void VerifyValidLocations(String topLocation)throws     InterruptedException {

  driver.get("https://carhire.vroomtest.com/");
      myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(byPickupLoc));
    driver.findElement(byPickupLoc).sendKeys(topLocation);
      myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("strong.ng-binding")));
    driver.findElement(By.cssSelector("strong.ng-binding")).click();

    //wait for element to be visible and perform click
    myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(bySearch));
    driver.findElement(bySearch).click();

    //Check for error message
    myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(byError));
    String actualErrorDisplayed = driver.findElement(byError).getText();
    String requiredErrorMessage = "Please correct the marked field(s) below.";
    Assert.assertEquals(requiredErrorMessage, actualErrorDisplayed);

    //Results Page
    myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".resultsCount > strong")));
  }


  @AfterTest
  public void afterTest() {

  driver.quit();
  }

   @DataProvider(name="searchLocation")
public Object[][] searchData() {
    Object[][] arrayObject = getExcelData("E:/ODESK/VroomVroomVroom/sampledoc.xlsx","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;
}

}

这是我得到的例外情况。

org.testng.TestNGException: 
Cannot instantiate class parameterizationExcel.ReadExcelDataProvider
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:382)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:295)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:117)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:183)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:128)
at org.testng.TestRunner.initMethods(TestRunner.java:415)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.init(TestRunner.java:211)
at org.testng.TestRunner.<init>(TestRunner.java:165)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:142)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:272)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:587)
at org.testng.SuiteRunner.init(SuiteRunner.java:161)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:114)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1260)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1247)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1101)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more

Caused by: java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:102)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at parameterizationExcel.ReadExcelDataProvider.<init>(ReadExcelDataProvider.java:27)
... 26 more

1 个答案:

答案 0 :(得分:3)

所以... 你正在获得NPE

public WebDriver driver;
public WebDriverWait myWaitVar = new WebDriverWait(driver, 30);

myWaitVar初始化移至@BeforeTest

@BeforeTest
public void beforeTest() {

  System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
    driver=new ChromeDriver();
    driver.manage().window().maximize();
    myWaitVar = new WebDriverWait(driver, 30);
}