stackov8
stackov8

Reputation: 434

how to check the presence of the element on the page?

please help solve the problem. i use rails4 + rspec + capybara.

my page has title-element:

<!DOCTYPE html>

<html lang="ru">
  <head>
    <title>I am learning Rails</title>
    <link rel="stylesheet" media="all" href="/assets/albums.self855.css?body=1" data-turbolinks-track="true" />
    ......
    .........
    ........

rspec test:

require 'spec_helper'

describe ImagesController, type: :controller do
  describe "index action" do
    it 'render title-element on root page' do
      visit '/'
      #page.should have_selector 'head title', :visible => false
      page.should have_selector('head title',
                          :text => "Складик картинок")
    end  
  end  
end  

i run in console:

rspec spec

but console displays follow error messages:

...F

Failures:

  1) ImagesController index action render title-element on root page
     Failure/Error: page.should have_selector('head title',
       expected to find css "head title" with text "I am learning Rails" but there were no matches
     # ./spec/controllers/images_controller_spec.rb:37:in `block (3 levels) in <top (required)>'

Finished in 0.94045 seconds (files took 1.95 seconds to load)
4 examples, 1 failure

Failed examples:

rspec ./spec/controllers/images_controller_spec.rb:30 # ImagesController index action render title-element on root page

Upvotes: 0

Views: 1411

Answers (2)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34318

Updated Answer:

Turns out that, there is already a have_title [Capybara RSpec matcher] which you can use to solve your problem.

describe ImagesController, type: :controller do
  describe "index action" do
    it 'render title-element on root page' do
      visit '/'
      expect(page).to have_title "Складик картинок"
    end  
  end  
end 

Upvotes: 1

Thomas Walpole
Thomas Walpole

Reputation: 49870

The title element isn't displayed on the page so it's not found by capybaras normal finders, however there is a title matcher you can use

page.should have_title('your title')

Upvotes: 1

Related Questions