简体   繁体   English

水豚:如何测试页面的样式表?

[英]Capybara: How to test a stylesheet of a page?

I want to have the ability to test the correct swapping of a stylesheet in my test suite. 我希望有测试正确的能力样式表的交换在我的测试套件。 With this post about testing the page title using Capybara , I thought I would be able to test any link tags in the head section of the page. 这篇关于使用Capybara测试页面标题的文章 ,我认为我可以测试页面head任何link标签。 But it seems I am mistaken. 但似乎我弄错了。

With a step like this: 用这样的步骤:

save_and_open_page
page.should have_xpath("//link") # just something as simple as this, first.

save_and_open_page generates a HTML like this (with some stuff removed for brevity): save_and_open_page生成这样的HTML(为简洁起见,删除了一些内容):

<head>
  ...
  <link href="/home/ramon/source/unstilted/public/system_test/stylesheets/fancake/css/2.css?1323572998" type="text/css" class="jquery-styler" rel="stylesheet">
  ...
</head>

But I get this failure: 但是我得到了这个失败:

expected xpath "//link" to return something (RSpec::Expectations::ExpectationNotMetError)

Given all that, how do I test a stylesheet ? 鉴于所有这些, 如何测试样式表

Thanks! 谢谢!

If you want to check that a CSS file exists on a page, then you can do the following: 如果要检查页面上是否存在CSS文件,则可以执行以下操作:

page.should have_xpath("//link[contains(@href, 'style.css')]")

That'll check whether there are any <link> elements where the href attribute contains style.css . 这将检查href属性是否包含style.css任何<link>元素。 The error about "expected xpath to return something" means that the XPath you provided didn't actually exist - why it thinks that, I'm not sure, as you have a perfectly valid <link> tag in the HTML you've provided. 关于“期望的xpath返回某项错误”的错误意味着您提供的XPath实际上并不存在-我不确定为什么它会认为您提供的HTML中有一个完全有效的<link>标记,所以我不确定。

当我检查CSS时,我正在做的事情就像

expect(page.body).to include('/home/ramon/source/unstilted/public/system_test/stylesheets/fancake/css/2.css')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM