0

Writing test cases using Cypress and Cucumber.

I have a few test cases written in Cucumber Syntax like following:

@focus
Scenario: All Accessibility tabs (Color scheme, Font size, Zoom) are visible when a user configures navigation.show_accessibility to be true
    Given Set navigation.show_accessibility to true
    When Trigger Accessibility configuration window
    Then Accessibility window has three tabs

@focus
Scenario: All Accessibility tabs (Color scheme, Font size, Zoom) are visible when a user configures navigation.show_accessibility to be false
    Given Set navigation.show_accessibility to false
    When Trigger Accessibility configuration window
    Then Accessibility window has three tabs

As we can see, among the two test cases, there are two testing steps that is present in all of them.

In implementation files, it is a better practice to group steps that belong to a test case together in the same order as they are in the Cucumber file. In my case, I would have something similar to this:

//All Accessibility tabs (Color scheme, Font size, Zoom) are visible when a user configures navigation.show_accessibility to be true
Given('Set navigation.show_accessibility to true', () => {
});

When('Trigger Accessibility configuration window', () => {
});

Then('Accessibility window has three tabs', () => {
});

//All Accessibility tabs (Color scheme, Font size, Zoom) are visible when a user configures navigation.show_accessibility to be false
Given('Set navigation.show_accessibility to false', () => {
});

When('Trigger Accessibility configuration window', () => {
});

Then('Accessibility window has three tabs', () => {
});

As we can see, I would end up with a few identical step implementations in my file. My question is:

  • How do I organize my implementation steps in a logical manner as in its Cucumber and not have any duplicated steps in my implementation file?
5
  • 1
    I'm not experienced in Cypress, but will your solution even run? How can Cucumber know if it should run the first or the second 'Trigger Accessibility configuration window'? (The following point may become my answer): In Cucumber, scenarios and step def. are decoupled - You can group them together, but you shouldn't duplicate step def. If two different behaviors are phrased in the same way, you have a problem with your project's naming. You should strive for ubiquity when naming things (and BDD libraries like Cucumber force you to do so). Mar 19, 2021 at 8:43
  • Why you need duplicate steps ?
    – PDHide
    Mar 21, 2021 at 9:16
  • @PDHide, I want to present step implementation files in a logical manner as its cucumber feature file.
    – Yu Zhang
    Mar 21, 2021 at 9:30
  • 1
    in cucmber there is no one to one mapping , it goes through all step definitions and finds the respective step. So you cannot and shouldn't have duplicate steps
    – PDHide
    Mar 21, 2021 at 9:33
  • in both the steps are you doing exactly the same thing ?
    – PDHide
    Mar 21, 2021 at 9:34

3 Answers 3

1

I recommend reading this post: https://github.com/TheBrainFamily/cypress-cucumber-preprocessor/issues/60 it deals with basically your situation with the exception that the same step definition names are in different files.

Although I haven't tried it myself now, it does seem from the discussion on the linked github page that cypress-cucumber in the past just used the first loaded step, but it was later changed in a way that you get an exception in such a situation. So it seems your example would not run and you'd receive "Multiple step definitions match" exception.

It'd be nice if you tried it and confirmed if this is true, so that we have all information even here in this question.

1

Don't Repeat yourself: Define function once and call with parameters to decide lower level function calls

I am not sure about test structure between cypress & Cucumber; but in general I would make accessibility configuration a high level function where true|false are parameters.

Based on decision logic inside the higher level function, lower level setting functions will be called as required.

1

In cucumber framework there is no one to one mapping between feature file and step definition. Cucumber just goes through all step definition classes and see of there is any definition for the step from feature file.

So you cannot and shouldn't have duplicate step definition for same step.

  1. As the term says step definition its not Feature definition you shouldn't focus on have to make step definition look exactly like feature file.

  2. If a step has same name it means it supposed to do the same thing under the hood , so you don't have to have duplicate step definition for that .

  3. use page object model :

eg:

homepage class:

class homepage(){
  function login(){
     username.sendkeys("something")
     password.sendkeys("something")
     password.submit()
 }
}

step definition

  import homepage
   
   homepage = new homepage()

   @When("User credentials are valid"){
        homepage.login()
   }

   @Then(" Dashboard is displayed"){
          
         expect(dashboard).to.be.displayed)
   }
  
}

Feature File:

   Scenario : User authorization
   Given: User in homepage
   When: user credentials are valid
   Then: Dashboard is displayed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.