The Issue

Sometimes, describing an error isn’t enough. Providing a screenshot will help to articulate your issue (especially for a design issue), and show what section of the website/app is the issue. If we are running an automated test and we know that we will be encountering a page/functionality that has been giving us trouble, including a Ruby & Watir Webdriver screenshot command will give us insight each time we run the script.

The Answer

We can take a screenshot in a ruby script by utilizing the following Watir Webdriver command:

This will take a screenshot at the point in the script you are at, and save it to your local drive. You can name the file whatever you want, and you have to include include a valid file extension. Lets implement this into a script.

The Code

We will navigate to the 3Qi Labs homepage and take a screenshot:

The Result

We can save and run the automated test script (screenshot.rb). After,we run, we can view the results:

  • Browser opens
  • Browser navigates to the 3Qi Labs homepage
  • Screenshot is taken of current browser window

Again, the screenshot will be saved to your local drive, with the name and file extension you specify in the script.

The Takeaway

While simple, you will find use for including the screenshot command in your Ruby & Watir Webdriver scripts, for either reporting purposes, or for your own reference. When debugging, having a screenshot at the point of an error will save time and give you more information to validate your issue with.