Convert Figma logo to code with AI

badeball logocypress-cucumber-preprocessor

Run cucumber/gherkin-syntaxed specs with Cypress

1,315
147
1,315
11

Top Related Projects

Cucumber for JavaScript

Run cucumber/gherkin-syntaxed specs with Cypress

46,661

Fast, easy and reliable testing for anything that runs in a browser.

2,279

Cucumber for golang

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration

Quick Overview

The cypress-cucumber-preprocessor is a plugin for Cypress that allows developers to write and execute Cucumber-style BDD (Behavior-Driven Development) tests within the Cypress testing framework. It enables the use of Gherkin syntax for writing test scenarios, making it easier for non-technical stakeholders to understand and contribute to the testing process.

Pros

  • Integrates Cucumber's BDD approach seamlessly with Cypress's powerful testing capabilities
  • Improves collaboration between developers, QA, and business stakeholders through readable Gherkin syntax
  • Supports reusable step definitions, reducing code duplication
  • Provides built-in support for parameterization and data tables in scenarios

Cons

  • May introduce a learning curve for teams not familiar with Cucumber or Gherkin syntax
  • Can potentially slow down test execution compared to native Cypress tests
  • Limited IDE support for Gherkin syntax within Cypress files
  • Requires additional configuration and setup compared to standard Cypress usage

Code Examples

  1. Writing a feature file:
Feature: Login Functionality

Scenario: Successful login
  Given I am on the login page
  When I enter valid credentials
  And I click the login button
  Then I should be redirected to the dashboard
  1. Implementing step definitions:
import { Given, When, Then } from "@badeball/cypress-cucumber-preprocessor";

Given("I am on the login page", () => {
  cy.visit("/login");
});

When("I enter valid credentials", () => {
  cy.get("#username").type("testuser");
  cy.get("#password").type("password123");
});

When("I click the login button", () => {
  cy.get("#login-button").click();
});

Then("I should be redirected to the dashboard", () => {
  cy.url().should("include", "/dashboard");
});
  1. Using data tables in scenarios:
Scenario: Add multiple items to cart
  Given I am on the product page
  When I add the following items to the cart:
    | Product Name | Quantity |
    | T-shirt      | 2        |
    | Jeans        | 1        |
  Then the cart should contain 3 items

Getting Started

  1. Install the preprocessor:

    npm install --save-dev @badeball/cypress-cucumber-preprocessor
    
  2. Configure Cypress (in cypress.config.js):

    const { defineConfig } = require("cypress");
    const createBundler = require("@bahmutov/cypress-esbuild-preprocessor");
    const preprocessor = require("@badeball/cypress-cucumber-preprocessor");
    const createEsbuildPlugin = require("@badeball/cypress-cucumber-preprocessor/esbuild");
    
    module.exports = defineConfig({
      e2e: {
        specPattern: "**/*.feature",
        async setupNodeEvents(on, config) {
          await preprocessor.addCucumberPreprocessorPlugin(on, config);
          on("file:preprocessor", createBundler({
            plugins: [createEsbuildPlugin.default(config)],
          }));
          return config;
        },
      },
    });
    
  3. Create feature files in your Cypress specs directory with a .feature extension and start writing your BDD tests!

Competitor Comparisons

Cucumber for JavaScript

Pros of cucumber-js

  • More versatile, can be used with various testing frameworks beyond Cypress
  • Larger community and ecosystem, with more plugins and extensions available
  • Better suited for non-UI testing scenarios, such as API or unit testing

Cons of cucumber-js

  • Requires more setup and configuration when integrating with Cypress
  • Less seamless integration with Cypress-specific features and commands
  • May have a steeper learning curve for teams already familiar with Cypress

Code Comparison

cypress-cucumber-preprocessor:

import { Given, When, Then } from "cypress-cucumber-preprocessor/steps";

Given("I visit the homepage", () => {
  cy.visit("/");
});

cucumber-js:

const { Given, When, Then } = require("@cucumber/cucumber");

Given("I visit the homepage", function () {
  // Implementation depends on the test runner being used
});

The main difference in the code is the import/require statement and the implementation of the step definition. cypress-cucumber-preprocessor is more tightly integrated with Cypress, allowing direct use of Cypress commands within step definitions. cucumber-js requires additional setup to work with Cypress or other test runners.

Run cucumber/gherkin-syntaxed specs with Cypress

Pros of cypress-cucumber-preprocessor

  • Seamless integration with Cypress for Cucumber-based testing
  • Supports TypeScript out of the box
  • Provides a more structured approach to writing and organizing tests

Cons of cypress-cucumber-preprocessor

  • May have a steeper learning curve for those unfamiliar with Cucumber syntax
  • Potentially slower test execution due to additional preprocessing

Code Comparison

cypress-cucumber-preprocessor:

import { Given, When, Then } from "cypress-cucumber-preprocessor/steps";

Given("I visit the homepage", () => {
  cy.visit("/");
});

When("I click the login button", () => {
  cy.get("#login-button").click();
});

Then("I should see the dashboard", () => {
  cy.url().should("include", "/dashboard");
});

Both repositories are actually the same project, maintained by the same owner (badeball). The repository was likely renamed or moved, resulting in two identical entries. Therefore, there are no significant differences to compare in terms of features, pros, cons, or code examples.

