
👉Cucumber Feature File and Step Definition
Cucumber Feature File and Step Definition: In every Cucumber-based automation project, the features directory is the heart of the framework. It contains all feature files, along with two important subdirectories: step_definitions and support.
These two files — Feature File and Step Definition — form the backbone of any Cucumber test.
👉 What is a Feature File in Cucumber?
Cucumber Feature File and Step Definition: A Feature File in Cucumber contains a high-level description of the application behavior written in Gherkin, a domain-specific language that uses simple, readable English syntax.
The purpose of the Feature File is to define user behavior and system outcomes in plain text, making it easy for non-technical stakeholders to understand and validate.
👉Key Components of a Cucumber Feature File
Keyword | Description |
Feature: | Describes the feature or functionality being tested. |
Scenario: | A concrete example that describes one particular test case. |
Scenario Outline: | Used to run the same scenario with multiple sets of test data. |
Given: | Sets the initial context or precondition. |
When: | Describes the action/event under test. |
Then: | Specifies the expected result of the action. |
👉 Sample Cucumber Feature File Example
gherkin
Feature: User Login
Scenario: Successful login with valid credentials
Given User is on the login page
When User enters valid username and password
Then User should be redirected to the dashboard
You can save this file with a .feature extension in the features/ directory, for example:
bash
features/login.feature
👉What is a Step Definition in Cucumber?
Cucumber Feature File and Step Definition: A Step Definition is the actual implementation code that executes the steps mentioned in the feature file. It maps the plain-text Gherkin steps (Given, When, Then) to code written in a programming language like Ruby, Java, or JavaScript.
In Ruby-based Cucumber projects, step definitions are stored in the following path:
bash
features/step_definitions/*.rb
Each method in the step definition file should match a step from the feature file using regular expressions.
👉 Example of Step Definition File (Ruby)
ruby
Given(“User is on the login page”) do
visit ‘http://example.com/login’
end
When(“User enters valid username and password”) do
fill_in ‘username’, with: ‘test_user’
fill_in ‘password’, with: ‘secure_pass’
click_button ‘Login’
end
Then(“User should be redirected to the dashboard”) do
expect(page).to have_content(‘Welcome, test_user’)
end
Save this file as login_steps.rb inside:
bash
features/step_definitions/
👉Summary: Key Takeaways
Feature File | Step Definition |
Written in Gherkin (plain English) | Written in a programming language (e.g., Ruby) |
Describes what should happen | Contains code that makes it happen |
Includes Feature, Scenario, Given, When, Then | Matches steps using Regex or strings |
Helps non-programmers understand scenarios | Executes actions on the system under test |
👉 Why Use Feature Files and Step Definitions?
- Cucumber Feature File and Step Definition: Encourages collaboration between developers, testers, and business analysts
- Keeps test logic separated from test descriptions
- Makes test cases more maintainable and readable
- Supports BDD (Behavior Driven Development) approach