Expected css "title" with text in capybara

I use capybara instead of webrat in rails. I installed capybara and use the gem 'capybara' in the gemfile. when i use

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

he gives an error

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

The test file is as follows:

 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 
+8
rspec capybara
source share
5 answers

I don’t know which version of stones you are using, but I came across a similar instance where use: the text failed, but when I used: the content passed the test. I use rails 3.2.3, rspec-rails 2.9.0, capybara 1.1.2 and gems on Ubuntu Lucid Lynx.

Try replacing

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

from

 page.should have_selector("title", :content => "anything title") 
+12
source share

The problem is that browsers treat <title> tags as invisible. (Thanks to DreadPirateShawn for the link to issue on this).

There is no “clean” way to get the title, but with a little hack you can still check the value of the title 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 will be ignored and it will look as if your test passed. A newer version will give you a nice error message, for example:

ArgumentError: Invalid keys: content must be one of: text ,: visible ,: between ,: count ,: maximum ,: minimum

+4
source share

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

The problem with: content is that it is not recognized by Capybara, and then is ignored and displayed as PASSED, but this is an incorrect behavior.

So, if you are using Capybara, switch each content to: text to see if the test really passes, perhaps errors are not noticed.

+1
source share

I faced the same problems. Empirically, the following is what I found:

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

expects your html output to contain a tag as shown below:

 <title text="AnyTitle"/> 

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

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

then it expects your html output to contain the tag below

 <title>AnyTitle</title> 

So, if your final html rendering contains the <title text="AnyTitle"/> , you should use: text otherwise, if the resulting html rendering contains the <title>AnyTitle</title> , you can use: content instead.

PS my environment Gem: Capybara-2.0.2, Rails-3.2.12, RSpec rails-2.12.2, Webrat-0.7.3 if you remove webrat, then the keyword ": conetnt" is not recognized only by Capybara.

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

 #gem webrat gem 'capybara', '1.1.2' 

refer to prusswan answer

0
source share

Just do the following:

 expect(page).to have_title("some title") 
0
source share

All Articles