Capybara: How to test the title of a page?

Ruby on-RailsRspecCapybara

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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"

Solution 2 - Ruby on-Rails

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::")

Solution 3 - Ruby on-Rails

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")

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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

  1. 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.

Solution 7 - Ruby on-Rails

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

Solution 8 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNerianView Question on Stackoverflow
Solution 1 - Ruby on-RailsLars SchirrmeisterView Answer on Stackoverflow
Solution 2 - Ruby on-RailsjpwView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDylan MarkowView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJakeView Answer on Stackoverflow
Solution 5 - Ruby on-RailsChuckJHardyView Answer on Stackoverflow
Solution 6 - Ruby on-RailsAristotelis_ChView Answer on Stackoverflow
Solution 7 - Ruby on-RailsStarkersView Answer on Stackoverflow
Solution 8 - Ruby on-RailsHengjieView Answer on Stackoverflow