Search code examples
cucumberbddcucumber-jvmgherkin

Is it possible to introduce custom step keyword in Cucumber?


I would like to define steps using my custom keywords besides existing ones in Cucumber, i.e.:

As an admin

I want to perform something

...

Is it possible to define custom keywords in Cucumber besides Given/Then/When/And/But?


Solution

  • '*' Keyword

    '*' is very special. This defies the whole purpose of having Given, When, Then and all the other keywords. Basically Cucumber doesn’t care about what Keyword you use to define test steps, all it cares about what code it needs to execute for each step. That code is called a step definition. Just remember that all the keywords can be replaced by the * keyword and your test will just work fine.

    Let’s see with example:

    Feature: LogIn Action Test Description: This feature will test a LogIn and LogOut functionality

    Scenario: Successful Login with Valid Credentials
    Given User is on Home Page
    When User Navigate to LogIn Page
    And User enters UserName and Password
    Then Message displayed Login Successfully

    Using * Keyword

    Feature: LogIn Action Test Description: This feature will test a LogIn and LogOut functionality

    Scenario: Successful Login with Valid Credentials
    * User is on Home Page
    * User Navigate to LogIn Page
    * User enters UserName and Password
    * Message displayed Login Successfully

    Given, When, Then, And can all be replaced with '*' keyword. And tests will work just fine.