Search code examples
cucumbercalabash

Cucumber Step Definitions - Must use Given, And, Then ... etc?


Do I have to use Given, And, Then, etc... when I am doing a step definition?

For example in my feature file I have Then I login to the app in one scenario. In another scenario I have And I login to the app.

I don't like all the Then And Given 's in step definitions. Is there a way to not have to specifically use these keywords and use some sort of wildcard? I've thought about just using When in step definitions. I just hate mixing all these keywords when they don't really matter anyway.

I wish I could define the step in my step_definition file without having to use various (meaningless) keywords in front of it.


Solution

  • If you want to change this in your feature file, instead of using one of the step definition keywords (Given, When, Then, And, But) you can use an asterisk (*) in your feature file as a sort of bullet point. Example

    Scenario: Test Scenario
    * run a sample step
    * run a different sample step
    

    This will generate

    Given(/^run a sample step$/) do
      pending # express the regexp above with the code you wish you had
    end
    
    Given(/^run a different sample step$/) do
      pending # express the regexp above with the code you wish you had
    end
    

    Also, Gherkin supports adding languages, this uses a JSON file to define the keywords for the step. This could be used to change what keywords were used for step definitions.

    You could fork Gherkin and clone it. Then edit i18n.json to add a new language which uses whatever keywords you wanted instead of these. It currently supports pirate for example. After this you would either have to submit a pull request for them to add the language definition to the project or build Gherkin yourself from the cloned fork (https://github.com/cucumber/gherkin/blob/master/README.md)

    Here is the reference page for adding a language

    https://github.com/cucumber/cucumber/wiki/Spoken-languages