我們喜歡在錯誤發(fā)生的時(shí)候有更詳細(xì)的信息,但是不可能一直盯著跑,那在錯誤發(fā)生時(shí),拍個screenshot,以便我們回頭來查看
time = Time.new
$b.driver.save_screenshot(File.dirname(__FILE__) + '/screenshots/' + @method_name + '_' + time.strftime('%Y%m%d_%H%M%S') + '.png');
實(shí)際上,我們可以用的assert語句還有 assert_true, assert_false和assert_equal,我們判斷返回值是否等于我們的期望值的時(shí)候,可以這么寫:
assert_equal 'Click Me', $b.text_field(:name, 'click1').value
我們在組織case的時(shí)候,好引入模塊化,或者層次化,這樣能搞好的整理我們的代碼,例如:
def form_register_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
對于一個form里的所有操作,我們都可以封裝到一個方法里,更加易讀。
對于time out,是個老大難問題,我們好能夠多處理一下,這里是個很好的實(shí)例:
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 browse_to_new_project
load_link(30){ $b.goto $site + "/designtourney/projects/new" }
end
def click_logo_design
load_link(30){ $b.link(:class, 'logo-design').click }
end
雖然默認(rèn)的log已經(jīng)足夠使用,但是可讀性不是很好,我們可以設(shè)置一個更加豐富的log文件來處理error:
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