TestNG异常:无法实例化类

时间:2017-11-21 09:08:29

标签: java selenium exception nullpointerexception testng

我收到了一个java null指针异常。我无法解决它。我在我的testbase类中初始化了驱动程序,并希望在我的Testing_TVO类中使用相同的驱动程序

这是我的测试基类

public class testbase {

public static WebDriver driver;
String baseURL = "https://mpower.tvo.org/educators/#/login/";
String browser  = "firefox";


public void init()
{
    selectBrowser(browser);
    getUrl(baseURL);
}

public void selectBrowser(String browser)
{
    if(browser.equalsIgnoreCase("firefox"))
    {
        System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe ");
        driver = new FirefoxDriver();
    }
    if (browser.equalsIgnoreCase("chrome"))
    {
        System.setProperty("webdriver.chrome.driver", "H:\\geckodriver-v0.17.0-win64\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    if(browser.equalsIgnoreCase("edge"))
    {
         System.setProperty("webdriver.edge.driver", "H:\\MicrosoftWebDriver.exe");
         driver = new EdgeDriver();
    }

}

public void getUrl(String url)
{
    driver.get(url);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

这是我的Test类,我想初始化web元素并编写脚本

public class Testing_TVO_Website extends testbase {


    @BeforeClass
    public void beforeClass() {
        init();

    }

    WebElement register_btn = driver.findElement(By.xpath(".//*[@id='loginPageRegister1']"));
    WebElement firstName_tbx = driver.findElement(By.xpath(".//*[@id='firstName']"));   
    WebElement lastName_tbx = driver.findElement(By.xpath(".//*[@id='lastName']"));

    @FindBy(xpath = ".//*[@id='educatorRoleId']")
    WebElement role;
    Select role_sel = new Select(role);

    @FindBy(xpath = ".//*[@id='boardId']")
    WebElement board;
    Select board_sel = new Select(board);

    @FindBy(xpath = ".//*[@id='schoolId']")
    WebElement school;
    Select school_sel = new Select(school);


    WebElement email_tbx = driver.findElement(By.xpath(".//*[@id='email']"));
    WebElement emailConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userEmailConfirm']"));
    WebElement password_tbx = driver.findElement(By.xpath(".//*[@id='userPassword']"));
    WebElement passwordConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userPasswordConfirm']"));

    @FindBy(xpath = ".//*[@id='source']")
    WebElement source;
    Select source_sel = new Select(source);

    WebElement agreement_chbx = driver.findElement(By.xpath(".//*[@id='agreement']"));
    WebElement registerSubmit_btn = driver.findElement(By.xpath(".//*[@id='registerSubmit']"));



    @Test
    public void TC_001_Verifying_Registeration() {
      register_btn.click(); 
      firstName_tbx.sendKeys("Shivam");
      lastName_tbx.sendKeys("Patel");
      role_sel.selectByIndex(1);
      board_sel.selectByValue("Algoma DSB");
      school_sel.selectByVisibleText("Blind River");

      email_tbx.sendKeys("patelshivam3033@adsb.on.ca");
      emailConfirm_tbx.sendKeys("patelshivam3033@adsb.on.ca");
      password_tbx.sendKeys("shivampatel");
      passwordConfirm_tbx.sendKeys("shivampatel");

      source_sel.selectByIndex(2);
      agreement_chbx.click();
      registerSubmit_btn.click();

      assertEquals(driver.findElement(By.xpath(".//*[@id='app-content']/div[3]/div/div/div[2]/h2")), "Thank you for registering!");




    }

但是我得到了java null指针异常。这是日志

  org.testng.TestNGException: 
Cannot instantiate class com.test.TVO_Assignment.Testing_TVO_Website
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
    at org.testng.TestClass.getInstances(TestClass.java:99)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
    at org.testng.TestClass.init(TestClass.java:77)
    at org.testng.TestClass.<init>(TestClass.java:42)
    at org.testng.TestRunner.initMethods(TestRunner.java:423)
    at org.testng.TestRunner.init(TestRunner.java:250)
    at org.testng.TestRunner.init(TestRunner.java:220)
    at org.testng.TestRunner.<init>(TestRunner.java:169)
    at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
    at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616)
    at org.testng.SuiteRunner.init(SuiteRunner.java:185)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
    ... 25 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
    at com.test.TVO_Assignment.Testing_TVO_Website.<init>(Testing_TVO_Website.java:34)
    ... 30 more

1 个答案:

答案 0 :(得分:1)

根据您的日志,错误不是空指针异常,实际错误是IllegalStateException,因为没有设置gecko驱动程序。这里的问题是,壁虎驱动程序路径末尾有额外的空间。这可能是一个错字。

System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe ");

请删除上一行末尾的空格。它解决了您的问题。

此外,您的代码存在问题,例如,您无法将页面工厂代码和测试代码组合在一个类中,使其分离。