Hassle-Free Start Guide for Ruby, Cucumber, and Capybara on Windows


Outline

I generally use Selenium Webdriver for web test automation projects. It is a great and well-known framework for this purpose. It has a very large community and support so when you get in trouble, you can find the solutions easily. On the other hand, some of my friends are big fans of Ruby for testing. I also attended some of Ruby testing seminars and saw its features and capabilities. In my opinion, Ruby has a very clean coding syntax and it is easy to learn. It does not require too much effort in automation with its libraries such as Capybara for driving web applications, RestClient for interacting with web services, SitePrism for POM (Page Object Model).

In this post, I will explain how to do installation and setup for Ruby, Capybara and Cucumber. It is a quick start tutorial for the Ruby Testing beginners. First, let’s start with definitions.

What is Capybara?

On Capybara’s official page it is described as follows: “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.”

A Sample Capybara Feature:

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.

An example Gherkin script:

Installation and Setups

Step-1: Install Ruby

>> For Windows the best place to install Ruby is http://rubyinstaller.org/downloads/. Go that site and install the latest 32-bit version of Ruby.

Cucumber Capybara

Note: The 64-bit versions of Ruby are relatively new in the Windows area and not all the packages have been updated to be compatible with it.

>> In this post, I will go with Ruby 2.2.4 version and I selected below options during installation. Make sure you check the option to “Add Ruby executables to your PATH.

ruby

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

ruby

>> 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.

ruby

Step-2: Install Ruby Development Kit

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

ruby

>> Extract it into C:/DevKit folder.

ruby

>> 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.

ruby

ruby

>> 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.

ruby

ruby

>> In order to test your installation, run below command.

gem install json –platform=ruby

ruby

If JSON is installed correctly, you should see below screen.

ruby

ruby

Step-3: 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.

cucumber

cucumber

Step-4: Install Capybara

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

capybara

Step-5: Install Selenium Webdriver

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

selenium

Step-6: Install RSpec

>> Run gem install rspec to download and install RSpec. It is an extensive library for assertions. (By the way there is a very nice tutorial at codeschool.com, its name is “Testing with RSpec”. I suggest you to check that course.)

rspec

Step-7: Install Ansicon (Optional)

If you want to colorize command prompt when running cucumber features, you need to install ANSICON because Windows does not understand ANSI color escape sequences.

  • In order to download latest release of ANSICON go to https://github.com/adoxa/ansicon/downloads.
  • Create C:\Ansicon directory and Unzip the download file into it.
  • Open command prompt and navigate to C:\Ansicon folder.
  • Navigate to X64 folder if your machine is 64bit otherwise go to X86 folder.

ansicon

  • Execute ansicon.exe -i to install and add ANSICON to your Windows.

ansicon

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 www.google.co.uk
  • 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.

capybara

>> 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:

directory

>> 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 google.co.uk 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 google.co.uk.

Step-2: After above operation, we are on google.co.uk 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”.

google_search_bar

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”.

yahoo_link

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.

folder

Then, run below command.

cucumber features\test.feature

ruby19

And, enjoy watching the test execution 🙂

ruby20

Conclusion

In this article, I wanted to describe the first steps of test automation with Ruby, Cucumber and Capybara. I hope that you were also successful with these instructions. This article is just the beginning of this topic. You should do more research on reference and official pages to improve your test automation skills and knowledge on Ruby, Cucumber and Capybara.  If you want to ask more questions, please do not hesitate to write your comments in the comments section. Thanks for reading my article. Happy testing! 🙂

References

[1] http://jnicklas.github.io/capybara/

[2] https://cucumber.io/

[3] http://agileforall.com/getting-started-with-ruby-cucumber-and-capybara-on-windows/

[4] http://www.gamesparks.com/blog/automated-testing-with-cucumber-and-capybara/

[5] http://shashikantjagtap.net/cucumber-capybara-poltergeist-rockstar-bdd-combo/

[6] https://www.gridlastic.com/capybara-cucumber-code-example.html

[7] http://code.tutsplus.com/tutorials/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber–net-21446

[8] https://semaphoreci.com/community/tutorials/introduction-to-writing-acceptance-tests-with-cucumber

[9] https://girliemangalo.wordpress.com/2012/10/29/automated-testing-with-cucumber-capybara/

[10] http://code.tutsplus.com/tutorials/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber–net-21446[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 5 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools....
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Getting Started with RobotFramework on Windows

