You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium PageObject类调用user()方法报NullPointerException(NPE)原因排查

Selenium调用页面对象空指针异常问题分析与解决

异常产生根因

  • 页面对象实例化时机错误:测试类homePage在成员变量位置直接初始化LoginPage lp = new LoginPage(driver),此时还没有调用initializeDriver()方法给driver赋值,传入LoginPage的driver本身是null,后续哪怕给测试类的driver赋值,LoginPage内部保存的driver还是初始的null值。
  • 变量重复声明冲突:测试类homePage重复定义了public WebDriver driver;,覆盖了父类base中声明的driver,父类initializeDriver、verifyPage方法操作的是父类的driver变量,测试类自己声明的driver始终为空,进一步加剧空指针问题。
  • 测试方法缺少前置逻辑:第二个测试方法testCase没有执行driver初始化流程,直接调用lp.user(),必然触发空指针异常。

解决方案

  1. 删除测试类homePage中重复声明的public WebDriver driver;,直接复用父类base中定义的driver变量。
  2. 调整页面对象初始化时机,不要在成员变量位置实例化LoginPage,改为driver初始化完成后再创建页面对象,推荐用TestNG的@BeforeMethod注解统一做前置初始化,保证每个测试用例执行前环境准备就绪。
  3. 修正不合理的等待条件:原testCase方法中对密码输入框使用elementToBeSelected等待条件错误,该条件仅适用于下拉选项、复选框、单选框类可选中元素,输入框应替换为elementToBeClickable。

修改后的测试类代码示例:

package Admission;

import java.io.IOException;
import java.time.Duration;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import pageObjects.LoginPage;
import resources.base;

public class homePage extends base{
    LoginPage lp;

    // 每个测试方法执行前先初始化driver和页面对象
    @BeforeMethod
    public void setup() throws IOException {
        driver = initializeDriver();
        verifyPage();
        lp = new LoginPage(driver);
    }

    @Test
    public void loginDetails() throws InterruptedException {
        WebElement aadharNo = lp.user();
        aadharNo.sendKeys("111111111111");
        
        WebElement password = lp.pass();
        password.sendKeys("21102021");
        
        WebElement submit = lp.signIn();
        submit.click();
        
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
        wait.until(ExpectedConditions.visibilityOf(lp.pro())).click();
        
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10)); 
        wait1.until(ExpectedConditions.visibilityOf(lp.signOut())).click();
    }

    @Test
    public void testCase() {
        WebElement aadharNo = lp.user();
        WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(30)); 
        wait2.until(ExpectedConditions.visibilityOf(aadharNo));
        aadharNo.sendKeys("111111111111");
        
        WebElement password = lp.pass();
        WebDriverWait wait3 = new WebDriverWait(driver, Duration.ofSeconds(20)); 
        // 修正等待条件为元素可点击
        wait3.until(ExpectedConditions.elementToBeClickable(password));
        password.sendKeys("hgfhg");
        lp.signIn().click();
    }

    // 每个测试方法执行完关闭浏览器,释放资源
    @AfterMethod
    public void tearDown() {
        if(driver != null) {
            driver.quit();
        }
    }
}

内容的提问来源于stack exchange,提问作者Varun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 00:15:01