72

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

Nerian
  • 15,901
  • 13
  • 66
  • 96

8 Answers8

108

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"
Lars Schirrmeister
  • 2,215
  • 1
  • 22
  • 23
14

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::")
jpw
  • 18,697
  • 25
  • 111
  • 187
14

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")
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
  • I get a Failure/Error: page.should have_xpath("//title", "home") wrong number of arguments (3 for 2) – Nerian Feb 26 '11 at 22:50
  • And the code is: scenario "The user is logged in and can see his home page" do visit('/') page.should have_xpath("//title", "home") end – Nerian Feb 26 '11 at 22:51
  • Oops, forgot that the second parameter is supposed to be a hash... Check my revised answer. – Dylan Markow Feb 27 '11 at 01:34
  • I didn't really want to post this as an answer, though I feel it could be (I know it's not very ruby-ish). `page.should have_content('Your Title')` – Tass Aug 19 '11 at 16:07
  • I should further add that `page.should have_content()` and `page.has_content?()` seem to not work as beautifully as the xpath methods. – Tass Aug 24 '11 at 18:48
  • The message when the test fails is really weird. It'll say e.g. `expected xpath "//title" with text "Notifications" to return something` if the title doesn't match (in this case) `Notifications`. This is much less helpful than the normal "expected vs got" pattern. – Tyler Collier Jul 29 '12 at 08:44
  • 8
    This code doesn't work anymore with capybara 2: https://github.com/jnicklas/capybara/issues/863 – Mic92 Nov 28 '12 at 06:00
3

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
Jake
  • 715
  • 7
  • 15
3

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.

2

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
ChuckJHardy
  • 6,956
  • 6
  • 35
  • 39
  • 2
    That is easier than page.should have_xpath("//title", :text => "My Title")? – Mike Blyth May 14 '11 at 13:23
  • 1
    Rendering views in controller tests is not my cup of tea. I vote for using of integration tests (and capybara) for pate title verification. – Grimmo Apr 23 '12 at 12:21
  • 2
    capybara 2, only accepts :text not :content as parameter. The content of a title tag cannot be checked with the latest capybara: https://github.com/jnicklas/capybara/issues/863 – Mic92 Nov 28 '12 at 06:03
0

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
Starkers
  • 10,273
  • 21
  • 95
  • 158
-1

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

Hengjie
  • 4,598
  • 2
  • 30
  • 35