我试图有一个处理系统中不同元素异常的函数。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块。
谢谢Justin找我。它仍然没有到达函数中的救援块。根据你的建议,我更新的代码看起来像这样:
//..
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
exception_check { browser.select_list(:id => "P5500_P_NATIONALITY").select("Americans") }
//..
这是应用于google的相同代码。Ca与字段名故意设置错误以引出异常:
//. .
test_env = "https://www.google.ca/"
browser = Watir::Browser.new # Defaults to use Chrome
browser.goto(test_env)
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
exception_check { browser.text_field(:name => "qq").set("watir") }
//. .
下面是被抛出的异常://. .
/opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:813:in `rescue in element_call': timed out after 30 seconds, waiting for #<Watir::TextField: located: false; {:name=>"qq", :tag_name=>"input"}> to be located (Watir::Exception::UnknownObjectException)
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:805:in `element_call'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/user_editable.rb:10:in `set'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:26:in `block in <main>'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:16:in `exception_check'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:26:in `<main>'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:680:in `rescue in wait_for_exists': timed out after 30 seconds, waiting for #<Watir::TextField: located: false; {:name=>"qq", :tag_name=>"input"}> to be located (Watir::Exception::UnknownObjectException)
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:675:in `wait_for_exists'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:701:in `wait_for_enabled'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:713:in `wait_for_writable'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:838:in `check_condition'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:802:in `element_call'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/user_editable.rb:10:in `set'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:26:in `block in <main>'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:16:in `exception_check'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:26:in `<main>'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/wait.rb:46:in `until': timed out after 30 seconds, waiting for true condition on #<Watir::TextField: located: false; {:name=>"qq", :tag_name=>"input"}> (Watir::Wait::TimeoutError)
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/wait.rb:125:in `wait_until'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:677:in `wait_for_exists'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:701:in `wait_for_enabled'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:713:in `wait_for_writable'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:838:in `check_condition'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/elements/element.rb:802:in `element_call'
from /opt/homebrew/lib/ruby/gems/3.0.0/gems/watir-6.19.1/lib/watir/user_editable.rb:10:in `set'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:26:in `block in <main>'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:16:in `exception_check'
from /Users/bobjames/Desktop/Automated-Testing-main/send_email.rb:26:in `<main>'
bobjames@Bobs-Air ~ %
//. .
问题是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") }