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

Cucumber框架Steps_FirstPage类实例化失败问题求助

Cucumber类实例化失败问题排查与解决

遇到错误:io.cucumber.core.exception.CucumberException: Failed to instantiate class steps_definations.Steps_FirstPage,已排查相关问题未解决,现提供项目代码请求协助。


问题根源分析与修复步骤

1. 修复Maven依赖冲突与Java版本

POM.xml中存在两个版本的cucumber-junit依赖,且Java编译版本过低,这是导致类实例化失败的核心原因:

  • 移除重复的cucumber-junit依赖,统一使用7.14.0版本
  • 将Java编译版本升级到1.8及以上(Selenium 4.x和Cucumber 7.x要求至少Java 8)

修改后的POM.xml关键部分:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <!-- junit依赖 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

    <!-- selenium-java依赖 -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.12.1</version>
    </dependency>

    <!-- cucumber-java依赖 -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.14.0</version>
    </dependency>

    <!-- cucumber-junit依赖(仅保留一个) -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. 修复元素定位错误

FirstPage类中signinBtn的定位方式错误,By.className()不能接收多个空格分隔的类名,需改为CSS选择器:

// 原错误代码
private By signinBtn= By.className("btn btn-primary sign-in-btn");
// 修改为
private By signinBtn= By.cssSelector(".btn.btn-primary.sign-in-btn");

3. 优化步骤类实例化逻辑(可选)

虽然不是实例化失败的直接原因,但建议使用依赖注入方式管理WebBase和FirstPage,避免静态driver带来的并发问题,同时符合Cucumber的最佳实践:

package steps_definations;

import com.framework.WebBase;
import com.webpages.FirstPage;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class Steps_FirstPage {

    private final WebBase webBase;
    private final FirstPage firstPage;

    // 构造函数注入
    public Steps_FirstPage(WebBase webBase, FirstPage firstPage) {
        this.webBase = webBase;
        this.firstPage = firstPage;
    }

    @Given("I opened {string} Browser and enter {string} URL")
    public void i_opened_browser_and_enter_url(String browser, String url) {
        webBase.openBrowser(browser);
        webBase.OpenWebsite(url);
    }

    @Then("verified that the open site")
    public void verified_that_the_open_site() {
        firstPage.verifiedPageTitle();
    }
    
    @When("I click Login button")
    public void i_click_login_button() {
        firstPage.clickOnLoginBtn();
    }

    @Then("verified Login page is open")
    public void verified_login_page_is_open() {
        firstPage.verifiedSignInPage();
    }

    @Then("closed browser")
    public void closed_browser() {
        webBase.closeBrowser();
    }
}

4. 执行Maven命令清理项目

修复后执行以下命令确保依赖更新和项目编译:

mvn clean install

项目代码原文

POM.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>a</groupId>
    <artifactId>Social</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>Social</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>


        <!--
        https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.12.1</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>7.14.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>7.12.1</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>7.14.0</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven
            defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see
                https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see
                https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see
                https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

WebBase类

package com.framework;

import java.time.Duration;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebBase {

    public static WebDriver driver;

    public void openBrowser(String browsername) {

        if (browsername.toLowerCase().contains("chrome")) {

            driver = new ChromeDriver();

        } else if

        (browsername.toLowerCase().contains("firefox")) {

            driver = new FirefoxDriver();

        } else if (browsername.toLowerCase().contains("edge")) {

            driver = new EdgeDriver();

        }

    }

    public void waitForPageLoader(long time) {

        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(time));

    }

    public void OpenWebsite(String url) {

        driver.manage().window().maximize();
        driver.get(url);

    }

    public void closeBrowser() {

        driver.quit();

    }

}

FirstPage类

package com.webpages;

import org.junit.Assert;
import org.openqa.selenium.By;

import com.framework.WebBase;

public class FirstPage extends WebBase {
    

    private By pageTitle = By.tagName("title");
    private By loginBtn= By.className("login-btn");
    private By signinBtn= By.className("btn btn-primary sign-in-btn");

    public void verifiedPageTitle() {
        
        try {
            String title = driver.getTitle();
            System.out.println(title);
            
        } catch (Exception e) {
            
            Assert.assertEquals(driver.getTitle(), pageTitle);
        }

        
    }
    
    public void clickOnLoginBtn() {
                
        try {
            
            driver.findElement(loginBtn).click();   
            
        } catch (Exception e) {
            
            Assert.fail("not able to find login button ");
        }
        
    }
    
    public void verifiedSignInPage () {
        
        try {
            
            driver.findElement(signinBtn);
            System.out.println("we are on page ");
            driver.getTitle();
            
        } catch (Exception e) {
        
            Assert.fail("login page  ");

        }
        
        
    }
    
    
}

Steps_FirstPage类

package steps_definations;

import com.framework.WebBase;
import com.webpages.*;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class Steps_FirstPage {

    WebBase webbase = new WebBase();
    FirstPage fP =new FirstPage();
    @Given("I opened {string} Browser and enter {string} URL")
    public void i_opened_browser_and_enter_url(String browser, String url) {

        webbase.openBrowser(browser);
        webbase.OpenWebsite(url);
    }

    @Then("verified that the open site")
    public void verified_that_the_open_site() {
        fP.verifiedPageTitle();
    }
    
    @When("I click Login button")
    public void i_click_login_button() {
        fP.clickOnLoginBtn();
    }

    @Then("verified Login page is open")
    public void verified_login_page_is_open() {
        fP.verifiedSignInPage();
        
    }

    @Then("closed browser")
    public void closed_browser() {
        webbase.closeBrowser();

    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:33:10