Book Image

Cucumber Cookbook

By : Shankar Garg
Book Image

Cucumber Cookbook

By: Shankar Garg

Overview of this book

Table of Contents (13 chapters)
Cucumber Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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, and copy and paste the Cucumber Step Definitions suggestions in the LoginSteps.java class. This is how LoginSteps.java looks:

    package com.StepDefinitions;
    
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.When;
    
    public...