Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec/Capybara testing have_selector conundrum

This is from Ruby on Rails Tutorial by Michael Hartl 2nd edition. The 3rd (and currently latest) edition does not test with RSpec so I decided to use this book.

I've read users' workarounds and know there are different ways to write the test to make it work but I want to use have_selector to keep the code consistent. Any explanation/advice would be very helpful. I do not understand why the below test passes for the h1 element and not the title element:

spec.rb:

describe "Static pages" do 

  describe "Home page" do

  it "should have the h1 'Sample App'" do
    visit '/static_pages/home'
    expect(page).to have_selector('h1', :text => 'Sample App')
  end

  it "should have the title 'Home'" do
    visit '/static_pages/home'
    expect(page).to have_selector('title', 
                         :text => "Ruby on Rails Tutorial Sample App | Home")
  end
end

home.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Ruby on Rails Tutorial Sample App | Home</title>
</head>
<body>
  <h1>Sample App</h1>

</body>
</html>
like image 466
SSOK Avatar asked Dec 29 '14 18:12

SSOK


1 Answers

Try:

expect(page).to have_selector('title', 
                     :text => "Ruby on Rails Tutorial Sample App | Home",
                     :visible => false)

Since the title tag is in <head> element, it is considered hidden. Specifying :visible => false includes those tags for consideration in the have_selector matcher.

like image 102
Prakash Murthy Avatar answered Sep 20 '22 16:09

Prakash Murthy