【问题标题】:WARN: Screenshot could not be saved. page.current_path is empty警告:无法保存屏幕截图。 page.current_path 为空
【发布时间】:2021-01-28 12:30:52
【问题描述】:

我正在使用 bundle exec rspec 运行测试,这给了我以下错误:

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

也出现此错误:

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. 

红宝石:2.7.2 导轨:6.0.3.4

经过以下测试,我得到了错误。

这是我要测试的测试: /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

【问题讨论】:

  • 这里没有足够的信息来猜测,只是页面似乎不包含您正在寻找的元素。将您的测试代码添加到问题中,您是否检查过page.html 以查看页面中的实际内容?
  • @ThomasWalpole 我添加了测试文件。你现在可以检查吗?

标签: ruby-on-rails rspec capybara rspec-rails ruby-on-rails-6


【解决方案1】:

您在尝试截屏时收到关于 page.current_path 不可用的警告可能是因为您的 RSpec after hooks 安装顺序错误(在进行截屏调用之前正在重置会话)。

至于失败的测试,真的很难准确地说出你在做什么,但有很多事情是潜在的问题

  1. 我假设元素 ID 被命名为 #task_&lt;task project order&gt; 如果不是,而是 #task_&lt;task id&gt;,那么您需要重新考虑如何进行测试

  2. tbody:nth-child(2) 查找作为第二个子元素的 tbody,而不是 tbody 元素的第二个子元素。我猜你想要tbody tr:nth-child(2) ...

  3. 在执行within 之前不需要找到元素,因为within 会找到元素(重试/等待)

  4. 必须在调用第二个visit 之前提交新订单的保存 - 希望您检查元素是否位于不同行中确实表明订单已保存。

应用我最终得到的那 3 个

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

如果修复选择器并不能修复您的测试(希望它应该同步浏览器,以便在订单实际更改之前不会发生第二次访问),那么您需要提出一些实际可见的东西表示新订单已保存,并在调用第二次访问之前等待。

如果使用相对较新的 Capybara 版本,您还可以使用基于位置的过滤器来实际验证我假设您正在寻找的内容(文本的垂直顺序发生变化) - 类似于

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)

【讨论】:

    【解决方案2】:

    我通过这样更改测试解决了错误:

      it "can re-order a task" 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")
        visit(project_path(project))
        within("#task_2") do
          expect(page).to have_selector(".name", text: "Use Telescope")
        end
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多