By Onur Baskirt / Apr 25, 2016 / 12 Comments
What is RobotFramework? RobotFramework is a GENERIC test automation framework for acceptance testing and acceptance test-driven development (ATTD). What it means that you can do web, mobile, desktop and other test automation activities with related test libraries. These libraries can...
By | 2017-10-22T02:34:04+00:00 March 17th, 2016|Capybara, Cucumber, Ruby, Test Automation, Web Automation|22 Comments

About the Author:

Onur Baskirt
Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as research assistant, design verification engineer and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. Now, he is working as Head of Software Testing and Manager of two Development Teams at Kariyer.net. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

22 Comments

  1. Anupam June 7, 2016 at 5:52 am - Reply

    Nicely compiled, solved my problem. All ingredients to start learning software testing is collected at one place.

  2. Jen July 2, 2016 at 3:27 pm - Reply

    Thank you made my testing life so easy!

    • Onur Baskirt
      Onur Baskirt July 4, 2016 at 3:58 pm - Reply

      Thank you! 🙂

    • rahna October 6, 2016 at 5:12 pm - Reply

      So so!
      Cool articles! Keep writing, please)

  3. Jenny November 20, 2016 at 5:26 pm - Reply

    Hi,

    When I start execute the test.feature file I’m getting this error:

    Unable to find Mozilla geckodriver. Please download the server from https://github.com/mozilla/geckodriver/releases and place it somewhere on your PATH.

    I have downloaded the file from the link and add to the environment variable path, but is still showing the same error. I must have not done it correctly could you please advise?

    Thanks

  4. Sudha April 10, 2017 at 12:18 pm - Reply

    It is very nice article.

  5. Marco April 19, 2017 at 8:14 pm - Reply

    We tried on several Windows10 with ruby2.2.6p396 (x64 and x86), and we received the next error:

    “Given I am on the Google homepage # c:/users/……./steps_definitions/steps_definitions/test_steps.rb:2

    Incompatible encoding regexp match (Windows-1252 regexp with UTF-8 string) (Encoding: CompatibilityError)”

    Failing Scenarios:
    cucumber features/test.feature: 2 #Scenario: Search a website in google

    1 scenario (1 failed)
    1 step (1 failed)

    But works on Linux.

    I suposed that ruby 2.X is now UTF-8 encoded by default.

    What should we do?

  6. Ana September 14, 2017 at 11:07 pm - Reply

    Hi!
    When I run test.feature I got the following error:
    C:/Automate2/features/support/env.rb:24:in '
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/rb_support/rb_language.rb:96:in
    load’
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/rb_support/rb_language.rb:96:in load_code_file'
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:142:in
    load_file’
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:84:in block in load_files!'
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:83:in
    each’
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:83:in load_files!'
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/runtime.rb:253:in
    load_step_definitions’
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/runtime.rb:61:in run!'
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/lib/cucumber/cli/main.rb:32:in
    execute!’
    C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/cucumber-2.4.0/bin/cucumber:8:in '
    C:/Ruby24-x64/bin/cucumber:22:in
    load’
    C:/Ruby24-x64/bin/cucumber:22:in `’

    I’m using:
    Win10
    ruby 2.4.1p111 (2017-03-22 revision 58053) [x64-mingw32]

    Thanks!

  7. JL October 17, 2017 at 4:40 am - Reply

    on the other hand, I got this error while installing json:

    >gem install json
    Temporarily enhancing PATH to include DevKit…
    Building native extensions. This could take a while…
    ERROR: Error installing json:
    ERROR: Failed to build gem native extension.

    C:/Ruby22-x64/bin/ruby.exe -r ./siteconf20171017-1044-n4dewd.rb extconf.rb
    creating Makefile

    make “DESTDIR=” clean

    make “DESTDIR=”

    make failed, uncaught signal 11

    Gem files will remain installed in C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/json-2.1.0 for inspection.
    Results logged to C:/Ruby22-x64/lib/ruby/gems/2.2.0/extensions/x64-mingw32/2.2.0/json-2.1.0/gem_make.out

  8. JL October 17, 2017 at 4:41 am - Reply

    oops previously comment didn’t post..

    I’m trying on Win10 x64 with ruby 2.2.4 and got this error:

    >cucumber features\test.feature
    undefined local variable or method without_resynchronize' for Helpers:Module (NameError)
    C:/Users/JL/Documents/testcucumber/features/support/env.rb:14:in

    C:/Users/JL/Documents/testcucumber/features/support/env.rb:13:in '
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/glue/registry_and_more.rb:106:in
    load’
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/glue/registry_and_more.rb:106:in load_code_file'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/runtime/support_code.rb:147:in
    load_file’
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/runtime/support_code.rb:88:in block in load_files!'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/runtime/support_code.rb:87:in
    each’
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/runtime/support_code.rb:87:in load_files!'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/runtime.rb:270:in
    load_step_definitions’
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/runtime.rb:67:in run!'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/lib/cucumber/cli/main.rb:33:in
    execute!’
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-3.0.1/bin/cucumber:9:in '
    C:/Ruby22-x64/bin/cucumber:23:in
    load’
    C:/Ruby22-x64/bin/cucumber:23:in `’

    • JL October 17, 2017 at 5:57 am - Reply

      this is fixed after updating some formatting..

      however I don’t know how to set my webdriver properly..
      I have geckodriver and chrome driver stored in the same folder, and already included the path in the Path system variable.

      It says:

      >cucumber features\test.feature
      Feature: Find the Yahoo Website

      Scenario: Search a website in google # features/test.feature:2
      Given I am on the Google homepage # features/step_definitions/test_steps.rb:2
      Could not find Firefox binary (os=windows). Make sure Firefox is installed or set the path manually with Selenium::WebDriver::Firefox::Binary.path= (Selenium::WebDriver::Error::WebDriverError)
      ./features/step_definitions/test_steps.rb:3:in /^I am on the Google homepage$/'
      features/test.feature:3:in
      Given I am on the Google homepage’
      When I will search for “yahoo” # features/test.feature:4
      Then I should see “yahoo” # features/step_definitions/test_steps.rb:12

      Thanks.

      • JL October 17, 2017 at 11:01 am - Reply

        sorry for disturbing, I have geckodriver set and able to run the test on firefox.

        I have 2 questions
        1) what I need to require and set in order to let it run on chrome, and provide me options to choose which browser to run on?

        2) I used to write in cucumber style for the step files,
        e.g.
        When “I will search for $keyword” do |keyword|
        @browser.div(:class => “gstl_0 sbib_a”).exists?
        @browser.div(:class => “gstl_0 sbib_a”).input(:id => “lst-ib”).set(keyword)
        end

        How can I be able to write in the above format? because in capybara I found hard to locate and click on an element.

        Thanks

        • Onur Baskirt
          Onur Baskirt October 17, 2017 at 12:03 pm - Reply

          Alper Mermer is very proficient on Ruby, Cucumber and Capybara. I also asked him for help. When he replied, I will get back to you.

        • Alper Mermer October 17, 2017 at 1:06 pm - Reply

          Hi JL,

          1- To have chrome as your browser, first download the appropriate version of chromedriver for your system and have its location in your path. After that, in your env.rb file add these lines in place of firefox:

          Capybara::Selenium::Driver.new(app, browser: :chrome)

          2- Actually, one of Capybara’s biggest strengths is its dynamic find logic.

          Here is a step in a feature file:
          When I search for “something”

          Here is your step definition:
          When(/^I search for “([^”]*)”$/) do |keyword|
          find_by_id(‘lst-ib’).set keyword
          end

          That’s it, happy testing 🙂

          • JL October 18, 2017 at 4:10 am

            Thanks Alper,

            1- I added these 2 lines in env.rb in order to have firefox works:
            require “selenium-webdriver”
            Selenium::WebDriver::Firefox::Binary.path=’C:\Users\JL\AppData\Local\Mozilla Firefox\firefox.exe’

            do I need to require something in additional to Capybara::Selenium::Driver.new(app, browser: :chrome)?
            and I always get chromedriver.exe has stopped working on cmd no matter what version.

            may I contact you via google handout?

          • JL October 18, 2017 at 4:27 am

            by the way I replaced
            require “selenium-webdriver”
            Selenium::WebDriver::Firefox::Binary.path=’C:\Users\JL\AppData\Local\Mozilla Firefox\firefox.exe’
            with
            Capybara::Selenium::Driver.new(app, browser: :chrome)
            and I get:

            >cucumber features\test.feature
            undefined local variable or method app' for main:Object (NameError)
            C:/Users/julian.tc.li/Documents/testcucumber/features/support/env.rb:9:in

  9. Alper Mermer October 20, 2017 at 3:08 pm - Reply

    Hi JL,

    Here is a configuration i use to run on chrome, you shouldn’t reference anything about Firefox to run on chrome

    Capybara.run_server = false
    Capybara.default_driver = :selenium
    Capybara.javascript_driver = :selenium
    Capybara.default_selector = :css
    Capybara.default_max_wait_time = 10
    Capybara.ignore_hidden_elements = false
    Capybara.exact = true
    Capybara.app_host = ‘http://www.google.com’

    Capybara.register_driver :selenium do |app|
    Capybara::Selenium::Driver.new(app, :browser => :chrome)
    end

Leave A Comment