Showing posts with label rspec. Show all posts
Showing posts with label rspec. Show all posts

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のサイトのテストです。

Wednesday, October 28, 2009

Cucumber Watir Rspec Mocha, How to add helper module to cucumber.

Cucumber with Watir



# features/support/env.rb
require 'spec/expectations'

if ENV['FIREWATIR']
require 'firewatir'
Browser = FireWatir::Firefox
else
case RUBY_PLATFORM
when /darwin/
require 'safariwatir'
Browser = Watir::Safari
when /win32|mingw/
require 'watir'
Browser = Watir::IE
when /java/
require 'celerity'
Browser = Celerity::Browser
else
raise "This platform is not supported (#{PLATFORM})"
end
end

# "before all"
browser = Browser.new

Before do
@browser = browser
end

# "after all"
at_exit do
browser.close
end


How to add helper modlue



# features/support/foo_helper.rb
module Foo
def zip_code(pref_name)
if pref_name == 'local'
"123-4567"
else
"Pls. ask postman"
end
end
end
World(Foo)


mocha integration.



# features/support/mocha_helper.rb
require "mocha"

World(Mocha::API)

Before do
mocha_setup
end

After do
begin
mocha_verify
ensure
mocha_teardown
end
end

日本語のFeatureの場合、win32 コンソールではひどいが、htmlに結果を出力すればOK。
例えば、

# config/cucumber.yml
default: --format profile features
html_report: --format progress --format html --out=features_report.html features

で、
C:\あ>cucumber -l ja --profile html_report features/send_tera_mail.feature
で実行すれば、ブラウザでは文字化けしない。
他のサイトでもいろいろ試しているようだけど、コンソールではきびしい。