Wednesday, November 4, 2009

Integrate Watir Rspec for long story testing and force test sequence.

WatirとRspecでテストケースなが~いテストをちょっと効率良く行う。
rspecでテストの順番を指定したい場合が多く、この方法は使えると。
レポートはhtmlで出力。


# spec/test_spec.rb
#! ruby -Ku
#!/usr/bin/env ruby
require 'spec_helper'

$KCODE = 'UTF8'

shared_examples_for "トップ画面にいることの確認" do
it "ログインしてくださいが表示されていること" do
@ie = Watir::IE.new
@ie.goto("#{$testDomain}/foo/index.aspx")
@ie.text.should include('ログインしてください')
end
end

shared_examples_for "ログインできることの確認" do
it "ログインできること、つまりログアウトの表示がされていること" do
@ie = Watir::IE.attach(:title,/.*/)
@ie.text_field(:id, 'account').set('foo')
@ie.text_field(:id, 'password').set('bar')
@ie.button(:xpath, '//td[2]/input').click
@ie.text.should include('ログアウト')
end
end

shared_examples_for "パスワード変更ができてログインできる事の確認" do
it "パスワード変更ができてログインできる事" do
@ie = Watir::IE.attach(:title,/.*/) #このattachで前のテストのIEインスタンスを使用することができる。んで続きが行える。
@ie.text_field(:id, 'CurrentPassword').set('bar')
@ie.text_field(:id,'NewPassword').set('woo')
@ie.text_field(:id,'ConfirmPassword').set('woo')
@ie.button(:name, 'Submit').click_no_wait
@autoit=WIN32OLE.new('AutoItX3.Control')
@autoit.WinWait('Windows Internet Explorer','',10).should == 1
# when dialog(confirm change pwd.), return 1.
@autoit.WinActive('Windows Internet Explorer')
@autoit.ControlClick('Windows Internet Explorer','','OK')
@ie.link(:text, 'ログアウト').click
@ie.text.should include('ログインしてください')
@ie.text_field(:id, 'account').set('foo')
@ie.text_field(:id, 'password').set('woo')
@ie.button(:xpath, '//td[2]/input').click
@ie.text.should include('ログアウト')
end
end

describe "OreOre site long story" do
it_should_behave_like "トップ画面にいることの確認"
it_should_behave_like "ログインできることの確認"
it_should_behave_like "パスワード変更ができてログインできる事の確認"
  # この順番に行われる。
  # describeでどんどんテストケースを追加すると、順番は思ったようにいかない。
# たぶん、test/unitと同じで名前のキャラコード順になる。(たぶん)
end


# spec/spec.opts
#! ruby -Ku
#!/usr/bin/env ruby
--format html:spec/doc/rspec_report.html
--loadby mtime
--colour
--reverse


# spec/spec_helper.rb
#! ruby -Ku
#!/usr/bin/env ruby
$LOAD_PATH.push File.expand_path('../../lib',__FILE__)

require 'rubygems'
require 'spec'
require 'watir'
require 'win32ole'
require 'redgreen'

WIN32OLE.codepage = WIN32OLE::CP_UTF8
#test target domain
$testDomain = 'http://foo.org'


# Rakefile.rb
require 'rake'
require 'spec/rake/spectask'

task :default => [:help]

desc "foo long Story Test"
task :spec do
Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList['spec/*_spec.rb']
t.spec_opts = ['--options', "spec/spec.opts"]
end
end

UTF8のサイトのテストです。

No comments:

Post a Comment