The Issue

We can utilize the Ruby statement Fail if we want the script to stop and skip all subsequent steps, depending on if a condition is met. Fail in Ruby is will output the same result as raising an exception (as discussed here here); both will produce a Runtime Error in the command line, stopping the script. If we add string after a fail command, it will display the specified text with the Runtime Error. Lets take a look at how we can incorporate it into a Watir Webdriver/Selenium script.

The Code

Here we will fail the script if it does not find the text “Trump” in our indicated element. If the script does contain the desired text, it will continue to the next step until completion.

If Trump is not found, the command line will display this:

If Trump IS found, we will simply see:

The Takeaway

Using the Ruby statement fail is another way to improve your test automation scripts. Whether Watir Webdriver/Selenium or Cucumber, fail will allow you to introduce additional logic and ensure your automation script is consistent with your test case.