It's worth noting that the cypress-cucumber-preprocessor is a popular tool for integrating Cucumber-style behavior-driven development (BDD) with Cypress testing framework. It allows developers to write tests in a more natural language format, making it easier for non-technical stakeholders to understand and contribute to the testing process.

46,661

Fast, easy and reliable testing for anything that runs in a browser.

Pros of Cypress

  • Native end-to-end testing framework with built-in assertions and commands
  • Extensive documentation and large community support
  • Real-time browser testing with time-travel debugging

Cons of Cypress

  • Limited support for Cucumber-style BDD testing out of the box
  • Steeper learning curve for those familiar with Cucumber syntax
  • Less flexibility in organizing test scenarios and step definitions

Code Comparison

Cypress:

describe('Login', () => {
  it('should log in successfully', () => {
    cy.visit('/login')
    cy.get('#username').type('user@example.com')
    cy.get('#password').type('password123')
    cy.get('button[type="submit"]').click()
    cy.url().should('include', '/dashboard')
  })
})

cypress-cucumber-preprocessor:

Feature: Login

Scenario: Successful login
  Given I am on the login page
  When I enter my username "user@example.com"
  And I enter my password "password123"
  And I click the submit button
  Then I should be redirected to the dashboard

The cypress-cucumber-preprocessor allows for more readable, Gherkin-style syntax, which can be beneficial for non-technical stakeholders and BDD practices. However, Cypress offers a more straightforward approach for those already familiar with JavaScript testing.

2,279

Cucumber for golang

Pros of Godog

  • Written in Go, offering better performance and concurrency support
  • Integrates well with Go's testing framework and ecosystem
  • Supports parallel execution of scenarios out of the box

Cons of Godog

  • Limited to Go projects, unlike Cypress which works with JavaScript/TypeScript
  • Smaller community and ecosystem compared to Cypress-based solutions
  • Less extensive documentation and fewer learning resources available

Code Comparison

Godog:

func FeatureContext(s *godog.Suite) {
    s.Step(`^I have (\d+) cucumbers in my belly$`, iHaveCucumbersInMyBelly)
    s.Step(`^I eat (\d+) cucumbers$`, iEatCucumbers)
    s.Step(`^I should have (\d+) cucumbers in my belly$`, iShouldHaveCucumbersInMyBelly)
}

Cypress-cucumber-preprocessor:

import { Given, When, Then } from "cypress-cucumber-preprocessor/steps";

Given("I have {int} cucumbers in my belly", (count) => {
  cy.wrap({ cucumbers: count }).as("belly");
});

When("I eat {int} cucumbers", (count) => {
  cy.get("@belly").then((belly) => {
    belly.cucumbers += count;
  });
});

Then("I should have {int} cucumbers in my belly", (expected) => {
  cy.get("@belly").its("cucumbers").should("eq", expected);
});

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration

Pros of SpecFlow

  • Supports multiple programming languages and test runners, offering greater flexibility
  • Integrates well with Visual Studio and other .NET development tools
  • Provides a rich set of extensions and plugins for enhanced functionality

Cons of SpecFlow

  • Primarily focused on .NET ecosystem, limiting its use in non-.NET projects
  • May have a steeper learning curve for developers not familiar with .NET

Code Comparison

SpecFlow (C#):

[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int number)
{
    calculator.Enter(number);
}

Cypress Cucumber Preprocessor (JavaScript):

Given('I have entered {int} into the calculator', (number) => {
  calculator.enter(number);
});

Both frameworks allow for easy mapping of Gherkin steps to code implementations. SpecFlow uses attributes in C#, while Cypress Cucumber Preprocessor uses JavaScript functions. The syntax differs, but the concept remains similar, allowing developers to write behavior-driven tests in a readable format.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

cypress-cucumber-preprocessor

Build status Npm package weekly downloads

This preprocessor aims to provide a developer experience and behavior similar to that of Cucumber, to Cypress.

:information_source: The repositor has recently moved from github.com/TheBrainFamily to github.com/badeball. Read more about the transfer of ownership here.

Installation

$ npm install @badeball/cypress-cucumber-preprocessor

Introduction

The preprocessor (with its dependencies) parses Gherkin documents and allows you to write tests as shown below.

# cypress/e2e/duckduckgo.feature
Feature: duckduckgo.com
  Scenario: visiting the frontpage
    When I visit duckduckgo.com
    Then I should see a search bar
// cypress/e2e/duckduckgo.ts
import { When, Then } from "@badeball/cypress-cucumber-preprocessor";

When("I visit duckduckgo.com", () => {
  cy.visit("https://www.duckduckgo.com");
});

Then("I should see a search bar", () => {
  cy.get("input").should(
    "have.attr",
    "placeholder",
    "Search the web without being tracked"
  );
});

Building

Building can be done once using:

$ npm run build

Or upon file changes with:

$ npm run watch

There are multiple types of tests, all ran using npm scripts:

$ npm run test:fmt
$ npm run test:types
$ npm run test:unit
$ npm run test:integration # make sure to build first
$ npm run test # runs all of the above

Attribution

A special thanks goes out to Łukasz Gandecki for developing and maintaning the cypress-cucumber integration before me, in addition to all other contributors. Some of the work has partially been sponsored by Klaveness Digital.

This project is tested with BrowserStack.

NPM DownloadsLast 30 Days