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]