Capybara:如何测试页面的标题?

发布于 2024-10-19 09:16:51 字数 60 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

倾其所爱 2024-10-26 09:16:51

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

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

,您可以测试标题,例如:

expect(page).to have_title "my_title"

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

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

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

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/863 the following is also working with capybara 2.0:

expect(first('title').native.text).to eq "my title"
肤浅与狂妄 2024-10-26 09:16:51

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

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

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

page.should have_xpath("//title", :text => "My Title")
一江春梦 2024-10-26 09:16:51

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

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

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

find('title').native.text.should have_content("Status of your account::")
诗笺 2024-10-26 09:16:51

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

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

然后我可以使用:

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

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
九厘米的零° 2024-10-26 09:16:51

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

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

    另一个选项是

  2. expect(page).to have_css 'title', text: '标题文本',可见: false
    从 Capybara 2.1 开始,默认值为 Capybara.ignore_hidden_​​elements = true,并且由于 title 元素不可见,您需要使用选项 visible: false 来进行搜索以包含不可见的页面元素。

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

  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: false for the search to include non visible page elements.

叹倦 2024-10-26 09:16:51

使用 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

Testing the Title of each page can be done in a much easier way with 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
多像笑话 2024-10-26 09:16:51

您只需将 subject 设置为 page,然后编写对该页面的 title 方法的期望:

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

在上下文中:

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

You just need to set the subject to page and then write an expectation for the page's title method:

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
咋地 2024-10-26 09:16:51

<代码>
it { should have_selector "title", text: full_title("这里是你的标题") }


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文