Using Regular Expressions to optimize Step Definitions

Until now, we have created Step Definitions with one-to-one relations with Steps. But this way of writing Step Definitions can be cumbersome as we write more and more Feature files. So, we will write generic Step Definitions that will apply to all the Steps that follow a certain pattern, thus bringing down the number of Step Definitions required. Let's see how to do this in this recipe.

How to do it…

  1. Let's assume we are writing Step Definitions for the following Scenario.
    Scenario: login fail - wrong username
        Given user is displayed login screen
        When user enters "wrongusername" in username field
        And user enters "123456" in password field
        And user clicks Sign in button
  2. Now run the Feature file, ...

Get Cucumber Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.