如何使用Capybara 2.0测试页面标题? [英] How can I test the page title with Capybara 2.0?

查看:60
本文介绍了如何使用Capybara 2.0测试页面标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试测试该页面包含< title>我的标题< / title> 并带有:

Trying to test that page contains <title>My Title</title> with:

# 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> 标签不再被视为选择器吗?!要么...?!?

I know I'm overlooking the painfully obvious but can't figure out what it is? Are <title> tags no longer considered 'selectors'?!? Or...?!?

编辑(调试器信息)

如果我进入调试器正如@shioyama巧妙建议的那样,很明显page.body包含< title>我的标题< / title> 。包含< h2>欢迎访问我的标题项目< / h2> 的同一page.body并通过!

If I drop down into the debugger as cleverly suggested by @shioyama it is clear that the page.body contains <title>My Title</title>. The same page.body that contains <h2>Welcome to My Title Project</h2> and passes!

似乎找到了< title> ... < / title> ; 标记,但其中没有 My Title 标记。但是它确实会在页面< a href = \ / \ class = \ brand\内的页面中找到我的标题 >我的标题< / a> 和/或< h2>欢迎来到我的标题项目< / h2>

It appears to find the <title>...</title> tag but not My Title within it. But it does find My Title later in the page within <a href=\"/\" class=\"brand\">My Title</a> and/or in <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. 
...

我还可以在调试器中尝试什么以找出为什么have_selector? ('title',text:...)失败了?

What else could I try in the debugger to figure out why have_selector?('title', text: ...) is failing?

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

推荐答案

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

I had the same issues when I upgraded to Capybara 2.0, and managed to solve them by creating the following custom RSpec matcher using Capybara.string:

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

现在,在主题{页面} 的规范文件中,我可以使用:

Now, in a spec file where subject { page }, I can use:

it { should have_title("My Title") }
it { should_not have_title("My Title") }

顺便说一句,关于这个问题的讨论对我找到答案非常有帮助,所以谢谢!

As an aside, the conversation on this question was extremely helpful in getting me to this answer, so thank you!

如果您不想创建自定义RSpec匹配器,请使用用户 dimuch 给出的StackOverflow答案,我现在知道您可以直接在have_selector nofollow noreferrer> page.source (别名为 page.body )来测试是否不可见e DOM元素:

If you don't want to create a custom RSpec matcher, thanks to this StackOverflow answer by user dimuch I now know that you can call have_selector directly on page.source (aliased page.body) to test for non-visible DOM elements:

it "should show the correct title" do
  page.source.should have_selector('title', text: 'My Title')
end

或其中主题{页面}

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?可以正常工作,所以如果可能,最好使用以下语法您计划升级:

Update 2:

From Capybara 2.1, there are built-in have_title/has_title? matchers, negating the need for the custom RSpec matcher in this answer. Furthermore, the its(:source) { ... } tests described in Update 1 seem to break under Capybara 2.1; I've confirmed the have_title/has_title? work as expected, so it's probably best to go with the following syntax if you plan on upgrading:

主题{页面}

it { should have_title("My Title") }
it { should_not have_title("My Title") }

内使用 expect 语法时 / 场景块:

expect(page).to have_title("My Title")
expect(page).to_not have_title("My Title")

这篇关于如何使用Capybara 2.0测试页面标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