Reputation: 10111
I am just writing a couple of test and I want to make sure that I can test that some meta tags are on the page.
describe "article", :type => :feature do
it "has correct meta tags", js: true do
love_article = FactoryGirl.create(:love_article, title: 'Kitten Takes Over the World!', created_at: "Sat, 30 May 2015 12:12:21 -0400", updated_at: "Mon, 01 Jun 2015 12:12:44 -0400")
visit "/articles/#{love_article.slug}"
page.source.should have_selector('title', text: 'Kitten Takes Over the World!', visible: false) # this is found
expect(page).to have_selector('pubdate', text: '05/30/15 12:12:21 PM', visible: false) # This is not found
# ERROR
# page.source.should have_selector('pubdate', text: '5/30/15 12:12:21 PM', visible: false)
# RSpec::Expectations::ExpectationNotMetError: expected to find css "pubdate" with text "5/30/15 12:12:21 PM" but there were no matches from /Users/moiseszaragoza/.rvm/gems/ruby-2.1.2/gems/rspec-expectations-3.0.4/lib/rspec/expectations/fail_with.rb:30:in `fail_with'
end
end
now when I add a debugger and I do page.source
I get
<!DOCTYPE html>
<html>
<head>\n
<title>Kitten Takes Over the World!</title>\n
<link rel=\ "stylesheet\" media=\ "all\" href=\ "/assets/application.css\">\n
<meta content=\ "Kitten Takes Over the World!\" property=\ "og:title\">\n
<meta content=\ "http://127.0.0.1:51840/articles/2015/05/30/kitten-takes-over-the-world\" property=\ "og:url\">\n
<meta content=\ "Tell on Me is enabling parents to phone in complaints on unsuspecting teen drivers.\" property=\ "og:description\">\n
<meta content=\ "/uploads/test/article/hero_image/42/test-image-file-employee.png\" property=\ "og:image\">\n
<meta content=\ "article\" property=\ "og:type\">\n
<meta content=\ "IE=edge,chrome=1\" http-equiv=\ "X-UA-Compatible\">\n
<meta charset=\ "utf-8\">\n
<meta content=\ "text/html\" http-equiv=\ "Content-Type\">\n
<meta name=\ "section\">\n
<meta content=\ "05/30/15 12:12:21 PM\" property=\ "og:pubdate\">\n
<meta content=\ "05/30/15 12:12:21 PM\" name=\ "pubdate\">\n
<meta content=\ "06/01/15 12:12:44 PM\" name=\ "lastmod\">\n
<meta content=\ "Joe Shmoe\" name=\ "author\">\n
I guess to clarify I need to find this line from the source code
<meta content=\ "05/30/15 12:12:21 PM\" name=\ "pubdate\">\n
Upvotes: 2
Views: 949
Reputation: 5120
Assuming you are using Capybara, you can do what was described in this post:
page.should have_css 'meta[name="description"]', :visible => false
or
page.find 'meta[name="description"]', :visible => false
Capybara by default does not work on elements not directly visible to the user (title is visible at the top of the browser/tab, so that was passing).
To check the content:
expect(page).to have_css 'meta[name="description"][content="expected_content"]', :visible => false
Upvotes: 4