Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question

5 Answers 5

up vote 9 down vote accepted

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")
share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Just do this:

expect(page).to have_title("some title")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.3.6.2365\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nosc"], "copyright__copyright__copyright": ["ybara alone.\n\nBut the clean way to fix it is: get rid of webrat and install a stable Capybara-1.1.2, that is in the Gemfile\n\n#gem webrat\ngem 'capybara', '1.1.2'\n\nplease refer to prusswan's answer\n\nshare|improve this answer\n\nJust do this:\n\nexpect(page).to have_title(\"some title\")\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}