試圖測試頁面包含<title>My Title</title>
有:如何使用Capybara 2.0測試頁面標題?
# 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>
標籤不再被認爲是「選擇器」?!?要麼...?!?
編輯器(Debugger信息):
如果我下拉到如此巧妙的@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=\"✓\" /></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.
...
我可以嘗試在調試器還有什麼弄清楚爲什麼have_selector(「標題」,文:......)?是失敗?
那麼,什麼是測試水豚2.0中的標題的正確方法?
如果你嘗試'應該have_xpath(「//標題[包含( '#{base_title}') ]「)'? –
@shioyama - 這工作,並通過!我仍然有興趣瞭解爲什麼'have_selector'對我來說不是*工作,但是... – Meltemi
嗯......這個xpath正在測試'