Cypress & BDD - biswajitsundara/Cypress GitHub Wiki

Cypress can be integrated with BDD (cucumber)

1. Install Cypress Cucumber

Use the command to install the cypress cucumber : npm install --save-dev cypress-cucumber-preprocessor

2. Configure Cypress Plugin

Add cucumber to the plugins, go to cypress/plugins/index.js and add the below code

const cucumber = require('cypress-cucumber-preprocessor').default

module.exports = (on, config) => {
  on('file:preprocessor', cucumber())
}

3. Update Cypress.json

Cucumber uses feature files and to make cypress recognize the files, add the below code in cypress.json
"testFiles": "**/*.feature"

4. Update Package.json

Add the below code in package.json

"cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true
  }

5. Create feature file

The feature file needs to be under integration. cypress\integration\Login.feature

@smoke
Feature: Login to website

Scenario:
Given I am at the login page
When I enter credentials
Then I landed on home page

6. Create step definition file

The step definition file needs to be under integration and the folder name should be same as the feature file name.
The feature file name is Login.feature so step definition should be in cypress\integration\Login\login.js

Given ('I am at the login page', () => {
    cy.visit("http://automationpractice.com/index.php?controller=authentication&back=my-account");
})

When ('I enter credentials', () => {
    cy.get('#email').type('[email protected]');
    cy.get('#passwd').type('automation');
    cy.get('#SubmitLogin').click();
})

Then ('I landed on home page', () => {
    cy.get('.account').invoke('text').then((text => {
        expect(text.trim()).to.eq('myname');
            }));
})

7. Execute the Test

  • Open Test Runner & execute the feature file Login.feature
  • In terminal enter the command node_modules\.bin\cypress run --browser chrome Login.feature
  • We can use tags to run specific scenario(s) this way. node_modules/.bin/cypress run --browser chrome TAGS='smoke'

Reference

https://github.com/TheBrainFamily/cypress-cucumber-preprocessor

⚠️ **GitHub.com Fallback** ⚠️