RSpec / Capybara has_selector conundrum test

This is from the Ruby on Rails tutorial by Michael Hartl 2nd edition. The third (and currently latest) version is not tested with RSpec, so I decided to use this book.

I read the user workarounds and know that there are different ways to write a test so that it works, but I want to use has_selector so that the code is consistent. Any explanation / advice would be very helpful. I do not understand why below the 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> 
+6
source share
2 answers

Try:

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

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

+9
source

Updating this for capybara's current best practices since there is now a has_title match.

 expect(page).to have_title('Ruby on Rails Tutorial Sample App | Home') 
+3
source

Source: https://habr.com/ru/post/980264/


All Articles