Last Updated: November 21, 2025
Cucumber
BDD testing framework
Core Concepts
| Item | Description |
|---|---|
Gherkin
|
Human-readable test scenarios |
Feature
|
Testable functionality |
Scenario
|
Specific test case |
Given/When/Then
|
Test steps structure |
Step Definitions
|
Code implementing steps |
Hooks
|
Before/After test actions |
Gherkin Example
Feature: User Login
As a user
I want to log in to the system
So that I can access my account
Scenario: Successful login
Given I am on the login page
When I enter valid credentials
And I click the login button
Then I should see the dashboard
And I should see a welcome message
Scenario: Failed login
Given I am on the login page
When I enter invalid credentials
And I click the login button
Then I should see an error message
And I should remain on the login page
Step Definitions (JavaScript)
const { Given, When, Then } = require('@cucumber/cucumber');
Given('I am on the login page', async function () {
await this.page.goto('http://example.com/login');
});
When('I enter valid credentials', async function () {
await this.page.fill('#username', 'testuser');
await this.page.fill('#password', 'password123');
});
When('I click the login button', async function () {
await this.page.click('button[type="submit"]');
});
Then('I should see the dashboard', async function () {
await expect(this.page).toHaveURL(/.*dashboard/);
});
Best Practices
- Write scenarios from user perspective
- Keep steps reusable and atomic
- Use Background for common setup
- Avoid implementation details in Gherkin
💡 Pro Tips
Quick Reference
Cucumber bridges communication between technical and non-technical team members