【问题标题】:Why does the NullPointerException occur on the Selenium driver?为什么 Selenium 驱动程序会出现 NullPointerException?
【发布时间】:2017-10-02 05:40:23
【问题描述】:

我有两个单独的 Selenium Webdriver 类。我第一次运行的类没有任何错误,但第二个类抛出 NullPointerException。
错误指出下面的行WebDriverWait wait = new WebDriverWait(driver,40);。不仅对于WebDriverWait,如果在一行代码中实例化driver,则会引发NullPointer 异常。例如Actions action = new Actions(driver);
上述代码也会引发 NullPointerException。但是头等舱没有问题。

我第一次上的课

package Initiate;

import dataProvider.CommonClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class NewBOM {

    public WebDriver driver;
    By newBomButton = By.id("btnUpdate");

    public NewBOM(WebDriver driver){
    this.driver=driver;
        this.CreateNewBOM();

    }

    public void CreateNewBOM(){
        Actions action = new Actions(driver);
        WebDriverWait wait = new WebDriverWait(driver, 40);
        wait.pollingEvery(2, TimeUnit.SECONDS);

        WebElement newBomBtn = driver.findElement(newBomButton);
        wait.until(ExpectedConditions.elementToBeClickable(newBomBtn));
        action.moveToElement(newBomBtn).click().build().perform();
    }
}

引发空指针异常的第二个类

package Initiate;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;


public class NewBOO {
    public WebDriver driver;

    public NewBOO(WebDriver driver){
        this.driver=driver;
    }

    public void test() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver,40);
        System.out.println("Test print");
    }
}

我第一堂课的TestNg课

import Initiate.NewBOM;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class CreateBOM {
    public WebDriver driver;

    private NewBOM objNewBOM;

    @Test(priority = 1)
    public void clickOnNewBOMButton(){
        objNewBOM = new NewBOM(driver);
        objNewBOM.CreateNewBOM();
    }

}

我第 2 课的 TestNg 课

import Initiate.NewBOO;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class CreateBOO {
    public WebDriver driver;
    NewBOO objNewBOO;
    @Test(priority = 1)
    public void temporyTest() throws InterruptedException {
        objNewBOO = new NewBOO(driver);
        objNewBOO.test();
    }
}

完整的错误信息

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at Initiate.NewBOO.test(NewBOO.java:17)
    at CreateBOO.temporyTest(CreateBOO.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)


===============================================
Default Suite`enter code here`
Total tests run: 1, Failures: 1, Skips: 0
===============================================


Process finished with exit code 0

我分别测试了两个 testNg 类,并且两个类都是独立的。但是第一类运行没有任何空指针异常,第二类抛出空指针异常。

【问题讨论】:

  • 你在哪里初始化driver
  • 哪个驱动程序?我在两个类上都初始化了它
  • 我在你提供的代码中没有看到初始化。
  • 初始化必须类似于 public WebDriver driver = new WebDriver(),可能还有一些附加选项。第一个类将运行,因为它不使用驱动程序。如果你这样做,它可能会同样失败。

标签: java selenium selenium-webdriver nullpointerexception testng


【解决方案1】:

看起来您的代码没有初始化 WebDriver 并尝试使用它,所以它抛出了 NullPointerException。 在 TestNG 中,你可以使用@BeforeSuite、@BeforeTest、@BeforeClass 来初始化你的依赖。下面的例子展示了如何在'BeforeClass'中初始化webdriver。

import Initiate.NewBOM;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class CreateBOM {
    public WebDriver driver;

    private NewBOM objNewBOM;

    @BeforeClass
    public void setupClass(){
        driver = new FirefoxDriver();
    }

    @Test(priority = 1)
    public void clickOnNewBOMButton(){
        objNewBOM = new NewBOM(driver);
        objNewBOM.CreateNewBOM();
    }

}

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 2012-10-06
    • 2012-03-26
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    相关资源
    最近更新 更多