把上面所有的都整理起来,是一个你可以作为参考的良好实例:
require "rubygems"
gem "test-unit"
require "test/unit"
require "watir-webdriver"
# check arguments for browser or headless specification
ARGV.each { |arg|
if arg.downcase.include? 'chrome'
$browser = 'chrome'
elsif arg.downcase.include? 'firefox'
$browser = 'firefox'
elsif arg.downcase.include? 'ff'
$browser = 'firefox'
elsif arg.downcase.include? 'ie'
$browser = 'ie'
elsif arg.downcase.include? 'headless'
$headless = true
end}
module Test
module Unit
class TestSuite
alias :old_run :run
def run(result, &progress_block)
old_run(result, &progress_block)
File.open('errors.log', 'w'){|f|
result.faults.each{|err|
case err
when Test::Unit::Error, Test::Unit::Failure
f << err.test_name
f << "
"
#not in log file
when Test::Unit::Pending, Test::Unit::Notification, Test::Unit::Omission
end
}
}
end
end
end
end
class TestExample < Test::Unit::TestCase
# setup is run before every test
def setup
$browser = 'chrome' if $browser.nil?
$site = 'http://test.localhost' if $site.nil?
if $headless
require 'headless'
$headless = Headless.new
$headless.start
end
if $browser == 'chrome'
$b = Watir::Browser.new :chrome
elsif $browser == 'firefox'
$b = Watir::Browser.new :ff
elsif $browser == 'ie'
$b = Watir::Browser.new :ie
end
$timeout_length = 30
load_link($timeout_length){ $b.goto $site }
end
# teardown is run after every test
def teardown
# take screenshot of end of test, useful for failures/errors
time = Time.new
$b.driver.save_screenshot(File.dirname(__FILE__) + '/screenshots/' + @method_name + '_' + time.strftime('%Y%m%d_%H%M%S') + '.png');
$b.close
if $headless
$headless.destroy
end
end
def browse_to_new_project
load_link($timeout_length){ $b.goto $site + "/designtourney/projects/new" }
end
def click_logo_design
load_link($timeout_length){ $b.link(:class, 'logo-design').click }
end
def form_fill_first_page
$b.text_field(:name, 'organization_name').set('Magic/More Magic')
$b.text_field(:name, 'question_38').set('As mentioned above, we make magic and more magic.')
$b.text_field(:name, 'question_39').set('People who like magic and more magic, as opposed to less magic.')
$b.link(:id=> 'show-more').click
$b.text_field(:name, 'question_41').set('Im putting stuff into question 41')
$b.text_field(:name, 'question_45').set('Im putting stuff into question 45')
end
def first_page_asserts type = 'regular'
assert_equal 'Magic/More Magic', $b.text_field(:name, 'organization_name').value
assert_equal 'As mentioned above, we make magic and more magic.', $b.text_field(:name, 'question_38').value
assert_equal 'People who like magic and more magic, as opposed to less magic.', $b.text_field(:name,'question_39').value
assert_equal 'Im putting stuff into question 41', $b.text_field(:name,'question_41').value
end
def wait_for_ajax
$b.div(:id, 'ajax-loader').wait_while_present
end
def load_link(waittime)
begin
Timeout::timeout(waittime) do
yield
end
rescue Timeout::Error => e
puts "Page load timed out: #{e}"
retry
end
end
def test_save_for_later
browse_to_new_project
click_logo_design
form_fill_first_page
$b.link(:class, 'save').click
wait_for_ajax
assert_true $b.div(:id, 'fallr').visible?
browse_to_new_project
$b.div(:id, 'fallr').wait_until_present
$b.wait_until{ $b.execute_script('return $('#fallr-wrapper').is(':animated')') == false }
sleep 0.5
$b.link(:id, 'fallr-button-yes').click
$b.div(:id, 'fallr-overlay').wait_while_present
# These assertions make sure the stuff for the first page is still all there
first_page_asserts
end
end