Question

I am using selenium webdriver and trying to get a page object to look at a page multiple times before it reports a test failure. My problem is that a am getting an error that is not being caught by my begin rescue block.
here is the error

Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"id","selector":"does not exist"}

class ManageMembers < Nav

  def initialize
    super
    ready = false # Used to indicate if all elements expected are present 
    tryCount = 0# A counter to track the number of attempts to 
    while(ready == false)
      puts "initalizing managemembers"
      #things on the side menue 
      begin
        $driver.find_element(:id, "does not exist")
        $driver.find_element(:id, "sidebar")
        $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
        $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
        @sidebar = true
      rescue @sidebar = false
      end
Was it helpful?

Solution

Adding this as an answer so we can mark this question as answered.

class ManageMembers < Nav
  def initialize
    super
    ready = false # Used to indicate if all elements expected are present 
    tryCount = 0# A counter to track the number of attempts to 
    while(ready == false)
      puts "initalizing managemembers"
      #things on the side menue 
      begin
        $driver.find_element(:id, "does not exist")
        $driver.find_element(:id, "sidebar")
        $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
        $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
        @sidebar = true
      rescue Exception => e 
        @sidebar = false
      end

OTHER TIPS

When you use the block syntax (begin ... rescue ... end) instead of the inline syntax (do_something rescue recover_from_something) you need to place the rescue statements on the next line. Right now you are mixing up both syntax styles.

begin
  $driver.find_element(:id, "does not exist")
  $driver.find_element(:id, "sidebar")
  $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
  $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
  @sidebar = true
rescue
  @sidebar = false
end

This doesn't apply if you rescue a begin ... end block in its entirety (making it a complete statement), but that looks awful and I'd advise to use the complete block syntax:

begin
  $driver.find_element(:id, "does not exist")
  $driver.find_element(:id, "sidebar")
  $driver.find_element(:id, "sidebar").find_element(:link, "Manage Members")
  $driver.find_element(:id, "sidebar").find_element(:link, "Create Member")
  @sidebar = true
end rescue @sidebar = false

Again, don't do this last bit ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top