Sunday, 20 October 2013

Rspec + Rake Framework in Ruby with Selenium - Webdriver

What is Rspec?

RSpec is a Behaviour-Driven Development tool for Ruby programmers.

What is ‘Behaviour-Driven Development (BDD)’?

Behaviour-driven development focuses on the users’ opinion on how they want application to behave, mainly used for acceptance testing and unit testing
So following is difference between general Test Driven Development (TDD) framework and Behaviour-Driven Development (BDD) framework.


1. TDD(Test Driven Development)
user = “test”
assert_equal(user.name, "test")

2. BDD(Behaviour-Driven Development)-Rspec
user = “test”
user.name.should == “test”


What is rake ?


Rake is a task management tool written in Ruby Language, It specifies task and task dependencies
Rake will be used for reporting with Selenium Webdriver ,that we will see in later part of this article.


Configuration of  rake and Rspec 

Note: Ruby should be already installed, for configuration of Ruby check following link.

Step1. Download following gem and install.
Install:  1. Start->cmd
            2. Go to folder where gem is installed using cd command
            3. Write gem install rspec



Install:  1. Start->cmd
                2. Go to folder where gem is installed using cd command
                3. Write gem install rake

Step 2: Write a script using Rspec framework.
Lets’ consider following is our manual scenario.
1. Verify User is able to Login using credentials  on http://newtours.demoaut.com/
2. Verify user is able to navigate to ‘select Flight ‘ Page
3. Verify User is able to Logout from application

We are going to write following code for above scenario and verify each of the points.


require 'Selenium-Webdriver'
require 'rspec/autorun'

describe "Rspec-example" do
  before(:all) do
    @driver = Selenium::WebDriver.for :firefox
    @driver.navigate.to "http://newtours.demoaut.com/"
    @wait =  Selenium::WebDriver::Wait.new(:timeout => 10)
  end

  after(:all) do
    @driver.quit
  end

  it "Verify User is able to  Login using credentials  on http://newtours.demoaut.com/" do
    @driver.find_element(:name,"userName").send_keys "mercury"
    @driver.find_element(:name,"password").send_keys "mercury"
    @driver.find_element(:name,"login").click
    #Wait until  'flight finder' appears
    @wait.until {@driver.find_element(:xpath,"/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr/td/img")}
    #checking user is navigated to home page
    @driver.find_element(:css,"b  font  font").displayed?.should  == true
    end

   it "Verify  user is able to navigate to ‘select Flight ‘ Page" do
    @driver.find_element(:name,"findFlights").click
    @wait.until {@driver.find_element(:css ,"b  font")}
    #verify Depart is present on page
    @driver.find_element(:css ,"b  font").displayed?.should == true
   end

  it "Verify User is able to Logout from application" do
  #click on Sign-off for Logout
  @driver.find_element(:xpath,"/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td/a").click
    sleep 5
    @wait.until{@driver.find_element(:xpath,"//td/table/tbody/tr/td[2]/table/tbody/tr/td/img")}
    #checking user logged out successfully and landed on sign-on page
    @driver.title.should == "Sign-on: Mercury Tours"
  end
end

Step 3 : execute Rspec  example?
1. Go to Start->cmd
2. Navigate to folder where  .rb file is saved  using  cd command
3. Write rspec   rspec_example.rb


This Run will give total no of examples passed, failed and time taken to execute a test

Now I want to execute many scripts like these one by one, also I need some data like which examples got passed and failed, So we will use Rake for this.


Step 4:  create a rakefile
Create a .rb file to allocate rake task , That file should be named as Rakefile.rb 
Write following code in rakefile.rb

require 'rubygems'
require 'rake'
require 'rspec/core/rake_task'


############# ALL ############# 

desc "Run all tests"
# RUN ALL
RSpec::Core::RakeTask.new('all') do |t|
  t.rspec_opts = ["--format documentation","--color"]
  t.pattern = [
      'spec/*.rb'
  ]
end 

Step 5: Folder structure to execute rake file

Step 6: Execute Test case using Rake

1. Go to Start->cmd
2. Navigate to folder Rake (As shown above)
(Note: Rake folder name can be anything)
3. Execute Command  “Rake All”
Above command will execute all .rb files which are under ‘spec’ folder.






  

2 comments:

  1. Thanks a lot for sharing the information.
    The below link has examples for automation framework in Java . (Includes folder structure pattern, reporting mechanism etcc..)
    http://seleniumeasy.com

    ReplyDelete
  2. Hi,
    I tried Rake All but got :

    rake aborted!
    Don't know how to build task 'All' (see --tasks)

    (See full trace by running task with --trace)

    ReplyDelete