How to set up Cucumber with Cypress?
What is Cucumber?
Cucumber is a tool that supports behavior-driven development (BDD). It runs automated acceptance tests written in BDD format. Cucumber provides a way to write tests that anybody can understand, regardless of their technical knowledge. It explains test steps and application behavior in simple English using the Gherkin language.
Why use Cucumber for testing?
Cucumber is an important testing tool for the following reasons:
- It's free to use as it's open-source.
- It can be integrated with many Automation tools such as Cypress, Selenium, Ruby on Rails, and other web-based testing tools.
How to configure Cucumber with Cypress?
Step 1: Install Cypress
Run the following command to install Cypress locally:
Step 2: Install Cucumber for Cypress
Run the following command to install the Cucumber for Cypress package:
Step 3: Add the configuration Cypress environment files
plugins/Index.js file add the following:
package.json file, add the following configuration:
In the spec files extension parameter in the
cypress.json file, make sure to point to the feature files:
Run Tests with Cypress and Cucumber
Let’s first write this test by just using Cypress:
This test navigates to
/login (using the
baseUrl specified in
cypress.json), enters the username and the password and clicks the “Log in” button.
In Cypress, users can group multiple commands into a single custom command by creating a file called
cypress/support/commands.js and add:
And then open
cypress/support/index.js and add:
Now, use the custom command in the tests:
The above code is written using Cypress. Now, on using Gherkin for Cucumber, the code goes as follows:
The test comprises 3 defined steps:
login/login.js) and write:
Once this code runs, Cucumber will convert it into Cypress commands. And, that’s how it works.