There are a two often overlooked Cucumber features — rerun and embed. Implement both of them and you’ll save time, improve your ability to diagnose failures, and be regarded as some kind of automation superhero. Read on for a quick and easy tutorial.
Once upon a time, we would restart the entire automation suite upon the detection of failures. The idea being, if the same scenarios fail, it’s likely a bug. Unfortunately, this adds a lot of time to the CI process. This bugged the crap out of me, so when I tried out a little Cucumber feature called rerun I was overjoyed.
Rerun is a Cucumber output format that prints failing scenarios, which can be saved to a file. To implement this, create two profiles in the cucumber.yml file:
first_try: -f rerun -o rerun.txt -r features/ second_try: @rerun.txt
Then, if you’re using Jenkins, have the job execute this shell script:
cucumber --profile first_try || echo 'First run had failures' if [ -s "rerun.txt" ]; then cucumber --profile second_try fi
Now, only the failures are rerun, saving you a lot of time!
Notice that the first line of the shell script is a conditional using the “or” operator. This is done so that if first_try fails, the echo command executes and the conditional returns true. That way, if first_try fails and second_try succeeds, the Jenkins job is marked as a success.
Anyways, that’s the basic setup. It really is that easy. Let me know if you have any questions.
If you’re using HTML or JSON as the Cucumber results output format, you can embed screenshots of failures into the report. Often times a quick glance at the screenshot is all you need to identify the cause for the failure. The Cucumber embed method call looks like this:
The idea is to wrap that inside a hook. So, move over to your hooks.rb file and add this:
After do |scenario| if scenario.failed? failed_screenshot = "/path/to/screenshot.png" @browser.screenshot.save failed_screenshot embed(failed_screenshot, "image/png") end end
Note, line 4 might not be applicable depending on what you use for web automation. I use Watir-WebDriver, in which case @browser is an instance of a Watir::Browser object, and calling screenshot.save on that object is how you take and save a screenshot. That line will be a little different depending on what technology you use.
So here we have another timesaving feature that requires little code to implement! Enjoy handy embedded screenshots in your HTML or JSON reports.