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

Cucumber JUnit下WebDriver空指针及Hooks未触发问题排查

问题分析与解决方案

一、WebDriver空指针问题(Cannot invoke "org.openqa.selenium.WebDriver.navigate()" ... null)

1. 检查PageContext的DI配置

  • 确保PageContext类被标记为Cucumber可识别的注入Bean:在类上添加@Component,并且类所在包要包含在TestRunner的glue路径中。
  • 确认WebDriver的注入逻辑:PageContext的driver字段必须通过构造方法或@Autowired正确注入,且getDriver()方法直接返回该字段,不能有未初始化的情况。示例:
    import org.openqa.selenium.WebDriver;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class PageContext {
        private final WebDriver driver;
    
        @Autowired
        public PageContext(WebDriver driver) {
            this.driver = driver;
        }
    
        public WebDriver getDriver() {
            return driver;
        }
    }
    
  • 配置WebDriver的Bean作用域:在WebDriver配置类中,给@Bean添加@Scope("cucumber-glue")或@Scope("prototype"),确保每个测试场景都能获取独立的WebDriver实例,避免共享实例导致的空指针。示例:
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Scope;
    
    @Configuration
    public class WebDriverConfig {
        @Bean
        @Scope("cucumber-glue")
        public WebDriver getChromeDriver() {
            System.setProperty("webdriver.chrome.driver", "path/to/your/chromedriver");
            return new ChromeDriver();
        }
    }
    

2. 检查MyHooks的构造注入

  • 确保MyHooks的构造方法正确接收PageContext实例,且没有手动初始化PageContext的操作(比如new PageContext()),必须依赖DI注入。示例:
    public class MyHooks {
        private final PageContext pageContext;
    
        // 构造方法注入,无需额外注解(Cucumber会自动识别)
        public MyHooks(PageContext pageContext) {
            this.pageContext = pageContext;
        }
    }
    

二、Hooks未触发问题

1. 核对TestRunner的配置

  • 确保@CucumberOptions的glue属性包含MyHooks所在的包,同时要覆盖步骤定义类、配置类的包路径。示例:
    import io.cucumber.junit.Cucumber;
    import io.cucumber.junit.CucumberOptions;
    import org.junit.runner.RunWith;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
            features = "src/test/resources/features",
            glue = {"com.yourpackage.hooks", "com.yourpackage.stepdefs", "com.yourpackage.config"},
            plugin = {"pretty", "html:target/cucumber-reports.html"}
    )
    public class TestRunnerTS01 {
    }
    
  • 确认TestRunner类正确继承Cucumber运行器:类上必须添加@RunWith(Cucumber.class)注解,不能用JUnit原生的运行器。

2. 检查Hooks注解的正确性

  • 必须使用Cucumber专属的Hooks注解:io.cucumber.java.Before和io.cucumber.java.After,绝对不能用JUnit的org.junit.Before/org.junit.After,这是最常见的触发失败原因。示例:
    import io.cucumber.java.After;
    import io.cucumber.java.Before;
    
    public class MyHooks {
        private final PageContext pageContext;
    
        public MyHooks(PageContext pageContext) {
            this.pageContext = pageContext;
        }
    
        @Before
        public void setUp() {
            pageContext.getDriver().navigate().to("https://your-test-url.com");
        }
    
        @After
        public void tearDown() {
            pageContext.getDriver().quit();
        }
    }
    

3. 验证包结构与类路径

  • MyHooks必须放在src/test/java目录下,且所属包要完全匹配TestRunner中glue指定的路径,Cucumber只会扫描glue包下的Hooks和步骤定义类。
  • 确认feature文件路径正确:TestRunner的features属性要指向feature文件所在的资源路径,比如src/test/resources/features。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 22:55:09