Cucumber-JVM未定义步骤问题求助:Selenium Maven项目执行报错
Hey there, sorry to hear you're stuck with this frustrating undefined steps issue—let's break down what might be going wrong and how to fix it.
First, that error message tells us Cucumber can't map your feature file steps to their corresponding definition methods, even though you've set up the standard directory structure correctly. Here are the most common fixes to check:
1. Fix Annotation Case Sensitivity
Looking at the step snippets you shared, you're using lowercase annotations like @given, @when, @then—but Cucumber requires uppercase annotations (@Given, @When, @Then) from the io.cucumber.java.en package. Lowercase annotations won't be recognized by the framework, so this is likely a key issue.
Update your step definitions to use uppercase annotations, and confirm you're importing the right classes:
import io.cucumber.java.en.Given; import io.cucumber.java.en.When; import io.cucumber.java.en.Then; import io.cucumber.java.PendingException; @Given("^User is already on login page$") public void user_is_already_on_login_page() throws Throwable { // Add your login page navigation code here throw new PendingException(); } @When("^title of login page is Free CRM$") public void title_of_login_page_is_Free_CRM() throws Throwable { // Add title validation code here throw new PendingException(); } @Then("^user enters username and password$") public void user_enters_username_and_password() throws Throwable { // Add credential input code here throw new PendingException(); }
2. Verify Glue Path in Your Test Runner
Double-check your Cucumber Test Runner's @CucumberOptions to ensure the glue property points to the package where your step definitions are stored (not just a single class). For example:
import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources", // Path to your feature files glue = "com.CRM.cucumber.stepdefinitions", // Replace with your actual step def package monochrome = true, plugin = {"pretty"} ) public class TestRunner { }
If the glue path is incorrect or missing, Cucumber won't scan for your step definitions at all.
3. Check Exact Step Matching (Regex & Feature Text)
Ensure the regex in your annotations perfectly matches the text in your feature file steps. Even tiny differences—like extra spaces, capitalization, or punctuation—will break the mapping. For example, if your feature file has:
Given user is already on login page
But your annotation uses^User is already on login page$(capital "U"), the match will fail. Adjust either the feature file or the regex to align exactly.
4. Refresh & Rebuild Your Maven Project
Stale compiled classes can sometimes cause this issue. Run these commands in your project root to clean and rebuild everything:
mvn clean mvn test
This ensures all your latest code changes are compiled and picked up by Cucumber.
5. Confirm Dependency Version Compatibility
Make sure your pom.xml uses compatible versions of all Cucumber-related dependencies (like cucumber-java, cucumber-junit, cucumber-core). Mismatched major versions can lead to unexpected framework behavior.
Give these fixes a try—most likely the annotation case or glue path is the culprit here.
内容的提问来源于stack exchange,提问作者Thalesh

