Google App engineのPythonで作ったツールのテストをどうしようかなと思っていたら、業務でつかっているSeleniumがぴったりだと気づいた。
しかし、毎回ログインするのは面倒だったり、テストケースを日本語で表示したかったりということで、RSpec上にSeleniumを使った試験をWindows上に構築。
おまけに画面キャプチャもテスト中のプログラムからも行えるようにしてみた。
複雑そうに見えるこの作業も今や本当に簡単になったもんだと感動。
手順
1.下記のリンクからSelenium RCのダウンロードし、適当なディレクトリに展開
http://seleniumhq.org/download/
2.RSpecのインストール
gem install rspec
3.SeleniumのRuby用アダプタ
gem install selenium-client
4.コンソールでRSpecの試験結果の色を表示するため。
gem install win32console
5.FireFoxのアドオンScreengrabを下記URLからインストール
https://addons.mozilla.org/ja/firefox/addon/1146
6.下記の内容のRakefileを適当なディレクトリに作成
# cofing: utf-8
require 'rubygems'
require 'spec/rake/spectask'
task :default => [:spec]
Spec::Rake::SpecTask.new do |t|
t.warning = true
t.rcov = false
t.spec_opts << '--color' << '-fs'
end
7.6.で作ったRakefileのディレクトリの配下にspecという名前のディレクトリの作成
8.7.で作ったディレクトリに下記の内容のselenium_helper.rbを作成
require 'date'
module SeleniumHelperMethods
def startup
@captures = {}
i=
1
date_str=
Date.today.to_s.gsub(
"-",
"")
while File.exist?(
File.dirname(
__FILE__) +
"/capture/" + date_str +
"/" + i.to_s)
do
i+=
1
end
@dir_name =
File.dirname(
__FILE__) +
"/capture/" + date_str +
"/" + i.to_s
FileUtils.mkdir_p(
@dir_name)
@selenium_driver =
Selenium::
Client::
Driver.new(
:host =>
"localhost",
:port =>
4444,
:browser =>
"*firefox",
:url =>
"http://localhost:8080",
:timeout_in_second =>
80,
:javascript_framework =>
:prototype)
@selenium_driver.start_new_browser_session
@selenium_driver.open
"/"
@selenium_driver.type
"email",
"test@example.com"
@selenium_driver.click
"//input[@value='Login']",
:wait_for =>
:page
end
def capture
@captures[
self.description] =
0 unless @captures[
self.description]
@captures[
self.description]+=
1
capture_name =
NKF.nkf(
"-Sw",
self.description) +
"_" +
@captures[
self.description].to_s +
".png"
@selenium_driver.capture_entire_page_screenshot(
@dir_name +
"/" + capture_name,
"");
end
end
Selenium::Client::Driver.newの内容は各自のアプリケーションに合わせて適宜修正してください。ここでは、GAEのローカルのテストを想定しています。
9.7.で作ったディレクトリ配下にRSpecのテストを適当_spec.rbの名前でutf-8の文字コードで作成(下記は私のアプリの例)
#!/usr/bin/env ruby
#
# Sample Ruby script using the Selenium client API
#
$:.unshift File.expand_path(File.dirname(__FILE__))
require "rubygems"
require "spec"
require "nkf"
require "selenium/client"
require "selenium/rspec/spec_helper"
require "selenium_helper"
describe NKF.nkf("-Ws",%q|帳簿登録|) do
attr_reader :selenium_driver
alias :page :selenium_driver
include SeleniumHelperMethods
before(:all) do
startup
end
after(:each) do
capture
end
after(:all) do
@selenium_driver.stop
end
it NKF.nkf("-Ws",%q|登録ボタンで一覧に表示|) do
page.type 'category','food'
page.type 'account_price','200'
page.type 'account_content','パン'
page.click '//input[@value="登録"]', :wait_for => :ajax
page.table_cell_text('//table[@class="account_list"].1.1').should == '200円'
end
end
上記のNKF.nkf("-Ws",%q|帳簿登録|)のくだりは、rspec実行時にコマンドプロンプト上ではUTF-8は文字化けしてしまうため、sjisに変換している。
またcaptureメソッド呼び出しにより、その時点の画面のキャプチャがitに指定した文言+連番で取得できる。一応各テスト終了ごとに自動的に画面キャプチャを呼び出すようにしている。
10.1.で展開したディレクトリの配下のselenium-server-1.0に移動し、下記のコマンド実行により、Selenium RCサーバの起動
java -jar selenium-server.jar
11. 6.で作ったRakefileがあるディレクトリに移動し、下記のコマンドでRSpecの実行
以上
うまくいけば、下記のような快適な環境ができる。
コマンドプロンプト
D:\test>rake spec
(in D:/test)
帳簿登録
- 登録ボタンで一覧に表示
Finished in 15.891 second
1 example, 0 failures
登録ボタンで一覧に表示_1.png
登録ボタンで一覧に表示_2.png
最近のコメント