首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >水豚找不到元标签

水豚找不到元标签
EN

Stack Overflow用户
提问于 2013-04-18 07:51:59
回答 2查看 5.2K关注 0票数 23

Capybara 2.1.0似乎没有找到任何元标签:

(rdb:1) p page.find 'meta'
*** Capybara::ElementNotFound Exception: Unable to find css "meta"

即使它们出现在page.source

(rdb:1) p page.source
"<!doctype html>\n<html>\n<head>\n<title>MyTitle</title>\n<meta charset='utf-8'>\n<meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible'>\n<meta content='width=device-width, initial-scale=1' name='viewport'>\n<meta name='description'>\n\n..."
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-18 07:51:59

解决方案是在findhave_selector中使用:visible => false

page.should have_css 'meta[name="description"]', visible: false

或者:

page.find 'meta[name="description"]', visible: false
票数 40
EN

Stack Overflow用户

发布于 2013-08-16 13:29:18

如果您想检查更具体的meta,包括描述文本等:

https://gist.github.com/Lordnibbler/6247531

RSpec::Matchers.define :have_meta do |name, expected|
  match do |actual|
    has_css?("meta[name='#{name}'][content='#{expected}']")
  end

  failure_message_for_should do |actual|
    actual = first("meta[name='#{name}']")
    if actual
      "expected that meta #{name} would have content='#{expected}' but was '#{actual[:content]}'"
    else
      "expected that meta #{name} would exist with content='#{expected}'"
    end
  end
end

RSpec::Matchers.define :have_title do |expected|
  match do |actual|
    has_css?("title", :text => expected)
  end

  failure_message_for_should do |actual|
    actual = first("title")
    if actual
      "expected that title would have been '#{expected}' but was '#{actual.text}'"
    else
      "expected that title would exist with '#{expected}'"
    end
  end
end

然后,像这样查找meta:

page.should have_meta(:description, 'Brand new Ruby on Rails TShirts')

用爱从施普雷那里借来:https://github.com/spree/spree/blob/master/core/lib/spree/testing_support/capybara_ext.rb

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16072240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档