0

Can someone please give me a example of how to use rspec-retry with the selenium-webdriver. I am trying to get it to reattempt the navigation when there is a Net::ReadTimeout error. I found an example here but I am new to Ruby and don't think I am using it right.

What I have tried.

require 'selenium-webdriver'
require 'rspec/retry'

Selenium::WebDriver::PhantomJS.path = 'phantomjs.exe'
driver = Selenium::WebDriver.for :phantomjs
driver.manage.timeouts.page_load = 300

driver.navigate.to "http://google.com"

RSpec.configure do |config|
  # show retry status in spec process
  config.verbose_retry = true
  # Try five times (retry 4 times)
  config.default_retry_count = 5
  # Only retry when Selenium raises Net::ReadTimeout
  config.exceptions_to_retry = [Net::ReadTimeout]
end

puts(driver.title)
driver.quit
4
  • What do you see in your test log when it fails? Are you sure it isn't retrying and failing repeatedly? Commented Feb 23, 2016 at 22:02
  • I don't know how to have it write to a test log so no I am not sure it is retrying and failing. Commented Feb 23, 2016 at 22:07
  • Maybe you should add more info to your question. Are you testing a Rails app with rspec-rails, or are you testing a remote application? Commented Feb 23, 2016 at 22:09
  • I am using pure Ruby and the code above. The only deference is that I am navigating to 100 or so URL's. The above code works 90% of the time but then randomly crashes with a Net::ReadTimeout error. I am simply trying to stop it from crashing on the error and have re-navigate to the URL. When I restart the code after a crash it seems to navigate just fine to the same URL. Commented Feb 23, 2016 at 22:16

1 Answer 1

5

It sounds like you may not be using rspec at all, but just want the retry behavior that is provided by the rspec-retry gem. If that's the case, you could just use ruby's rescue and retry keywords to achieve the same thing.

For example, the following code will retry the navigation 1 time if the navigation throws a Net::ReadTimeout

require 'selenium-webdriver'

Selenium::WebDriver::PhantomJS.path = 'phantomjs.exe'
driver = Selenium::WebDriver.for :phantomjs
driver.manage.timeouts.page_load = 300

attempts = 0  # has to be outside the begin/rescue to avoid infinite loop
begin
  driver.navigate.to "http://google.com"
rescue Net::ReadTimeout => e
  if attempts == 0
    attempts += 1
    retry
  else
    raise
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.