Capybara: How to test the title of a page? Capybara: How to test the title of a page? ruby-on-rails ruby-on-rails

Capybara: How to test the title of a page?


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

page.titlepage.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"


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


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