Trying to test that page contains with:
# spec/features/reports_spec.rb
require \'spec_helper\'
feature \"A
I had the same issues when I upgraded to Capybara 2.0, and managed to solve them by creating the following custom RSpec matcher using Capybara.string:
spec/support/utilities.rb
RSpec::Matchers::define :have_title do |text|
match do |page|
Capybara.string(page.body).has_selector?('title', text: text)
end
end
Now, in a spec file where subject { page }, I can use:
it { should have_title("My Title") }
it { should_not have_title("My Title") }
As an aside, the conversation on this question was extremely helpful in getting me to this answer, so thank you!
If you don't want to create a custom RSpec matcher, thanks to this StackOverflow answer by user dimuch I now know that you can call have_selector directly on page.source (aliased page.body) to test for non-visible DOM elements:
it "should show the correct title" do
page.source.should have_selector('title', text: 'My Title')
end
or where subject { page }:
its(:source) { should have_selector('title', text: 'My Title') }
From Capybara 2.1, there are built-in have_title/has_title? matchers, negating the need for the custom RSpec matcher in this answer. Furthermore, the its(:source) { ... } tests described in Update 1 seem to break under Capybara 2.1; I've confirmed the have_title/has_title? work as expected, so it's probably best to go with the following syntax if you plan on upgrading:
When subject { page }:
it { should have_title("My Title") }
it { should_not have_title("My Title") }
When using expect syntax within an it/scenario block:
expect(page).to have_title("My Title")
expect(page).to_not have_title("My Title")