mattheworiordan / capybara-screenshot

Automatically save screen shots when a Capybara scenario fails
MIT License
1.02k stars 168 forks source link

No screenshot on "Timed out waiting for Selenium session reset" error #258

Open lnsight opened 5 years ago

lnsight commented 5 years ago

I had to patch Capybara::Selenium::Driver#wait_for_empty_page for that.

mattheworiordan commented 5 years ago

Please can you write a test to reproduce this issue, or better yet, create a PR.

y-yagi commented 5 years ago

We recently encount same error but can't reproduce manually... @lnsight Can you share your patch?

lnsight commented 5 years ago

Patch for capybara 3.15.0:

module CapybaraShotOnTimedOutSessionReset
  def wait_for_empty_page(timer)
    until find_xpath('/html/body/*').empty?
      if timer.expired?
        timestamp = Time.current.strftime('%y-%m-%d-%H-%M-%S')
        file_path = File.join(Capybara.save_path, "timed_out_session_reset_#{timestamp}.png")
        save_screenshot(file_path) # rubocop:disable Lint/Debugger
      end

      raise Capybara::ExpectationNotMet, 'Timed out waiting for Selenium session reset' if timer.expired?

      sleep 0.01

      # It has been observed that it is possible that asynchronous JS code in
      # the application under test can navigate the browser away from about:blank
      # if the timing is just right. Ensure we are still at about:blank...
      @browser.navigate.to('about:blank') unless current_url == 'about:blank'
    end
  end
end

Capybara::Selenium::Driver.prepend(CapybaraShotOnTimedOutSessionReset) if ENV['CAPYBARA_EXTRA_SHOT']
lnsight commented 5 years ago

Please can you write a test to reproduce this issue, or better yet, create a PR.

I'm not reproduced an issue yet because this error appears randomly. In our system it's occurs on ajax response after browser session cleanup (authorization popup prevents redirect to blank page).

y-yagi commented 5 years ago

@lnsight Thanks!