我们喜欢在错误发生的时候有更详细的信息,但是不可能一直盯着跑,那在错误发生时,拍个screenshot,以便我们回头来查看
time = Time.new
$b.driver.save_screenshot(File.dirname(__FILE__) + '/screenshots/' + @method_name + '_' + time.strftime('%Y%m%d_%H%M%S') + '.png');
实际上,我们可以用的assert语句还有 assert_true, assert_false和assert_equal,我们判断返回值是否等于我们的期望值的时候,可以这么写:
assert_equal 'Click Me', $b.text_field(:name, 'click1').value
我们在组织case的时候,好引入模块化,或者层次化,这样能搞好的整理我们的代码,例如:
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,是个老大难问题,我们好能够多处理一下,这里是个很好的实例:
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
虽然默认的log已经足够使用,但是可读性不是很好,我们可以设置一个更加丰富的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