I'm a Java developer, I'm new to Ruby and I'm worried that I'm forcing or not-so-well using the goodness of the Ruby syntax.

  • What do you think about the Exception catching and how to print it in error messages?
  • What about the code block (closure?) I'm passing to the "until" method?
  • Is it ok to implicitly rely on what the last executed sentence of the block will be, regarding what would be its returned value?
def waitUntilDisappears(type, name) #Waits for a particular element to disappear
    puts "Waiting for element #{name} to disappear..."
    wait = Selenium::WebDriver::Wait.new(:timeout => 5)
    wait.until do
      element = driver.find_element(type, name)
      if element != nil
        displayValue = element.css_value("display")
        puts "Element #{name} has displayValue #{displayValue}."
        displayValue != "block"
    puts "Element #{name} disappeared or not present. OK."
  rescue Exception => e
    puts "Error: Could not wait for element #{name} to disappearDetails: #{e.inspect}"
  • \$\begingroup\$ Thanks for the edit, I added "Ruby" to the title because Selenium here is used via Ruby (it could be other languages like Java, etc, but this case is the use of Ruby i'm interested in reviewing). \$\endgroup\$ – jotadepicas Jan 13 '15 at 19:03

Some notes:

  • waitUntilDisappears. In Ruby, always: wait_until_disappears.
  • if element != nil -> if element.
  • if element != nil. I think find_element never returns nil, it raises exception if not found, so this is not needed.
  • rescue Exception => e. A rescue that covers a whole method can omit the begin.
  • rescue Exception => e. Rescuing from Exception is bad practice. Rescue from StandardError, which is the same as rescue => e.
  • puts "Error". It's bad practice to catch an exception (even worse ifit's all exceptions), just print to the screen and return as if nothing happened. Raise an exception or return a value that signals the error.
  • Is Element#visible? not enough?

I'd write:

def wait_until_disappears(type, name, timeout: 5)
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { !driver.find_element(type, name).visible? }
  • \$\begingroup\$ Hey, thanks! Regarding Element#visible it works different on different browsers, I've seen that most people check manually for the "display" attribute. About the "catch & log" of the exceptions, I'm currently refactoring this testing library, It would probably become a "catch, log and throw", so I can add some context information to the error. Thanks again! \$\endgroup\$ – jotadepicas Jan 15 '15 at 14:07
  • \$\begingroup\$ How to make it work for iOS Mobile Devices Automation? I am using Appium. \$\endgroup\$ – paul Apr 1 '20 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.