How to write a common step definition which can be used by Given, When, Then etc. in cypress Cucumber

Viewed 649

I had recently integrated cucumber with cypress and added the cucumber Full support plugin (VS CODE) to get the step suggestions when we develop feature files. Normally with the java-cucumber experience even though we define the step under Given when we write the feature file, the the step suggestions with pop up even if we are writing a Then scenario (Tool : Idea). But in this case, if we define a step with Then

Then("User click on Contact Us",() => {
    cy.get('#basic-navbar-nav > div > a:nth-child(6)').click()
})

When we write the feature, the step suggestions will pop only if we start the step in feature with a Then

Feature: Login

    Scenario Outline: Sample

        Given User go to TestSite
        Then User click on Contact Us

I want to know if we can write a step def which will be common to all Given, When, Then etc. In java-cucumber Intellij-Idea support I think we have a * option insted of Given, When , then etc. Is there a workaround for this in cypress ?

2 Answers

I haven't used that plugin before so I cannot answer the question specifically to that, but to get a more gherkin like syntax in my Cypress tests I commonly use something similar to the below example with context and nested describes.

context('Given ...', () => {
  describe('When ...', () => {
    it('Then ...', () => {
     ...
    })
  })
})

You can achieve a generic step definition by using defineStep as follows:

defineStep("User click on Contact Us",() => {
  cy.get('#basic-navbar-nav > div > a:nth-child(6)').click()
})

Just like Given, When, Then it can be imported from cypress-cucumber-preprocessor/steps

import { defineStep } from 'cypress-cucumber-preprocessor/steps';

You can use this step in your feature file then as Given, When or Then step like:

Feature: Login
    Scenario Outline: Sample
        Given User click on Contact Us
        When User click on Contact Us
        Then User click on Contact Us
Related