使用specs测试,来自railstutorial第3章的capybara不起作用(have_selector('title',:text =>'| Home'))

far*_*ukg 10 testing capybara spork rspec-rails ruby-on-rails-3.2

我正在研究ruby.railstutorial.org/ruby-on-rails-tutorial-book.我使用rails 3.2.7,spork,rspec,capybara,launchy和一些警卫:)

我在第3章中有一个非常奇怪的问题:测试:

似乎测试不适用于<head>-Tag中的内容.如果我将<title>-tag放在<body>-tag而不是head-tag中,它可以正常工作.当我把<h1>-tags放在<title>里面 - <head>标签时,它也有效.这很奇怪,不是吗?

请帮帮我搞清楚.

示例来自:ruby.railstutorial.org/chapters/static-pages#code:title_test:

it "should have the right title" do
  visit '/static_pages/home'
  page.should have_selector('title',
                    :text => "Ruby on Rails Tutorial Sample App | Home")
end
Run Code Online (Sandbox Code Playgroud)

错误消息是:

失败:

1)静态页面主页应该有标题'Home'失败/错误:page.should have_selector('title',:text =>'| Home')Capybara :: ExpectationNotMet:期望找到带有文本的css"title" |主页"但没有比赛.还找到"",它匹配选择器但不是所有过滤器.#./spec/requests/static_pages_spec.rb:15:在'块(3级)中'

那个工作:

it "should have the h1 'Sample App'" do
  visit '/static_pages/home'
  page.should have_selector('h1', :text => 'Sample App')
end
Run Code Online (Sandbox Code Playgroud)

呈现的HTML文件:

<!DOCTYPE html>
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | Home</title>
  <!-- some css,js stuff -->
</head>
<body>

<h1>Sample App</h1>
<p>
  This is the home page for the
  <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
  sample application
</p>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑:你可以在github上找到我正在使用的文件:https: //github.com/farukg/sample_app/

指向文件的链接:https: //github.com/farukg/sample_app/blob/master/spec/requests/static_pages_spec.rb

我所做的解释:主页的代码就像应该的样子.about页面的代码有自己的布局,里面标题标签上面有一个h1-tag,表明它可以用于某种原因.最后,帮助页面在body标签内部也有标题标签.

我绝对迷茫,为什么我有这么奇怪的行为?

守卫的完整输出:

> Run all
Bundle already up-to-date
Running all specs
Running tests with args ["--drb", "--colour", "-f", "progress", "-r", "/home/faruk/.rvm/gems/ruby-1.9.3-p125/gems/guard-rspec-1.2.0/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
..FFF.....Neues Fenster in aktueller Browsersitzung erstellt.
.FF..

Failures:

  1) Static pages Home page having application layout should have_selector head title 'Home'
     Failure/Error: page.should have_selector('head title',
     Capybara::ExpectationNotMet:
       expected to find css "head title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  2) Static pages Home page having application layout should have content 'Home'
     Failure/Error: page.should have_content("Ruby on Rails Tutorial Sample App | Home")
       expected there to be text "Ruby on Rails Tutorial Sample App | Home" in "Sample App This is the home page for the Ruby on Rails Tutorial sample application"
     # ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'

  3) Static pages Home page having application layout should have css title 'Home'
     Failure/Error: page.should have_css("title", :text => "Ruby on Rails Tutorial Sample App | Home")
     Capybara::ExpectationNotMet:
       expected to find css "title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/requests/static_pages_spec.rb:36:in `block (3 levels) in <top (required)>'

  4) Static pages about page with own layout should JUST have_selector head title 
     Failure/Error: page.should have_selector('head title')
     Capybara::ExpectationNotMet:
       expected to find css "head title" but there were no matches
     # ./spec/requests/static_pages_spec.rb:86:in `block (3 levels) in <top (required)>'

  5) Static pages about page with own layout should have_selector head title 'About Us'
     Failure/Error: page.should have_selector('head title',
     Capybara::ExpectationNotMet:
       expected to find css "head title" with text "Ruby on Rails Tutorial Sample App | About Us" but there were no matches
     # ./spec/requests/static_pages_spec.rb:93:in `block (3 levels) in <top (required)>'

Finished in 0.66215 seconds
15 examples, 5 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:20 # Static pages Home page having application layout should have_selector head title 'Home'
rspec ./spec/requests/static_pages_spec.rb:27 # Static pages Home page having application layout should have content 'Home'
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Home page having application layout should have css title 'Home'
rspec ./spec/requests/static_pages_spec.rb:83 # Static pages about page with own layout should JUST have_selector head title 
rspec ./spec/requests/static_pages_spec.rb:90 # Static pages about page with own layout should have_selector head title 'About Us'
Done.

> Neues Fenster in aktueller Browsersitzung erstellt.
Neues Fenster in aktueller Browsersitzung erstellt.
Run Code Online (Sandbox Code Playgroud)

hta*_*irt 7

这只是因为<title>是在<head>这样它不会出现.

用这个,解决了我的问题:

page.should have_selector 'title', :visible => false
Run Code Online (Sandbox Code Playgroud)

提示:您可以应用同样的方法来检查其他<head>元素,<meta>例如,确保您(并且您会保持)Google友好.

  • 非常感谢您解释这个问题.下面是使用CSS选择器的相同内容的示例用法:page.should have_css('head img [src ="http://d.adroll.com/cm/f/out"]',visible:false) (2认同)

pru*_*wan 6

我刚刚在Github上检查了你的项目,似乎你正在使用capybara的边缘版本:

的Gemfile

gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
Run Code Online (Sandbox Code Playgroud)

Gemfile.lock的

GIT
  remote: git://github.com/jnicklas/capybara.git
  revision: e561d249555195cdd0e9251246fc75aae876f833
  specs:
      capybara (2.0.0.beta2)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 2.0)
      xpath (~> 1.0.0.beta1)
Run Code Online (Sandbox Code Playgroud)

如果没有别的,我敢说这是你问题的原因.(当然,在更新capybara以使用边缘版本后,我的参考项目全部被打破)

因此,除非您热衷于帮助进行Capybara 2的beta测试,否则您最好坚持使用此处列出的1.1.2版本.


Lee*_*lly 5

如果你使用的是Capybara 2,你可以使用这样的have_title方法:

page.should have_title("Ruby on Rails Tutorial Sample App | Home")

  • 谢谢,这个解决方案对我有用 (2认同)