Tech Tips

  1. プログラミング
  2. 741 view

[SeleniumRC][Ruby]Webdriver RemoteでFirefox,IE,Chrome上で自動ブラウザテスト

Contents

Background

会社でSelenium RCを使って
自動ブラウザテストをしています。
たまに手順を忘れてしまうのでメモ。

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

20141130_overview_image
Selenium RCを使ってブラウザテストをする際の構成のイメージは以下のような感じです。
この画像は Official Page of Selenium RC
から持ってきました。.
20141130_seleniumrc_sample
今回作成するスクリプトの結果は以下のような感じです。

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. SeleniumRCスタンドアロンサーバーをダウンロード

http://www.seleniumhq.org/download/
からSeleniumサーバをダウンロード (current version 2.44.0)。

2. Seleniumサーバの起動

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

3. ChromeとIEドライバのダウンロードと起動

http://www.seleniumhq.org/download/からドライバをダウンロード。ダウンロード完了したら、ダブルクリックなどで起動。

4. 実行

bundle exec rspec spec/

More Information

プログラミングの最近記事

  1. PlatformIO IDE for VSCode を使用して VSCode で Ardu…

  2. ROS Docker イメージで発生した GPG error の解消方法

  3. Streamlit で訪れた国を色づけした世界地図を作成できるアプリケーションを作成してみ…

  4. M5Stack Core2 for AWS – ESP32 IoT開発キットで…

  5. D3.js v7 で点・線・テキスト・ツールチップ・ズームを設定する方法

関連記事

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

PAGE TOP