Acceptance testing with Cucumber and Capybara

Cucumber is a software testing tool used for defining the acceptance test cases and running it.

Cucumber itself is written in Ruby Programming language. It uses ‘Gherkin’ language to define the test cases and acceptance tests are written in behavior-driven development style.

Please refer the below links to know more about Cucumber(Features, Step Definitions)

https://cucumber.io/

https://en.m.wikipedia.org/wiki/Cucumber_(software)

Capybara is a library used for simulating the user actions. Please refer https://github.com/teamcapybara/capybara to know more about Capybara.

In this article, I am going to show how we can test the “Yelp” website with Cucumber and Capybara.

My objective is to write an acceptance test which opens up a browser and goes to ‘Yelp’ site and search for a restaurant and validate the results.

Here is the Feature file. It’s written in Gherkin language.

Refer the code atĀ https://github.com/dkbalachandar/ruby-cucumber-test

ruby-cucumber-test/features/yelp.feature

@run
Feature: Search Restaurants
  Scenario: Go to yelp and search for valid restaurant
    Given a user goes to Yelp
    When Search for taco bell
    Then See the List of taco bell Restaurants

   Scenario: Go to yelp and search for restaurant
    Given a user goes to Yelp
    When Search for Qboba
    Then See the List of Qboba Restaurants

   Scenario: Go to yelp and search for restaurant
    Given a user goes to Yelp
    When Search for Chipotle
    Then See the List of Chipotle Restaurants
  
  Scenario: Go to yelp and search for invalid restaurant
    Given a user goes to Yelp
    When Search for hhahsdahsdhasd
    Then See No Results found error message

  Scenario Outline:Go to yelp and search for <searchText> 	
    Given a user goes to Yelp
    When Search for <searchText> 	 
    Then See the List of  Restaurants
    Examples:
	    |searchText|
	    |Scardello|
	    |Roti Grill|
	    |Mughlai Restaurant|
	    |Spice In The City Dallas|			


Here is the step definitions file. Its a Ruby file and uses Capybara to simulate the user actions.

ruby-cucumber-test/features/step_definitions/yelp-step.rb


Given(/^a user goes to Yelp$/) do    
  visit "https://www.yelp.com"   
end

When(/^Search for (.*?)$/) do |searchTerm|
  fill_in 'dropperText_Mast', :with => 'Dallas, TX'    
  fill_in 'find_desc', :with => searchTerm
  click_button('submit')
end

Then(/^See the List of (.*?) Restaurants$/) do |searchTerm|  
 expect(page).to have_content(searchTerm)
 expect(page).to have_no_content('No Results')
end

Then(/^See No Results found error message$/) do
 expect(page).to have_content('No Results')
end

This file has all the Env related configurations. I have used ‘Chrome’ as my default browser instead of Firefox. Those configurations can be defined here.

ruby-cucumber-test/features/support/env.rb


require 'capybara/cucumber'
require 'colorize'
require 'rspec'
Capybara.default_driver = :chrome 
Capybara.register_driver :chrome do |app|
   Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Below is the Gemfile for my program. This file is used for describing gem dependencies for Ruby program.

ruby-cucumber-test/Gemfile


source "https://rubygems.org"

gem "cucumber"
gem "capybara"
gem "selenium-webdriver"
gem "rspec"
gem "chromedriver-helper"

Follow the below steps to run thisĀ 


1. Install Bundler (http://bundler.io/): gem install bundler
2. Run bundler: bundle install
3. Start test: cucumber --tag @run

After running the test case, the output will be like below.

cucumber.jpg

Advertisements