Start with Ruby WebDriver - Procedure to create first test script for the beginners

Setup of Ruby and Selenium 2
  1. Download ruby setup file  from url http://rubyinstaller.org/downloads . Install the executable file into your system and set Ruby as Environmental variable into your system.
  2. Open command prompt and check “ruby  -v “ to verify ruby installation and path setup.
  3. After ruby installation run below command for selenium2 installation.
gem install selenium-webdriver

Create First Test Scripts:

Capture your test scenario using selenium Ide. import test script into Ruby/ Test::Unit /Webdriver and save as a .rb file in your machine. I have created below a sample Google search script.


require "selenium-webdriver"
gem "test-unit"
require "test/unit"

class Google < Test::Unit::TestCase

  def setup
    @driver = Selenium::WebDriver.for :firefox
    @base_url = "https://www.google.co.in/"
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 30
  end
 
  def teardown
    @driver.quit   
  end
 
  def test_Google
    @driver.get(@base_url + "/")
    @driver.find_element(:id, "gbqfq").clear
    @driver.find_element(:id, "gbqfq").send_keys "testing"
    @driver.find_element(:id, "gbqfb").click
  end
 
  def element_present?(how, what)
    @driver.find_element(how, what)
    true
  rescue Selenium::WebDriver::Error::NoSuchElementError
    false
  end
 
  def verify(&blk)
    yield
  rescue Test::Unit::AssertionFailedError => ex
    @verification_errors << ex
  end 
end

 Open command prompt and Go to created script folder and run command.
  ruby TestFlleName

Test should be executed and after execution screen should look like as below.

3 comments:

  1. Hi.

    I have tried to run the given above script after following all your instruction (install ruby, check version, install gem install selenium-webdriver). when I run the given script in post I faced the following error message.

    Error Message is:
    E:\AutomationWorkspace\SeleniumWorkspace\Ruby>ruby firstRuby.rb
    C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Co
    uld not find test-unit (>= 0) amongst [awesome_print-1.1.0, bigdecimal-1.1.0, bu
    ilder-3.2.2, calabash-android-0.4.6, childprocess-0.3.9, cucumber-1.3.2, diff-lc
    s-1.2.4, ffi-1.9.0-x86-mingw32, gherkin-2.12.0-x86-mingw32, i18n-0.6.4, io-conso
    le-0.3, json-1.5.5, minitest-4.7.5, minitest-2.5.1, multi_json-1.7.7, multi_test
    -0.0.2, rake-0.9.2.2, rdoc-3.9.5, retriable-1.3.3, rubyzip-0.9.9, selenium-webdr
    iver-2.35.1, slowhandcuke-0.0.3, tzinfo-0.3.37, websocket-1.0.7] (Gem::LoadError
    )
    from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `
    to_spec'
    from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems.rb:1231:in `gem'
    from firstRuby.rb:2:in `'


    Please suggest regarding the same.

    ReplyDelete
    Replies
    1. Hi Rahul.

      As you see in error message "Could not find test-unit (>= 0) amongst ....." you need to install test-unit ruby module before run your test

      Thank you to follow blog

      Delete
  2. the second line should not be gem "test-unit". It should be require "test-unit" as you are importing the installed library file.
    Thanks,
    Vishal Aggarwal
    http://testocean.blogspot.in

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution