In the latest installment of our Cucumber Automation Framework series, learn how to drive a browser using WATiR WebDriver.

Blog eight of a series.

Are you ready to finally write some code? If so, you’ve come to the right place. We’re going to walk you through the process of automating each step in your BDD Gherkin script using WATiR WebDriver.

To refresh, we have created step definitions for the following BDD Gherkin script:

WebDriver

In the first step, we will navigate to Google. The step definition created for this is:

WebDriver

Before we can jump to the website, we need to open a browser. If you look in the features/support directory, there is a file called “hooks.rb”. It contains two hooks: Before and After. The Before hook executes before the Scenario is started; the After hook executes after the Scenario is completed.

WebDriver

TestGen auto-populated these for us. In the Before hook, a Firefox browser is opened; in the After hook, the browser is closed. You can add additional browsers if you would like, as shown above. Just keep in mind that you can only have one browser type running at a time.

If you run your .feature file with these hooks in place, you should see a Firefox browser open, then quickly close.

Now that we have a browser, we need to tell it where to go. This is done in the Given step definition using the WebDriver goto method. This will tell the browser to visit the provided URL. I added a sleep command of 10 seconds so you can see the browser going to the website.

WebDriver

We now have one passed step, one pending step, and three skipped steps:

WebDriver

And that is good because it tells us what we need to do next: make the next pending step pass. This is our When step, where we enter the text we want to search on.

Again, we use WebDriver methods to populate the text field. The second line may or may not be necessary, depending on which ChromeDriver you are using (if you are using Chrome as your driver).

WebDriver

Two steps now passed, one pending, and two skipped:

WebDriver

The third step is the Then:

WebDriver

As you can imagine, we are going to use WebDriver to accomplish this:

WebDriver

If you are this far, congratulations! You have searched for results and found them. Now we need to validate that the link actually takes us to the “Centric SQAT Practice page.” Your last two steps will look like this:

WebDriver

Run your .feature file. If all goes well, you will see all green:

WebDriver

Congratulations! You have just written your first BDD Cucumber Scenario and it passed. That is quite an accomplishment. More importantly, you learned a lot about WebDriver’s capabilities. You also learned about:

  • WebDriver’s goto method, which takes to you a webpage
  • WebDriver’s text_field and textarea methods, which allow you to enter text on a webpage
  • WebDriver’s button method, which takes to you to click a button on a webpage
  • WebDriver’s text.include? method, which enables you to validate text on a webpage

There are many more WebDriver methods your agile testing team can use on a webpage to manipulate objects. I would encourage you to do further research on WATiR WebDriver.