【问题标题】:NullPointerException on running selenium script with JUnit使用 JUnit 运行 selenium 脚本时出现 NullPointerException
【发布时间】:2023-04-03 17:03:01
【问题描述】:

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

考虑以下浏览器配置类。

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 测试运行时,它会给出 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);

了解我为什么会遇到此异常吗?

【问题讨论】:

  • 您解决了这个问题吗?我有同样的例外

标签: java selenium selenium-webdriver junit


【解决方案1】:

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();
  }
}

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2018-01-30
    • 2019-05-12
    • 1970-01-01
    相关资源
    最近更新 更多