loader image
Ruby on Rails

Behavior Driven Development and Testing in Ruby

Understanding the Behavior Driven Development and Testing to Ruby on Rails using Capybara libraries can be quite easy as you will see in this blog. Here is a brief explanation of how to test the actual behaviour of the system from an end-user perspective using BDD (Behavior Driven Development) based testing. There are many BDD testing tools for rails, such as the renowned RSpec is considered as a main library and cucumber for writing high-level tests. But here, we are using capybara framework as it is most helpful for this testing.

BDD testing in ruby

Capybara is one of the well-known web-based automation frameworks to build functional tests that provide simulation on how the users might interact with your application. Numerous web-based automation tools like Selenium, Watir, Capybara, etc. that are readily available to solve the same purpose. Although they are similar, they are meant to solve the same problem with a slightly different methodology to suit different test types. 

The codebase used for testing is in macOS Catalina(10.15.2).

Pre requests

  • Install Chrome or Firefox web drivers
  • Ruby – 2.6.3

List of gems required

  • gem ‘capybara’
  • gem ‘poltergeist’
  • gem ‘selenium-webdriver’
  • gem ‘nokogiri’

How to install Chrome driver in mac

Step1: Get Chrome Driver for Mac

As a first step, download chrome driver for Mac operating system using the link below,

Link: https://sites.google.com/a/chromium.org/chromedriver/downloads

Now, check your downloads folder and unpack chromedriver_mac64.zip file. You will see the chrome driver executable file.

Step2: Move the driver to /usr/local/bin folder

Next step you need to define it as a path in your system. You can check the availability of global path on your system by using the below command,

Now, using the below command you can move the chromedriver file to the /usr/local/bin folder.

Note: Executing unsigned apps on OS X Catalina

If the app is unsigned, you cannot execute using Catalina in (10.15.2). In that case, you can execute it by making use of the below command,

Now, all the prerequisites are completed and Selenium tests can be done using chrome driver.

Basic DSL

Capybara has an intuitive DSL which is used to express executable actions. These are the list of commands that are used for testing.

Navigate to any Page,

Click any specific link or text,

Choose any specific radio buttons,

Checking/unchecking any checkbox,

Click any specific button by its name,

Fill up any specific field,

Select any values in the dropdown,

Upload/attach any files,

JavaScript and Handling Asynchronous Calls

Capybara has an inbuilt internal mechanism to handle asynchronous loading of parts of the page. An ideal example is to click on some part of the page. This will make JavaScript execute and create a new element on the page.

For now, let’s assume that a user is filling an address. Here, Dropdown 1 contains the state values and Dropdown 2 contains the values of cities. In this case, dropdown 2 values will be empty initially and it will be auto-populated based on the value selected by the user in dropdown 1. Hence we need to define a waiting time to indicate the system to wait for some time after the click of Dropdown 1. Once the asynchronous process is completed on the link Dropdown 1, it will wait for 2 seconds (default time) and then link Dropdown 2 will be selected. The waiting time can be configured using the below command, 

Defining the wait time is a good practice as it helps to avoid the cause of unexpected failures in the script.

Capybara also supports execution of JavaScript using the below command,

JavaScript code can be passed like this:

Debugging

For debugging the test case, you can take the save and open the HTML in the browser. Use the below command to launch the page in the browser. But, you need “gem ‘launchy’”.

For taking screenshot of success and failure case by using below command

Use also use pry gem for debugging in between test case.

Example

For example, I am going to open the Wikipedia and search “ruby on rails” and then navigate to the GitHub repo page. Then, take a screenshot of both, save the page as HTML and open in the browser.

You just copy the ruby code and create some_file_name.rb  file 

Just run the following.

BDD in Ruby

 

This is everything you should know about the Behavior Driven Development and Testing in Ruby on Rails using Capybara libraries. Also, read more such blogs by subscribing to us. 

Get in touch with us to hire the best Ruby on Rails developers in the industry. 

Looking for a Tech partner to dominate the digital world?
The following two tabs change content below.
Guru Moorthy

Guru Moorthy

An active Full Stack Blockchain developer having 3+ years of experience in Blockchain. With great passion, he immensely adapted himself in complete life cycle of Blockchain & Crypto development. And, This rising Blockchain enthusiast have forked and modified 12 out of top 20 Crypto coins and aiming to explore all use cases of Blockchain.

Leave a Reply

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

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.