Writing Tests
BDD with Cucumber
Tests are defined as Gherkin scenarios in .feature files under cypress/e2e/features/. Step definitions connect Gherkin steps to Cypress commands.
Example Feature File
gherkin
Feature: Employee login
Scenario: Successful login with valid credentials
Given I am on the login page
When I enter valid employee credentials
And I click the login button
Then I should be redirected to the employee dashboardStep Definitions
Step definitions live alongside the feature files or in cypress/support/step_definitions/. Each step maps to a function:
js
import { Given, When, Then } from '@badeball/cypress-cucumber-preprocessor'
import LoginPage from '../../pages/Login/login'
const login = new LoginPage()
Given('I am on the login page', () => {
login.visit()
})
When('I enter valid employee credentials', () => {
login.enterCredentials(
Cypress.env('EMPLOYEE_EMAIL'),
Cypress.env('EMPLOYEE_PASSWORD')
)
})Fixtures
Test data lives in cypress/fixtures/. Use cy.fixture('filename') to load it.
js
cy.fixture('application_form').then((data) => {
// use data in test
})File Uploads
Use cypress-file-upload:
js
cy.get('[data-cy=upload-input]').attachFile('test-document.pdf')Best Practices
- Use
data-cyattributes as selectors — never class names or text (they change) - One scenario = one user flow, not one assertion
- Keep step definitions thin — delegate to page objects
- Use
cy.intercept()to assert API calls were made with correct payloads