Getting end of file reached (EOFError) using ruby webdriver - ruby

Some of my tests pass one time then fail with "end of file reached(EOFError)". Can't figure out what causes this consistency issue. Sometimes it fails when filling out a form. Other times it fails when clicking a button.
Using the following:
OSX 10.9.3
Watir-webdriver 0.6.10
Ruby 1.9.3
Chrome 35.0
Chromedriver ChromeDriver v2.10

Not sure what the issue is but a simple work around to get rid of this error is to use a Begin/Rescue statement around the code that is causing this error (check what line number the terminal output says is causing the error).
For example:
browser.close #This is the line giving the EOFError
Do the following:
begin
browser.close #if there is an error: jump to the rescue statement
rescue
#don't put any code in the rescue statement (ignore the error)
end
#rest of code
The way the begin/rescue statement works is if the code in the begin statement causes an error, it will run the code in the rescue statement. In this case, since there isn't any code in the rescue statement, it will merely ignore the error and continue with the rest of the code.

Related

Escape Mechinze Error in Ruby Scraping

I am getting the following server response error while trying to scrape SERP results:
/Users/*********/.rvm/gems/ruby-2.3.0/gems/mechanize-2.7.5/lib/mechanize/http/agent.rb:323:in `fetch': 503 => Net::HTTPServiceUnavailable for http://******.*****.com/sorry/index?continue=http://www.********.com/search%3Fq%3D<term1>%2B<term2> -- unhandled response (Mechanize::ResponseCodeError)
I am trying to figure out how to escape the error / exception, so that the program will continue to run instead of automatically exiting.
Like anything in Ruby it probably boils down to rescue and recover:
loop do
begin
Mechanize.do_stuff!
# Success!
break
rescue Mechanize::ResponseCodeError
# Server-side failure, so let's try again after a quick break
sleep(10)
end
end
Note the sleep(10) is there to avoid slamming the server furiously and making it malfunction even harder.

Ruby closing program window on Windows 7

I've download Ruby using rubyinstaller. Apps closed itself after finishing executing, so i put gets at the end of it - it works, but if there is an error it closes, and I have problem with debugging. How can i prevent this?
Probably the best way is to run your Ruby program from console (command line).
An alternative hack is to wrap your main program in an exception handler:
begin
... your code here ...
rescue => exc
puts "#{exc.class}: #{exc}" # write exception message
puts exc.backtrace.join "\n" # write backtrace
gets # wait for Return
end

Ruby lint error what is the right way to write this with a guard clause

Trying to run a command, if success continue on, if failure raise error and send commands output to console.
output = `#{command}`
unless $CHILD_STATUS.success?
raise "#{command} failed with:\n#{output}"
end
C: Use a guard clause instead of wrapping the code inside a conditional expression.
the code functions correctly but, rubocop doesn't like it. What would be the best way to improve the style of this code and still give me the same functionality?
try this
raise "#{command} failed with:\n#{output}" unless $CHILD_STATUS.success?

Using single line conditional with require/rescue

I want to avoid an error, if a require is not successfull.
I can do it with:
begin
require 'unexisting_script'
rescue LoadError
end
I tried to do the same with a one-line condition:
require 'unexisting_script' rescue LoadError
and get the error no such file to load -- unexisting_script (LoadError)
With other exceptions/commands I have no problem with a one line rescue, this works:
1 / 0 rescue ZeroDivisionError
I also tried to bracket the command, but withous success:
(require 'unexisting_script') rescue LoadError
I can put everything in one line with ;:
begin require 'unexisting_script'; rescue LoadError; end
but I'm still wondering, why the shortest version does not work.
I found some related questions, but none of them is mentioning a problem with require and rescue:
Why does this rescue syntax work?
How does one use rescue in Ruby without the begin and end block
My question:
Can I use rescue in a one-line condition with require? If yes: how? If no: Why?
You cannot specify the error class when you use rescue in postfix/oneliner notation. What rescue LoadError or rescue ZeroDivisionError means is that it will rescue a (subclass of) StandardError, and in such case, evaluate LoadError or ZeroDivisionError, which has no effect. Since ZeroDivisionError is a subclass of StandardError, it was captured, but LoadError is not, and it was not captured.
By the way, I cannot think of a use case where you want to not raise an error when a required file does not exist. Required files are dependencies, and if requiring them fails, then the program will not work correctly anyway. I feel a code smell in what you are doing. When failure of loading a file does not mess the program, that is usually when you should be using load instead of require.

Equivalent to Perl's END block in Ruby

Is there a Perl equivalent END block in Ruby? In Perl, if I specify an END block, the code in that block will get executed no matter where the program bails out. It is great functionality for closing open file handles. Does Ruby support similar functionality? I tried Ruby's "END{}" block but that doesnt seem to get called if I had an exit in the code due to an error.
Thanks!
Use at_exit, which will run regardless of whether an exception was raised or not:
at_exit { puts 'exited!' }
raise
prints "exited" as expected.
You should only consider this if you cannot use an ensure, as at_exit causes logic to reside far away from where the actual exit occurs.
Yes. A block may have an 'ensure' clause. Here's an example:
begin
# This will cause a divide by zero exception
puts 3 / 0
rescue Exception => e
puts "An error occurred: #{e}"
ensure
puts "I get run anyway"
end
Running this produces:
An error occurred: divided by 0
I get run anyway

Resources