Check Your Web-App’s Functionality Using Rspec, Cucumber And Capybara With Selenium

Sometime it is not possible or even feasible too check each functionality of your website/web-app. Or you added a new feature in you site/app – now you want check that if the new functionality breaks old one’s. Now you can go through all the features by yourself or you can automate it. As I am a lazy programmer – I always go for less work. To automate I find Rspec + Cucumber + Capybara With Selenium. I am not a Ruby programmer. So when I start implementing my tests, it was a bit hard for me to figure out how to it. In this post you will see how easliy you can test you app even if you are not a ruby programmer. But you do need to know basic ruby. Here is link to learn basic ruby http://www.tutorialspoint.com/ruby/index.htm.

First install ruby and rvm . Then we need to install http://gembundler.com. Now create a folder named “test”. Create Gemfile. Then add the following codes.

mkdir test
cd test
nano Gemfile

source "https://rubygems.org"
gem "rack", "~>1.1"
gem "rspec", :require => "spec"
gem "capybara"

Now run bundle common. It will install all the necessary gems.

bundle

Now create spec, spec->features and spec->support folders. Create a ruby file named spec_helper.rb.

spec_helper.rb file will hold all the necessary gems and configs.

require "rubygems"
require "bundler/setup"
require 'rspec' 
require 'capybara'
require "capybara/rspec"
require "support/features/session_helpers"
require 'capybara/dsl'

Capybara.default_driver = :selenium
Capybara.app_host = 'http://www.your-site.com'

RSpec.configure do |config|
  config.include Features::SessionHelpers, type: :feature
end 

Here we make selenium our default driver and set our websites main url. In RSpec configuration we include session so that we can login to in site.

For this post we will just check user sign up and login. Create another folder in spec->support->features. In this folder we will add functions that can be used our test. Lest create a ruby file named session_helpers.rb. We will add a function named sign_up_with and sign_in_with.

module Features
  module SessionHelpers
    def sign_up_with(name, email, password, confirm_password)
      visit '/users/new'
      fill_in 'name', with: name
      fill_in 'email', with: email
      fill_in 'password', with: password
      fill_in 'confirm_password', with: confirm_password
      click_button 'Register'
    end

    def sign_in_with(email, password)
      visit '/login'
      fill_in 'login', with: email
      fill_in 'password', with: password
      click_button 'Submit'
    end
  end
end

visit is method to navigate to other pages.fill_in finds the element and fills with values. click_button clicks button.

Now create a ruby file signup_spec.rb where I will write two test – wrong email address and with all correct values. We have to add “_spec” every ruby file. Rspec will look for all files with “_spec” in filename.

require 'spec_helper'

feature 'Signup' do
  valid_email = 'hhhhbk+' + Time.now.to_f.to_s + '@gmail.com'

  scenario 'with invalid email' do
    sign_up_with 'me me', 'invalid_email', '123456', '123456'
    expect(page).to have_content('Please enter a valid email address')
  end

  scenario 'with all valid info' do
    sign_up_with 'me me', valid_email, '01919046260', '123456', '123456'
    expect(page).to have_content('Check your email to verify your email address')
  end
end

For learn about scenario https://github.com/cucumber/cucumber/wiki/Scenario-Outlines and for feature https://groups.google.com/forum/?fromgroups=#!topic/cukes/HVZogRjk4yE.

To run test just type rspec on command prompt and it will run all the test show you if the test are ok or not.

rspec

Now you can add more test for your app. Happy Testing.

About Zakir Hyder

This entry was written by .

07. March 2013 by Zakir Hyder
Categories: Black-box Testing, Capybara, Cucumber, Rspec, Ruby, Selenium | Tags: , , , , , | Comments

Comments

  1. […] with Capybara’s find because it looks for name/id/label text for matching element. Check Check Your Web-App’s Functionality Using Rspec, Cucumber And Capybara With Selenium. In perfect world, it is more than enough. But we dont live in perfect world – we live in a […]

  2. […] setup you mac as mentioned in Check Your Web-App’s Functionality Using Rspec, Cucumber And Capybara With Selenium. As of writing this post you can use firefox version 19 and chrome version 26 with […]

  3. […] Sometime it is not possible or even feasible too check each functionality of your website/web-app. Or you added a new feature in you site/app – now you want check that if the new functionality breaks old one's. Now you can…  […]