Ruby-on-rails Capybara:如何测试页面的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5129423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:31:36  来源:igfitidea点击:

Capybara: How to test the title of a page?

ruby-on-railsrspeccapybara

提问by Nerian

In a Rails 3 application using Steak, Capybara and RSpec how do I test the page's title?

在使用 Steak、Capybara 和 RSpec 的 Rails 3 应用程序中,如何测试页面的标题?

回答by Lars Schirrmeister

Since the version 2.1.0of capybara there are methods on the session to deal with the title. You have

2.1.0版本的水豚开始,会话中有处理标题的方法。你有

page.title
page.has_title? "my title"
page.has_no_title? "my not found title"

So you can test the title like:

所以你可以像这样测试标题:

expect(page).to have_title "my_title"

According to github.com/jnicklas/capybara/issues/863the following is also working with capybara 2.0:

根据github.com/jnicklas/capybara/issues/863,以下内容也适用于capybara 2.0

expect(first('title').native.text).to eq "my title"

回答by jpw

This works under Rails 3.1.10, Capybara 2.0.2 and Rspec 2.12, and allows matching partial contents:

这适用于 Rails 3.1.10、Capybara 2.0.2 和 Rspec 2.12,并允许匹配部分内容:

find('title').native.text.should have_content("Status of your account::")

回答by Dylan Markow

You should be able to search for the titleelement to make sure it contains the text you want:

您应该能够搜索该title元素以确保它包含您想要的文本:

page.should have_xpath("//title", :text => "My Title")

回答by Jake

I added this to my spec helper:

我将此添加到我的规范助手中:

class Capybara::Session
  def must_have_title(title="")
    find('title').native.text.must_have_content(title)
  end
end

Then I can just use:

然后我可以使用:

it 'should have the right title' do
  page.must_have_title('Expected Title')
end

回答by ChuckJHardy

Testing the Title of each page can be done in a much easier way with RSpec.

使用 RSpec 可以更轻松地测试每个页面的标题。

require 'spec_helper'

describe PagesController do
  render_views

  describe "GET 'home'" do
    before(:each) do
      get 'home'
      @base_title = "Ruby on Rails"
    end

    it "should have the correct title " do
      response.should have_selector("title",
                                :content => @base_title + " | Home")
    end
  end
end

回答by Aristotelis_Ch

In order to test for the title of a page with Rspec and Capybara 2.1 you could use

为了使用 Rspec 和 Capybara 2.1 测试页面的标题,您可以使用

  1. expect(page).to have_title 'Title text'

    another option is

  2. expect(page).to have_css 'title', text: 'Title text', visible: false
    Since Capybara 2.1 the default is Capybara.ignore_hidden_elements = true, and because the title element is invisible you need the option visible: falsefor the search to include non visible page elements.

  1. expect(page).to have_title 'Title text'

    另一种选择是

  2. expect(page).to have_css 'title', text: 'Title text', visible: false
    由于 Capybara 2.1 的默认值是Capybara.ignore_hidden_elements = true,并且由于标题元素是不可见的,因此您需要visible: false搜索选项以包含不可见的页面元素。

回答by Starkers

You just need to set the subjectto pageand then write an expectation for the page's titlemethod:

你只需要设置subjecttopage然后为页面的title方法写一个期望:

subject{ page }
its(:title){ should eq 'welcome to my website!' }

In context:

在上下文中:

require 'spec_helper'

describe 'static welcome pages' do
  subject { page }

  describe 'visit /welcome' do
    before { visit '/welcome' } 

    its(:title){ should eq 'welcome to my website!'}
  end
end

回答by Hengjie

it { should have_selector "title", text: full_title("Your title here") }

it { should have_selector "title", text: full_title("Your title here") }