繁体   English   中英

瓦提尔。 您可以在函数/方法中使用 Begin > Rescue 吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM