Reputation: 25
I'm trying to have a function that handles exceptions for different elements in the system. The Begin / Rescue code works, but once it's placed within a function, the Rescue part doesn't work and the exceptions don't get caught.
This is the exception code that works:
//..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//..
This is the same code but now in a function. this doesn't work.
//..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")) //..
Any ideas as to what I'm doing wrong? Basically the objective is to have as many exception traps as possible without needing multiple Begin/Rescue blocks.
Thanks Justin for looking. It still doesn't reach the rescue block within the function. My updated code looks like this as per your suggestion:
//..
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") }
//..
This is the same code applied for google.ca with the field name deliberately set wrong to bring out the exception:
//..
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") }
//..
And here is the exception that gets thrown: //..
/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 ~ %
//..
Upvotes: 0
Views: 183
Reputation: 46846
The problem is that browser.select_list(:id => "P5500_P_NATIONALITY").select("Americans")
gets evaluated and the result of it sent as the parameter to the exception_check
function. In other words, the selection is outside the begin-rescue
block.
You'll need to change the method to support a block
that can can be evaluated within the begin-rescue
:
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
Then call the method using a block
instead of a parameter:
exception_check { browser.select_list(:id => "P5500_P_NATIONALITY").select("Americans") }
Upvotes: 0