使用JUnit运行selenium脚本时出现NullPointerException

时间:2016-05-29 14:36:37

标签: java selenium selenium-webdriver junit

我正在使用pageObject Model进行硒自动化。

请考虑以下浏览器配置类。

package BrowserConfig;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class crossBrowserConfiguration {
    By logInPanel = By.xpath("//div[@id='logInPanelHeading']");

    public static WebDriver driver = null;
    WebDriverWait wait = new WebDriverWait(driver,30);

    @Before
    public void initBrowser(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("Website that contains Login Page");
        wait.until(ExpectedConditions.visibilityOfElementLocated(logInPanel));
        }   

    @After
    public void closeBrowser(){
        driver.quit();
    }
}

我有一个登录屏幕的页面对象。

package PageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import BrowserConfig.crossBrowserConfiguration;

public class LoginScreen extends crossBrowserConfiguration {

    WebDriverWait wait = new WebDriverWait(driver,30);  
    By userName = By.xpath("//input[@id='txtUsername']");
    By password = By.xpath("//input[@id='txtPassword']");
    By loginButton = By.xpath("//input[@id='btnLogin']");
    By welcomeNote = By.xpath("//a[@id='welcome']");
    By empListVerify = By.xpath("//div[@id='employee-information']/a");

    public void logIN(String UserName, String Password){
        driver.findElement(userName).sendKeys(UserName);
        driver.findElement(password).sendKeys(Password);
        driver.findElement(loginButton).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(welcomeNote));

        //Employee List Verification
        String empListBtnText = driver.findElement(empListVerify).getText();
        System.out.println(empListBtnText);
    }

}

最后,我有以下测试用例脚本:

package TestCases;

import org.junit.Test;

import BrowserConfig.crossBrowserConfiguration;
import PageObjects.LoginScreen;

public class initiateBrows extends crossBrowserConfiguration{

    LoginScreen Obj1 = new LoginScreen();

    @Test
    public void runThis() throws Exception{
        Obj1.logIN("admin", "123456");
    }

}

当我以JUnit Test运行我的测试时,它会在不运行的情况下提供nullPointerException。异常的堆栈跟踪是:

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212)
    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 BrowserConfig.crossBrowserConfiguration.<init>(crossBrowserConfiguration.java:15)
    at TestCases.initiateBrows.<init>(initiateBrows.java:8)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

at BrowserConfig.crossBrowserConfiguration.<init>(crossBrowserConfiguration.java:15) 指的是WebDriverWait wait = new WebDriverWait(driver,30);

有关我为何面临此异常的任何见解?

1 个答案:

答案 0 :(得分:0)

创建类的对象时初始化wait字段。这在JUnit调用@Before方法之前发生。因此driver对象为null。有不同的方法来解决问题。一个是使驱动程序对象成为一个简单的字段并立即初始化它。

public class crossBrowserConfiguration {
  By logInPanel = By.xpath("//div[@id='logInPanelHeading']");

  public final WebDriver driver = new FirefoxDriver();
  WebDriverWait wait = new WebDriverWait(driver,30);

  @Before
  public void initBrowser(){
    driver.manage().window().maximize();
    driver.get("Website that contains Login Page");
    wait.until(ExpectedConditions.visibilityOfElementLocated(logInPanel));
  }   

  @After
  public void closeBrowser(){
    driver.quit();
  }
}