使用Test Unit的方式組織測試腳本,只有使用這種架構(gòu),才能進(jìn)行assert
require "test/unit"
require "watir-webdriver"
class TC_myTest <Test::Unit::TestCase
def testcase1
$browser=selenium.Browser.new(chrome)
$browser.goto('http://10.32.148.243:8080/parkinglot/')
assert($browser.element(:text, 'floor Manage').click)
end
end
好有些常規(guī)的功能在所有的code之前,和后運(yùn)行
# 在所有case運(yùn)行之前進(jìn)行一些操作
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
$b.goto $site
end
# 關(guān)閉所有的窗口
def teardown
$b.close
if $headless
$headless.destroy
end
end
通過上面的代碼,會自動(dòng)運(yùn)行系統(tǒng)的default設(shè)定的瀏覽器,一直其設(shè)定的default的URL
當(dāng)然, 使用參數(shù)化的過程來選擇瀏覽器,也更加專業(yè):
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}