简体   繁体   English

如何使用ruby和watir以正确的方式使用重试和救援异常?

[英]how to use retry and rescue exception using ruby and watir in right way?

begin
  ie.select_list(:id, "PageSize").select_value("300")
rescue Watir::Exception::UnknownObjectException  
  aftererrorObj.errorMain(ie,con,country)
  retry
rescue Timeout::Error
  aftererrorObj.errorMain(ie,con,country)
  retry 
end

my ruby code is handling exception in this way.Is this right way to handle exception? 我的ruby代码以这种方式处理异常。这是处理异常的正确方法吗? will it work? 它会起作用吗?

If you're just looking to rescue the script in case the object has not loaded yet due to AJAX or slow response time, something like this might be more appropriate: 如果由于AJAX或响应时间较慢而导致对象尚未加载,您只是想要拯救脚本,这样的事情可能更合适:

Watir::Wait.until(60) { ie.select_list(:id, "PageSize").exists? }

OR 要么

ie.select_list(:id, "PageSize").when_present.select_value("300)

If just want to catch multiple types of exceptions in the same rescue, combine them as comma separated list: 如果只想在同一个救援中捕获多种类型的异常,请将它们组合为逗号分隔列表:

begin
  ie.select_list(:id, "PageSize").select_value("300")
rescue Watir::Exception::UnknownObjectException, Timeout::Error
  aftererrorObj.errorMain(ie,con,country)
  retry
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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