The Issue

We previously utilized the if/else statements with Ruby for Watir Webdriver scripts. Now we can take a look at how we can implement them into our Cucumber feature files and step definitions.

The Code

In the below example, we want to see if a button is visible, and fail it if not. We have implemented the if/else statements; if the browser finds the specified text, it will use the puts command to display output in the command line. If it does not find the text, it will fail the step and display a message in the command line.

For this step, |text| is whatever we write in our feature file. In the below feature file, where the step is implemented, the |text| variable is “Remove” and “Add”:

This will give us the below output in our command line:

The command line output displays the visible validations when the button is found, and the fail when it is not, as per our previously defined step.

The Takeaway

Implementing if/else logic in your test automation allows you to develop more complex and efficient scripts. As you build out your step definitions, you’ll see just how often you use statements!