Getting Started with Ruby, Cucumber and Capybara on Windows

2 comments, 660 points
Cucumber and Capybara on Windows
Cucumber and Capybara on Windows

Getting Started with Ruby, Cucumber and Capybara on Windows

I’m a very big fan of Selenium WebDriver for functional testing of web projects. On the community there is plenty of documentation how to with Selenium WebDriver but in the other hand few years ago I became a big fan of Capybara and Ruby language as well.

In my current project we are using Capybara for web functional testing and SitePrism for giving a page object structure to the project, Capabash for mobile and RestClient for web services.

It’s a pretty awesome combination in one project and gives you everything you need to brake your apps :).

But recently we had some new starters that chose Windows as an operating system and we said oops , we din’t have any documentation how to setup cucumber and Capybara on Windows.

I’m writing this post because I feel there is a lack of documentation about that.

First of all What is Capybara?

A simple definition from their official page :

Capybara is a library written in the Ruby programming language which makes it easy to simulate how a user interacts with your application. Capybara can talk with many different drivers which execute your tests through the same clean and simple interface. You can seamlessly choose between Selenium, Webkit or pure Ruby drivers. Capybara automatically waits for your content to appear on the page, you never have to issue any manual sleeps.”

Capybara code sample :

   visit '/sessions/new'
    within("#session") do
      fill_in 'Email', with: ''
      fill_in 'Password', with: 'password'
    click_button 'Sign in'
    expect(page).to have_content 'Success'
What is Cucumber?

Cucumber is a framework for writing and executing high level descriptions of your software’s functionality. It supports Behavior Driven Development (BDD). It offers a way to write tests that anybody can understand, regardless of their technical knowledge. The language that Cucumber understands is called Gherkin. Cucumber itself is written in Ruby, but it can be used to “test” code written in Ruby or other languages, including but not limited to Java, C# and Python.

Cucumber Test example :

Scenario Outline: eating
  Given there are 12 cucumbers      # <start> replaced with 12
  When I eat 5 cucumbers            # <eat> replaced with 5
  Then I should have 7 cucumbers    # <left> replaced with 7
Installation :

Go to ruby website : Go that site and install the latest 32-bit version of Ruby.

Ruby on 64-bit version is kind of new in Windows so I suggest you stick on 32-bit for the time being.

Make sure you check the option to “Add Ruby executables to your PATH.

Check Ruby installation with ruby -v command. You will see the Ruby version as shown below.

Check RubyGems installation by running gem -v on a command line. RubyGems is the package manager for Ruby and should come with the standard Ruby installation.

Install Ruby Development Kit

Download Ruby Development Kit in the same page. It will allow Ruby to build native extensions for libraries.

Extract it into C:/DevKit folder.

Open command prompt and go to C:/DevKit folder and run ruby dk.rb init command to generate config.yml file to be used later.

Run ruby dk.rb install This step installs (or updates) an operating_system.rb file into the relevant directory needed to implement a RubyGems pre_install hook and a devkit.rb helper library file into <RUBY_INSTALL_DIR>\lib\ruby\site_ruby.

In order to test your installation, run below command.

gem install json –platform=ruby

Install Cucumber

Run gem install cucumber command to download and install Cucumber automatically. After installation, run cucumber –help command to confirm that installation is successful.

Install Capybara

Run gem install capybara to download and install the Capybara web automation library.

Install Selenium Webdriver

Run gem install selenium-webdriver to download and install the Selenium web automation framework.

Install RSpec

Run gem install rspec to download and install RSpec. It is an extensive library for assertions.

Starting Automation with Ruby & Capybara & Cucumber

First, we need to create a feature directory. We add all feature files into this folder.

Feature files should be written with Gherkin Syntax (Given-When-Then). Gherkin, a domain-specific languages (DSL) that “lets you describe the software’s behavior without detailing how that behavior is implemented.”

Each scenario can have up to three parts: Givens, Whens, and Thens:

  • Given – Given lines describe what pre-condition should exist.
  • When – When lines describe the actions you take.
  • Then – Then lines describe the result.

There are also And lines, which do whatever the line above them does. For example:


In this case, the first And line acts as a Given line, and the second one acts as a Then line.

Go into the features folder and create test.feature file. Our test scenario is:

  • Go to
  • Search yahoo
  • See the yahoo in search results
  • Click the yahoo link
  • Wait 10 seconds

Now, open test.features file with notepad++ or another text editor and write above test scenario as Gherkin syntax.

After writing the feature file try to run the test with below command.

cucumber features\test.feature

We haven’t defined the test steps yet. Thus, we will get below result after test execution.

In order to prepare the step definition file, first create a folder as step_definitions in features folder and then create test_steps.rb file. The directory structure should be like that:

You can copy the code snippets of the test result and paste into test_steps.rb file. Now, we can write the test steps. I want to show you how to do it, step by step.

Step-1: First, we need to navigate to website. Capybara provides visit method for this function. In Selenium, we can do this with driver.get() or driver.navigate().to() methods. So, we should add below line to go

Step-2: After above operation, we are on page, now it is time to write the search text into the search bar. As shown below figure, search bar’s id is “lst-ib”.

Capybara provides us fill_in method for text entrance operations. We can do this operation with below line. In Selenium, we can do this with textElement.sendKeys(String) method.

Step-3: Then, we need to check expected search results on current page. To do this we can use the page.should have_content method. In Selenium, we can do this with Junit, TestNG, or Hamcrest assertions. For example, assertThat(element.getText(), containsString(“yahoo”));

Step-4: Then, it is time to click the yahoo link. As shown below figure link text is “yahoo”.

In Capybara, we can use click_link method to do this. In Selenium, we can do this with driver.findElement(By.linkText(“Yahoo”));

Step-5: At the last step, we will wait for 10 seconds statically to see yahoo website. To do this, we can use sleep(10) method. In Selenium, we use Thread.sleep(10000);

Now, it is time to put them in together. Our test_steps.rb code is shown below.

After writing step definitions, create a support folder into features folder, and create a env.rb file for environmental setups. Then, just copy and paste below environmental setup into that file and save it. I got this setup from reference-3.

And Finally, we are ready to run our feature file. First, go to your project directory. It contains features folder as shown below.

Then, run below command.

cucumber features\test.feature

And, enjoy watching the test execution 🙂

Happy Testing!

Like it? Share with your friends!

2 comments, 660 points
Test engineer


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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    1. Hey Jordano,

      you are welcome, anyway if you want to get the best of ruby and capybara i strongly suggest to switch to Ubuntu Linux, us free and will give you the chance to be on latest ruby bindings and update your gems as well.