導入しようにも手順とかそういったものが、あまり見つからなかったので、最低限まとめてみました。
環境準備編
リモート(linux)
sudo su
cd /usr/local/src/
curl -O https://selenium.googlecode.com/files/selenium-server-standalone-2.32.0.jar
java -jar selenium-server-standalone-2.32.0.jar -role hub
クライアントMAC
mkdir -p ~/src
cd ~/src
curl -O https://selenium.googlecode.com/files/selenium-server-standalone-2.32.0.jar
java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://<<リモートサーバのIP OR HOST>>:4444/grid/register
実装編
gem install
sudo gem install selenium-webdriver
これだけ
サンプルプログラム作成
selenium.rb
require "test/unit"
require "rubygems"
require "selenium-webdriver"
class GoogleSearchTest < Test::Unit::TestCase
def setup
# 127.0.0.1なのは、ローカルにクライアント・サーバを起動しているからだよ
@driver = Selenium::WebDriver.for :remote, :url => "http://127.0.0.1:5555/wd/hub", :desired_capabilities => :firefox
end
def teardown
@driver.quit
end
def test_search_schoo
# 1. google検索ページへ遷移
@driver.navigate.to "http://www.google.co.jp/"
# 2. 遷移先のページタイトルが「Google」である事
assert_equal( "Google", @driver.title)
# 3. input name='q'のエレメントを取得
element = @driver.find_element(:name, 'q')
# 4. schooをエレメントに送信
element.send_keys "schoo"
# 5. 送信ボタンを押す
element.submit
# 6. 2秒ほど待ちますよ
sleep(2)
# 7. 遷移先のページタイトルを出力しちゃうよ
p @driver.title
# 8. 検索結果にschooという文言が存在するかチェックしちゃえ、存在しない場合はテスト失敗、そしてエラー文言出力
assert( /.*schoo.*/ =~ @driver.title, "検索結果にschooが存在しない。")
# 9. schoo(スクー) WEB-campusというリンクをクリック
@driver.find_element(:link_text, 'schoo(スクー) WEB-campus').click
# 10. また2秒ほど、描画待ちなんで
sleep(2)
# 11. 遷移先のページタイトルを出力
p @driver.title
# 12. schoo(スクー) WEB-campusというタイトルだよねー?
assert( "schoo(スクー) WEB-campus" == @driver.title, "クリックしたリンクが想定外のページ")
end
end
テスト編
実行
ruby selenium.rb
テスト結果
Run options:
# Running tests:
[1/1] GoogleSearchTest#test_search_schoo"schoo - Google \u691C\u7D22"
"schoo\uFF08\u30B9\u30AF\u30FC\uFF09 WEB-campus"
Finished tests in 19.673129s, 0.0508 tests/s, 0.1525 assertions/s.
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
ruby -v: ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.0]
ついでに別ブラウザも
cd ~/src
curl -O https://chromedriver.googlecode.com/files/chromedriver2_mac32_0.8.zip
unzip chromedriver2_mac32_0.8.zip
追記1(Rspecへお引越し)
sudo gem install rspec
sudo gem install selenium-client
selenium_rspec.rb
#! ruby -Ku
# coding: utf-8
require "rubygems"
require "selenium-webdriver"
require "rspec"
include RSpec::Expectations
describe "xxxx" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
@verification_errors = []
@driver = Selenium::WebDriver.for :remote, :url => "http://127.0.0.1:5555/wd/hub", :desired_capabilities => :chrome
@base_url = "https://www.google.co.jp"
@accept_next_alert = true
@driver.manage.timeouts.implicit_wait = 30
end
before(:each) do
#@selenium_driver.start_new_browser_session
end
··
after(:each) do
@driver.quit
@verification_errors.should == []
end
··
it "test_xxxx" do
@driver.get(@base_url + "/")
expect(@driver.title).to eq("Google")
@driver.find_element(:id, "gbqfq").click
@driver.find_element(:id, "gbqfq").send_keys "schoo"
@driver.find_element(:id, "gbqfb").click
sleep 2
#/.*schoo.*/ =~ page.get_title.should be_true
@driver.find_element(:link, "schoo(スクー) WEB-campus").click
sleep 2
#expect(page.get_title).to eq("schoo(スクー) WEB-campus")
expect(@driver.title).to eq("schoo(スクー) WEB-campus")
end
end
ついでにrspecのshoulは古い!expectを使おう
http://qiita.com/items/d880250adc8cdbe7a32f
とあったので、便乗してみました。
javascriptのテストでも使ってるから、同じ書き方が出来るのは嬉しいところ
参照
https://code.google.com/p/selenium/downloads/list
https://code.google.com/p/chromedriver/downloads/list