\$\begingroup\$
\$\endgroup\$
1
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
begin
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"
end
end
puts "Element #{name} disappeared or not present. OK."
rescue Exception => e
puts "Error: Could not wait for element #{name} to disappearDetails: #{e.inspect}"
end
end
jotadepicasjotadepicas
asked Jan 13, 2015 at 18:27
-
\$\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– jotadepicas2015年01月13日 19:03:50 +00:00Commented Jan 13, 2015 at 19:03
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
Some notes:
waitUntilDisappears
. In Ruby, always:wait_until_disappears
.if element != nil
->if element
.if element != nil
. I think find_element never returnsnil
, it raises exception if not found, so this is not needed.rescue Exception => e
. A rescue that covers a whole method can omit thebegin
.rescue Exception => e
. Rescuing fromException
is bad practice. Rescue fromStandardError
, which is the same asrescue => 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? }
end
answered Jan 15, 2015 at 10:20
-
\$\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– jotadepicas2015年01月15日 14:07:31 +00:00Commented Jan 15, 2015 at 14:07
-
\$\begingroup\$ How to make it work for iOS Mobile Devices Automation? I am using Appium. \$\endgroup\$paul– paul2020年04月01日 15:17:36 +00:00Commented Apr 1, 2020 at 15:17
Explore related questions
See similar questions with these tags.
lang-rb