Cypress & BDD - biswajitsundara/Cypress GitHub Wiki
Cypress can be integrated with BDD (cucumber)
Use the command to install the cypress cucumber : npm install --save-dev cypress-cucumber-preprocessor
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()) }
Cucumber uses feature files and to make cypress recognize the files, add the below code in cypress.json
"testFiles": "**/*.feature"
Add the below code in package.json
"cypress-cucumber-preprocessor": { "nonGlobalStepDefinitions": true }
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
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'); })); })
- 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'
https://github.com/TheBrainFamily/cypress-cucumber-preprocessor