mattheworiordan / capybara-screenshot

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

WARN: Screenshot could not be saved. page.current_path is empty #277

Open MunimIftikhar opened 3 years ago

MunimIftikhar commented 3 years ago

I'm running the test using bundle exec rspec, which gives me the error of: WARN: Screenshot could not be saved. page.current_path is empty. also getting this error: Failure/Error: expect(page).to have_selector(".name", text: "Take Notes"). Here's the test I'm trying to test: /spec/system/add_task_spec.rb

require "rails_helper"

RSpec.describe "adding a new task" do
  let!(:project) { create(:project, name: "Project Bluebook") }
  let!(:task_1) { create(
    :task, project: project, title: "Search Sky", size: 1, project_order: 1) }
  let!(:task_2) { create(
    :task, project: project, title: "Use Telescope", size: 1,
           project_order: 2) }
  let!(:task_3) { create(
    :task, project: project, title: "Take Notes", size: 1,
           project_order: 3) }

  it "can re-order a task", :js do
    visit(project_path(project))
    find("#task_3")
    within("#task_3") do
      click_on("Up")
    end
    expect(page).to have_selector(
      "tbody:nth-child(2) .name", text: "Take Notes")
      #END:P1
      #START:P2
    visit(project_path(project))
    find("#task_2")
    within("#task_2") do
      expect(page).to have_selector(".name", text: "Take Notes")
    end
  end

end
christianhellsten commented 3 years ago

"WARN: Screenshot could not be saved. page.current_path is empty" is an indication that the browser has yet to load a page. Are you sure the page has loaded?

westonganger commented 3 years ago

I was also getting this warning. I noticed that the screenshots are actually getting saved despite what the error says.

To work around this I have added the following monkey patch.

Capybara::Screenshot::Saver.class_eval do
  def save
    current_path do |path|
      if path.empty?
        next ### CUSTOM
        #warn 'WARN: Screenshot could not be saved. `page.current_path` is empty.'
      else
        begin
          save_html if @html_save
        rescue StandardError => e
          warn "WARN: HTML source could not be saved. An exception is raised: #{e.inspect}."
        end

        begin
          save_screenshot
        rescue StandardError => e
          warn "WARN: Screenshot could not be saved. An exception is raised: #{e.inspect}."
        end
      end
    end
  end
end

I think that likely an official fix is due here.

dacook commented 2 years ago

This is a continuation of this thread: https://github.com/mattheworiordan/capybara-screenshot/issues/243

dacook commented 2 years ago

It looks like this warning occurs due to multiple problems. In my case, it is not saving the screenshots as @westonganger suggests above, so that fix doesn't help me.

GalenkoEugene's comment helped me discover that my problem was caused by Capybara.reset_sessions! in an rspec after hook, but that fix doesn't apply as I'm not using the retry gem (maybe I should..). I guess the browser 'window' is closed before this gem has a chance to take the screenshot.

I found in my case reset_sessions was intended to clear cookies, so I've replaced it with a more direct method to do that (thanks makandra) and it seems to be working! For those looking for code to try out:

  # Delete cookies after all responses have completed
  config.after(:each, js: true) do
    RackRequestBlocker.wait_for_requests_complete
    # Capybara.reset_sessions! # <- that was too extreme, let's try a gentler method:
    Capybara.current_session.driver.browser.manage.delete_all_cookies # Delete cookies with Selenium
  end

So it seems this is often due to conflicting configuration. I don't know enough to be able to suggest how it could be generally resolved.