Welcome to BhauAutomation!
What is Cucumber with JavaScript?
Cucumber is a testing tool used for Behaviour-Driven Development (BDD). It helps testers write test scenarios in simple English and connect them with JavaScript code.
It uses a simple format called Gherkin with three main keywords: Given, When, and Then.
Why Use Cucumber?
- Write test cases in simple English.
- Connect requirements with automation code.
- Make test scenarios easy to understand.
- Reuse step definitions.
- Improve communication between testers and developers.
Simple Example
Imagine we want to test a login feature.
Step 1: Create a Feature File
File name: login.feature
Feature: Login
Scenario: Successful Login
Given the user is on the login page
When the user enters valid username and password
Then the user should see the welcome message
Step 2: Create Step Definitions
File name: login.js
const { Given, When, Then } = require('@cucumber/cucumber');
Given('the user is on the login page', function () {
console.log('Login page opened');
});
When('the user enters valid username and password', function () {
console.log('Login details entered');
});
Then('the user should see the welcome message', function () {
console.log('Welcome message displayed');
});
Step 3: Run the Test
npx cucumber-js
Expected Output (simplified):
Login page opened
Login details entered
Welcome message displayed
1 scenario passed
3 steps passed
Simple Explanation
- Given: Describes the starting condition.
- When: Describes the action performed.
- Then: Checks the expected result.
- Step Definitions: Connect English steps with JavaScript code.
Conclusion
Cucumber with JavaScript makes automation testing easier by allowing testers to write scenarios in simple English and connect them with code.
Keep Learning with BhauAutomation!