Watir

WEBアプリ開発に便利な機能&負荷テストツール集 から Rubyのブラウザ自動化ライブラリ。すごいなぁ。

インストー

gem update --system
gem install watir

サンプル

watir.rb という名前で以下を作成。(これだと駄目)

#-------------------------------------------------------------#
# Demo test for the Watir controller.
#
# Simple Google test written by Jonathan Kohl 10/10/04.
# Purpose: to demonstrate the following Watir functionality:
#   * entering text into a text field,
#   * clicking a button,
#   * checking to see if a page contains text.
# Test will search Google for the "pickaxe" Ruby book.
#-------------------------------------------------------------#

require "rubygems"
# the Watir controller
require "watir"

# set a variable
test_site = "http://www.google.com"

# open the IE browser
ie = Watir::IE.new

# print some comments
puts "Beginning of test: Google search."

puts " Step 1: go to the test site: " + test_site
ie.goto test_site

puts " Step 2: enter 'pickaxe' in the search text field."
ie.text_field(:name, "q").set "pickaxe" # "q" is the name of the search field

puts " Step 3: click the 'Google Search' button."
ie.button(:name, "btnG").click # "btnG" is the name of the Search button

puts " Expected Result:"
puts "  A Google page with results should be shown. 'Programming Ruby' should be high on the list."

puts " Actual Result:"
if ie.text.include? "Wikipedia"
  puts "  Test Passed. Found the test string: 'Wikipedia'. Actual Results match Expected Results."
else
  puts "  Test Failed! Could not find: 'Wikipedia'."
end

puts "End of test: Google search."

サンプル実行

$ ruby watir.rb
./watir.rb:21: uninitialized constant Watir (NameError)
        from D:/OLS/Program/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from D:/OLS/Program/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from watir.rb:15

エラー発生。watir.rb という名前を付けたのがいけなかった。watir_example.rb などとして再度実行。

$ ruby watir_example.rb
Beginning of test: Google search.
 Step 1: go to the test site: http://www.google.com
 Step 2: enter 'pickaxe' in the search text field.
 Step 3: click the 'Google Search' button.
 Expected Result:
  A Google page with results should be shown. 'Programming Ruby' should be high
on the list.
 Actual Result:
  Test Passed. Found the test string: 'Wikipedia'. Actual Results match Expected Results.
End of test: Google search.

実行してから少し時間がかかるけど、IE が起動して Google で検索。Wikipedia という文字列が検索結果を表示している内容に含まれていたのでテスト成功。

TestLinkClient と TestLinkWatir と CI ツールで Web ブラウザのテストも自動的にできちゃうのかな。