gpt4 book ai didi

ruby-on-rails - 如何使用Capybara 2.0测试页面标题?

转载 作者:行者123 更新时间:2023-12-03 12:28:05 25 4
gpt4 key购买 nike

尝试使用以下命令测试该页面包含<title>My Title</title>:

# spec/features/reports_spec.rb
require 'spec_helper'

feature "Archive Management" do
subject { page }

describe "Index Page" do
before(:all) { 10.times { FactoryGirl.create(:randomreport) } }
after(:all) { Report.delete_all }

describe "when no search terms present" do
before { visit reports_path }

it { should have_selector('title', text: 'My Title') } # <= Fails w/Capybara 2.0
it { should have_selector('title') } # <= passes
it { should have_text('My Title') } # <= passes
it { should have_selector('h2', text: "Welcome") } # <= passes
end
end
end

错误信息:
 Failure/Error: it { should have_selector('title', text: base_title) }
Capybara::ExpectationNotMet:
expected to find css "title" with text "My Title" but there were no matches. Also found "", which matched the selector but not all filters.

我知道我正在忽略这痛苦的事实,但无法弄清楚这是什么吗? <title>标签不再被视为“选择器”吗?!要么...?!?

编辑(调试器信息):

如果我按照@shioyama的建议巧妙地进入调试器,则显然page.body包含 <title>My Title</title>。包含 <h2>Welcome to My Title Project</h2>并通过的同一page.body!

似乎在其中找到了 <title> ... </title>标记,但未找到 My Title。但是稍后会在 My Title和/或 <a href=\"/\" class=\"brand\">My Title</a>中的页面中找到 <h2>Welcome to The My Title Project</h2>:
(rdb:1) p page
#<Capybara::Session>
(rdb:1) p page.body
"<!DOCTYPE html>\n<html>\n<head>\n<title>My Title</title>\n
<meta content='research, report, technology' name='keywords'>\n<meta
content='Some Project' name='description'>\n<link href=\"/assets/application.css\"
...
</head>\n<body>\n<header class='navbar navbar-fixed-top
navbar-inverse'>\n<div class='navbar-inner'>\n<div class='container'>\n
<a href=\"/\" class=\"brand\">My Title</a>\n<div class='pull-right'>\n
<ul class='nav'>\n<li><a href=\"/about\">About</a></li>\n<li><a href=\"/help\">Help</a>
</li>\n</ul>\n<form accept-charset=\"UTF-8\" action=\"/reports\"
class=\"navbar-search\" method=\"get\"><div style=\"margin:0;padding:0;display:inline\">
<input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /></div>\n
<input class=\"search-query\" id=\"query\" name=\"query\"
placeholder=\"Search\" type=\"text\" />\n</form>\n\n</div>\n</div>\n</div>\n</header>\n\n
<div class='container'>\n<div class='hero-unit center'>\n<h1>My Title</h1>\n
<h2>Welcome to The My Title Project</h2>\n<p>Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
...

我还能在调试器中尝试什么以弄清为什么has_selector?('title',text:...)失败了?

那么,在Capybara 2.0中测试标题的正确方法是什么?

最佳答案

升级到Capybara 2.0时遇到了相同的问题,并通过使用 Capybara.string 创建以下自定义RSpec匹配器设法解决了这些问题:

spec / support / utilities.rb

RSpec::Matchers::define :have_title do |text|
match do |page|
Capybara.string(page.body).has_selector?('title', text: text)
end
end

现在,在 subject { page }的规范文件中,我可以使用:
it { should have_title("My Title") }
it { should_not have_title("My Title") }

顺便说一句,关于这个问题的对话对于让我获得这个答案非常有帮助,所以谢谢!

更新1:

如果您不想创建自定义RSpec匹配器,则感谢 this StackOverflow answer用户 dimuch的帮助,我现在知道您可以直接在 have_selector (别名为 page.source)上调用 page.body来测试不可见的DOM元素:
it "should show the correct title" do
page.source.should have_selector('title', text: 'My Title')
end

subject { page }所在的位置:
its(:source) { should have_selector('title', text: 'My Title') }

更新2:

从Capybara 2.1开始,内置了 have_title / has_title?匹配器,在此答案中无需自定义RSpec匹配器。此外,更新1中描述的 its(:source) { ... }测试似乎在Capybara 2.1下无法正常运行。我已经确认 have_title / has_title?可以正常工作,因此如果计划升级,最好使用以下语法:

subject { page }时:
it { should have_title("My Title") }
it { should_not have_title("My Title") }

expect / it块中使用 scenario语法时:
expect(page).to have_title("My Title")
expect(page).to_not have_title("My Title")

关于ruby-on-rails - 如何使用Capybara 2.0测试页面标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13573525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com