Introduction to Selenium with Ruby Capybara and Cucumber

This tutorial is for people who want to learn how to write automated tests using Selenium, with Ruby, Capybara, and Cucumber.

In this example, I will search for the term “Canada” on Duckduckgo.com website and make sure I can see the link to Wikipedia’s page on Canada.

Note I am doing this on Windows but this will it work if you are using a Mac.

To start we will have to install the following: Ruby, Cucumber, and Capybara.

Capybara Cucumber Tutorial

Step 1 Install Ruby

Ruby – Ruby is a programming language. Don’t worry if you are not an expert at Ruby as it is not necessary. First, you need to download the compiler. Go to the following website

http://rubyinstaller.org/downloads/ and download Ruby

Download Ruby
Download Ruby

Once downloaded click on the .exe and follow the instructions as necessary. Make sure you select “Add Ruby executables to your PATH”, and “Associate .rb and .rbw files with this Ruby installation”.

Ruby Setup
Ruby Setup

To check go to the command prompt and type

How to tell what version of Ruby you have
How to tell what version of Ruby you have

Step 2 Create Folder for Project

Now, create a folder for your project.  In this example, I will put all my work in C:\Repo\DuckDuckGo\

Step 3 Install Capybara

Capybara is a library that helps navigate websites. Think about commands such as being to navigate to a website, click on a link or a button, and select a drop down.

Here we want to type “gem install capybara

gem install capybara
gem install capybara

Step 4 Install Cucumber

Cucumber – this is a framework that will allow you to write and execute feature files in the Gherkin Language. Gherkin is a user-friendly language that can be read and understood by product owners, business analysts, developers, and testers.

type gem install cucumber

gem install cucumber
gem install cucumber

Step 5 Install Selenium Webdriver

 

Install Selenium-Webdriver
Install Selenium-Webdriver

Step 6 Initialize Cucumber

Now type cucumber –init

cucumber --init
cucumber –init

Notice the directory structure. It created a features directory. Inside you will put your features files containing your Gherkin Syntax.

Step 7 Install ANSICON

Since you will be running your test through the command line you need to install ANSICON. This simply outputs fonts in colored output.

ANSICON
ANSICON

Now go to the directory where you extracted the file and install ANSICON.

How to Install Ansicon
How to Install Ansicon

Step 8 Set env.rb

Now change directory and go to C:\Repo\DuckDuckGo\features\support> and edit the env.rb file.

Env.rb
Env.rb

 

Step 9 Write Your First Feature File

Now in your features directory and create a file called

DuckDuckGoSearch.feature
DuckDuckGoSearch.feature

DuckDuckGo.features

 

Step 10 Run Cucumber

Now cucumber will suggest the code it wants you to write. Simply copy the code and go into the \step_definitions folder.

Paste the code you copied and paste it into a file called DuckDuckGo.rb

You code should now look like this.

Now you need to “fill in the blanks” for the missing code for all the methods. This is where you use the Capybara.

Run Cucumber
Run Cucumber

Step 11 Create DuckDuckGo.rb File

 

Here you need to go into your step definitions file and create a DuckDuckGo.rb file and copy and paste your step definitions

Initial Step Definition
Initial Step Definition

Step 12 Write the code to complete the Step Definition file

Final Step Definition
Final Step Definition

Step 13 Run Cucumber and Analyse results

Run cucumber
Run cucumber

Trouble Shooting

  1. If you receive the message, “cannot load such file — selenium-webdriver (LoadError)” it means you forgot to install selenium-webdriver.  Type “gem install cucumber”