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.
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.
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
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.
Now you can add more test for your app. Happy Testing.