[英]WATIR. Can you use Begin > Rescue within a function / method?
我正在嘗試使用一個函數來處理系統中不同元素的異常。 Begin / Rescue 代碼可以工作,但是一旦將其放入函數中,Rescue 部分就不起作用,並且不會捕獲異常。
這是有效的異常代碼:
//..begin
browser.select_list(:id => "P5500_P_NATIONALITY").select("Americans")
rescue Watir::Wait::TimeoutError, Watir::Exception::UnknownObjectException, Watir::Exception::NoValueFoundException
print "Error handling caught an exception: Relationships Page. See log."
print "\n"
log_fileWriter("after_header","Relationship Page:",$exception_error_code,"2",test_id)
screenCapture(browser,"Add Relationship",test_id)
end//..
這是相同的代碼,但現在在一個函數中。 這不起作用。
//..def exception_check(element_to_check)
begin
element_to_check
rescue Watir::Wait::TimeoutError, Watir::Exception::UnknownObjectException, Watir::Exception::NoValueFoundException
print "Error handling caught an exception: Relationships Page. See log."
print "\n"
log_fileWriter("after_header","Relationship Page:",$exception_error_code,"2",test_id)
screenCapture(browser,"Add Relationship",test_id)
end
end
exception_check(browser.select_list(:id => "P5500_P_NATIONALITY").select("Americans")) //..
關於我做錯了什么的任何想法? 基本上,目標是在不需要多個 Begin/Rescue 塊的情況下擁有盡可能多的異常陷阱。
問題是browser.select_list(:id => "P5500_P_NATIONALITY").select("Americans")
被評估並將其結果作為參數發送到exception_check
函數。 換句話說,選擇在begin-rescue
塊之外。
您需要更改方法以支持可以在begin-rescue
中評估的block
:
def exception_check
yield
rescue Watir::Wait::TimeoutError, Watir::Exception::UnknownObjectException, Watir::Exception::NoValueFoundException
print "Error handling caught an exception: Relationships Page. See log."
print "\n"
log_fileWriter("after_header","Relationship Page:",$exception_error_code,"2",test_id)
screenCapture(browser,"Add Relationship",test_id)
end
然后使用block
而不是參數調用該方法:
exception_check { browser.select_list(:id => "P5500_P_NATIONALITY").select("Americans") }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.