Set up automation environment using cucumber and capybara

This article is about setting up automation environment using cucumber and capybara from scratch with minimal configuration and the code. For this tutorial I have created a sample project and code for that is available here on github. We will start from nothing and build eventually. So lets start

Directory structure for cucumber

features – This directory will contain all your feature files and some other important directories.
step-definitions – It will have all the step definitions.
support – It will have all the configuration files including env.rb

env.rb for chrome

Now capybara knows that it has to use chrome browser for running the tests.

Chrome driver

In order to run the tests in chrome you have to install chrome driver in your machine. which can be downloaded from here.
Once you download the driver export it in you path or copy the executable to you /usr/local/bin because that is by default included in your path.

Gemfile

rspec gem is used for putting assertions in your step definitions.

Feature File

In this file you can see a scenario where we want to search something on google. And for this we are using three steps. Lets see each step’s implementation in detail.

Given I visit google

In this step we are calling a capybara’s method visit which will take us to google.com

And I search “github ajitsing”

In this step we are searching for a query in google. There are few things to note here.

  1. Variable search_box contains the selector of search input box on google home page. I found this selector using inspect element, which is not so difficult to find. And similarly I have added the selector for search_button in our step.
  2. Now we use capybara’s find method to find that particular element on the page. First we are calling find for getting the search input box and then we are calling the set method on it to fill in the query which we want to search.
  3. And then we are calling find method to find the search button and calling click method to click on the button.
  4. Clicking on the button will take us to the search results page.

Then I pause the browser

This step is used for pausing the test in order to see the result in the browser, not doing that will quit the browser immediately which will not give us time to look into the search results.
Generally we use binding.pry only when we want to debug something. It will open a ruby console on command line to execute the ruby statements directly and see the result on the browser.

Leave a Reply

Your email address will not be published. Required fields are marked *