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…
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
Now run the Feature file, and copy and paste the Cucumber Step Definitions suggestions in the
LoginSteps.java
class. This is howLoginSteps.java
looks:package com.StepDefinitions; import cucumber.api.java.en.Given; import cucumber.api.java.en.When; public...