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

Selenium Cucumber/BDD框架login.feature配置异常,关键字不被识别

Troubleshooting Unrecognized 'When'/'Then' Keywords in Selenium Cucumber/BDD

First off, let's fix the most obvious issue with your login.feature file—incorrect formatting. Cucumber relies on proper line breaks and indentation to parse keywords like Given, When, Then, and And. Your current content is all run together, which is why Selenium/Cucumber can't pick up the keywords correctly.

Step 1: Fix the Feature File Structure

Here's the properly formatted version of your feature file:

Feature: Application Login
  Scenario: Home page default login
    Given User is on Netbanking landing page
    When User login into application with username and password
    Then Home page is populated
    And Cards are displaying

Key fixes here:

  • Each core keyword (Feature, Scenario, Given, etc.) starts on a new line
  • Scenario steps are indented (2-4 spaces) under the Scenario line to clearly associate them with the test case
  • And is used correctly as a continuation of the previous Then step

Step 2: Ensure Corresponding Step Definitions Exist

Even with a properly formatted feature file, Cucumber will throw errors if it can't find matching step definitions for your When and Then steps. You need to create methods in your test code (Java, Python, etc.) annotated with @When and @Then that map exactly to the text in your feature file.

Example step definitions (Java):

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;

public class LoginSteps {
    @Given("User is on Netbanking landing page")
    public void user_is_on_netbanking_landing_page() {
        // Selenium code to navigate to the netbanking landing page
    }

    @When("User login into application with username and password")
    public void user_login_into_application_with_username_and_password() {
        // Selenium code to enter credentials and submit the login form
    }

    @Then("Home page is populated")
    public void home_page_is_populated() {
        // Selenium code to verify the home page loads successfully
    }

    @Then("Cards are displaying")
    public void cards_are_displaying() {
        // Selenium code to verify the cards section is visible
    }
}

Step 3: Verify Cucumber Test Runner Configuration

Make sure your test runner class has the correct glue path pointing to the package where your step definitions are stored. This tells Cucumber exactly where to look for the code that maps to your feature file steps.

Example test runner (Java):

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features/login.feature", // Path to your feature file
    glue = "com.yourproject.stepdefinitions", // Path to your step definitions package
    plugin = {"pretty", "html:target/cucumber-reports"}
)
public class TestRunner {
}

Step 4: Check Dependency Versions

Ensure you're using compatible versions of Cucumber and Selenium in your build file (pom.xml for Maven, build.gradle for Gradle). Mismatched versions can cause unexpected parsing issues—for example, using Cucumber 7.x with outdated Selenium libraries might break keyword recognition.

If you’ve gone through these steps and still have issues, double-check for typos in your feature file steps or step definition annotations. Even a small mismatch (like "login" vs "log in") will prevent Cucumber from matching the steps correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 18:17:31