8

I am using capybara in place of webrat in rails. I have installed capybara and use gem 'capybara' in Gemfile. when I use

page.should have_selector("title", :text => "anything title")

it's give an error

Failure/Error: page.should have_selector("title", :text => "anything title")
expected css "title" with text "anything title" to return something

test file is as below:

require 'spec_helper'

describe "Test pages" do  
  describe "Home page" do    
    it "should have the content 'Demo App'" do
    visit '/test_pages/home'      
    page.should have_selector("title", :text => "anything title")               
  end
 end
end
1
  • 1
    Have you tried save_and_open_page right before your test assertion to see what Capybara thinks it is seeing?
    – nmott
    Apr 23, 2012 at 12:52

5 Answers 5

12

Not sure which version of gems you are using but I ran into a similar instance where using :text failed but when I used :content it passed the test. I'm using rails 3.2.3, rspec-rails 2.9.0, capybara 1.1.2 and therubyracer gems on Ubuntu Lucid Lynx.

Try replacing

page.should have_selector("title", :text => "anything title")

with

page.should have_selector("title", :content => "anything title")
7
  • Hey! I am using gem 'rails', '3.2.3', gem 'rspec-rails', '2.9.0', and also gem 'capybara', '1.1.2' on Ubuntu and as well as windows also. I've used :content and it's working fine! Thanks.
    – vajapravin
    May 10, 2012 at 5:16
  • Glad it worked but I have since gotten :text to work and according to the documentation using :text is the correct way to do this. I had a ghost in the machine as I cannot recreate the issue. See rubydoc:Capybara::RSpecMatchers#have_selector
    – rbnewb
    May 23, 2012 at 20:24
  • I have the same problem. For some reason, only on the title element, :content works but :text does not... so weird. Sep 26, 2012 at 18:32
  • 6
    For me a test with :content passed even though it shouldn't have passed.
    – Martynas
    Oct 4, 2012 at 22:11
  • It didn't work for me either way. with :context I get the following error: Failure/Error: page.should have_selector('title', ArgumentError: invalid keys :content, should be one of :text, :visible, :between, :count, :maximum, :minimum Is there any other solution for this problem?
    – ElHacker
    Jan 23, 2013 at 21:09
4

The problem here is that browsers treat the <title> tags as being invisible. (Thank you DreadPirateShawn for linking to the issue on this subject).

There's no 'clean' way to get the title, but with a bit of a hack you can still test the title's value by doing the following:

first('head title').native.text.should == "WhateverYourTitleNeedsToBe"

Do NOT use the :content symbol because in older versions of Capybara invalid tags would be ignored and it would appear as if though your test has passed. The newer version will give you a nice error message like:

ArgumentError: invalid keys :content, should be one of :text, :visible, :between, :count, :maximum, :minimum

1
  • I believe that the cleanest way to get the title is to use xpath. Like this: page.find :xpath, '//title', visible: false
    – etagwerker
    Apr 3, 2013 at 22:35
1

I have a similar problem, just to let you know, the use of :content is not supported in Capybara, it should be :text.

The problem with :content is that it is not recognized by Capybara and then it is ignored and showed as PASSED, but this is a wrong behavior.

So if you use Capybara, switch every :content to :text to see if the test really passes, maybe there are errors not noticed.

0

I encountered the same problems. Empirically the following is what I found:

page.should have_selector("title", :text => "AnyTitle")

will expect your html output to contain a tag as below:

<title text="AnyTitle"/>

however, if you use :content instead of :text as below

page.should have_selector("title", :content => "AnyTitle")

then it will expect your html output to contain a tag as below

<title>AnyTitle</title>

So if your resulting html rendering contains <title text="AnyTitle"/> tag, you should use :text otherwise if your resulting html rendering contains <title>AnyTitle</title> tag, you might want to use :content instead.

P.S. my Gem environments: capybara-2.0.2, rails-3.2.12, rspec-rails-2.12.2, webrat-0.7.3 if remove webrat, then the ":conetnt" key word is unrecognized by Capybara alone.

But the clean way to fix it is: get rid of webrat and install a stable Capybara-1.1.2, that is in the Gemfile

#gem webrat
gem 'capybara', '1.1.2'

please refer to prusswan's answer

0

Just do this:

expect(page).to have_title("some title")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.