Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

ruby on rails - WARN: Screenshot could not be saved. page.current_path is empty

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:

1) adding a new task can re-order a task
     Failure/Error: expect(page).to have_selector(".name", text: "Take Notes")
       expected to find visible css ".name" with text "Take Notes" within #
<Capybara::Node::Element tag="tr" path="/HTML/BODY[1]/TABLE[1]/TBODY[1]/TR[2]"> but there were
 no matches. Also found "Use Telescope", which matched the selector but not all filters. 

Ruby: 2.7.2 Rails: 6.0.3.4

After the following test, I got the error.

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
question from:https://stackoverflow.com/questions/65937138/warn-screenshot-could-not-be-saved-page-current-path-is-empty

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The warning you're getting about page.current_path not available when attempting to screenshot is probably because your RSpec after hooks are installed in the wrong order (session is being reset before the screenshot call is made).

As for the failing test, it's really hard to tell exactly what you're doing but a number of things stand out as potential issues

  1. I'm assuming the element ids are named #task_<task project order> If it's not and instead it's #task_<task id> then you need to rethink how you're doing the test

  2. tbody:nth-child(2) finds the tbody that is a second child, not the second child of the tbody element. I'm guessing you wanted tbody tr:nth-child(2) ...

  3. There is no need to find the element before doing within since within will find the element (with retry/waiting)

  4. The saving of the new order has to be committed before you call the second visit - hopefully your check for the element being in a different row does indicate the order has been saved.

Applying those 3 I end up with

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

If fixing the selector there doesn't fix your test (hopefully it should sync the browser so the second visit doesn't occur until after the order has actually been changed), then you need to come up with something visible that actually indicates the new order has been saved and wait for that before calling the second visit.

If using a relatively recent version of Capybara you could also use the location based filters to actually verify what I assume you're looking for (that the vertical order of the text changes) - something like

notes_row = find('tr', text: 'Take Notes')
expect(page).to have_selector('tr', text: 'Use Telescope', above: notes_row)
notes_row.click_on('Up')
expect(page).to have_selector('tr', text: 'Use Telescope', below: notes_row)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...