The Issue

We’ve previously used the Ruby puts/print statements with Watir Webdriver, as well as the fail statement. Now we can take a look at how to implement our puts/print/fail statements into a Cucumber step definition and feature file.

The Code

We can utilize puts/print by placing it within our step definition, ideally to coincide with a particular click, validation, or fail:

Here, this step will first call a pre-existing step with this line:

This specified step already exists in the  step definition, and is a basic step using the “element, how, what” variables to fill in our Watir Webdriver command.

The “element” could be button, link, etc.

The “how” would be :id ,:text, etc.

The “what” would be the object identifier, which is “Stream” in the above example.

Now that we understand what the called step is doing, we can look at the rest of our function. The step will be executed unless a certain text, defined by “target”, is found in our element. If it is found, we will display a message using puts, and fail it. We have placed string in parentheses after our fail command, containing references to each of our variables:

If the fail is executed, it will display this string as output in the command line, filling in the variables with what we have defined in the step. Thus, our command line would display both the text we found (thanks to our puts command), and the message we have added after our fail command.

The Takeaway

The puts/print/fail Ruby statements can be utilized in conjunction with your Cucumber scripts to display desired info in your command line during an automated test run. As is this case with using it for Watir and Selenium scripts, you can incorporate puts/prints in order to emphasize specific clicks, validations, or fails. You can incorporate fail to ensure your script stops given certain condition is met, allowing you to tailor your automation scripts to match your test cases.