Categories: Uncategorized

[SeleniumRC][Ruby]Auto Browser Test Using Webdriver Remote with Firefox,IE,Chrome

Background

I used to use Selenium RC to do browser test for my products.
I’d like to share of how to use it with like hello world sample.

Environment

  • OS
    • Linux 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
  • Ruby
    • 2.1.2

Overview



This image from Official Page of Selenium RC.
The sample script’s result is like following.

Development

Directory

.
|-- Gemfile
`-- spec
    `-- sample_spec.rb

Gemfile

source 'https://rubygems.org'
gem 'selenium-webdriver'
gem 'selenium-client'
gem 'rspec'

sample_spec.rb

# encoding: utf-8
require "json"
require "selenium-webdriver"
require "rspec"
include RSpec::Expectations
describe "SeleniumRCTest" do
  before(:each) do
    # Local
    @driver = Selenium::WebDriver.for(:remote, :url => 'http://localhost:4444/wd/hub', :desired_capabilities => :firefox)
    #@driver = Selenium::WebDriver.for(:remote, :url => 'http://localhost:4444/wd/hub', :desired_capabilities => :chrome)
    #@driver = Selenium::WebDriver.for(:remote, :url => 'http://localhost:4444/wd/hub', :desired_capabilities => :ie)

    @base_url = "http://www.google.com/"
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
  end
  after(:each) do
    @driver.quit
    @verification_errors.should == []
  end
  it "test_sample" do
    # Search Amazon
    @driver.get "http://www.google.com"
    @driver.get(@base_url + "/?gfe_rd=ctrl&ei=6IUzU63nMoHN8ge88IHYBw&gws_rd=cr")
    @driver.find_element(:id, "gbqfq").clear
    @driver.find_element(:id, "gbqfq").send_keys "amazon"
    @driver.find_element(:css, "td > span").click
  end
  def element_present?(how, what)
    @driver.find_element(how, what)
    true
  rescue Selenium::WebDriver::Error::NoSuchElementError
    false
  end
  def alert_present?()
    @driver.switch_to.alert
    true
  rescue Selenium::WebDriver::Error::NoAlertPresentError
    false
  end
  def verify(&blk)
    yield
  rescue ExpectationNotMetError => ex
    @verification_errors << ex
  end
  def close_alert_and_get_its_text(how, what)
    alert = @driver.switch_to().alert()
    alert_text = alert.text
    if (@accept_next_alert) then
      alert.accept()
    else
      alert.dismiss()
    end
    alert_text
  ensure
    @accept_next_alert = true
  end
end

Run sample

1. Download Selenium RC standalone server

Visit http://www.seleniumhq.org/download/
Download Selenium Server (current version 2.44.0)

2. Run Selenium RC server

java -jar selenium-server-standalone-2.44.0.jar

3. Download IE and Chrome driver

Visit http://www.seleniumhq.org/download/
and download IEDriver and chromedriver.
After downloading, run the drivers with double click.

4. Run

bundle exec rspec spec/

More Information

zuqqhi2