ruby 你如何测试 div 在 rspec/capybara 中是否具有某种 css 样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11198882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you test if a div has a certain css style in rspec/capybara?
提问by Goalie
How do you test if a div tag has a certain css style? I'm trying to test if it has display:none;or display:block.
你如何测试一个 div 标签是否具有某种 css 样式?我正在尝试测试它是否具有display:none;或display:block。
I tried the following but its giving me an error:
我尝试了以下操作,但它给了我一个错误:
it {should have_selector('signup_server_generic_errors', /display:\s*none/)}
回答by Kevin Bedell
I'd recommend that instead of trying to locate the css style, you instead write your tests to find the css class name.
我建议您不要尝试定位 css 样式,而是编写测试以查找css class name。
This way you can change the underlying css styling while keeping the class the same and your tests will still pass.
通过这种方式,您可以在保持类不变的同时更改底层 css 样式,并且您的测试仍将通过。
Searching for the underlying style is brittle. Styles change frequently. Basing your rspecs on finding specific style elements makes your tests more brittle -- they'll be more likely to fail when all you do is change a div's look and feel.
寻找潜在的风格是脆弱的。风格经常变化。基于查找特定样式元素的 rspecs 会使您的测试更加脆弱——当您所做的只是更改 div 的外观和感觉时,它们更有可能失败。
Basing your tests on finding css classes makes the tests more robust. It allows them to ensure your code is working correctly while not requiring you to change them when you change page styling.
基于查找 css 类的测试使测试更加健壮。它允许他们确保您的代码正常工作,同时不需要您在更改页面样式时更改它们。
In this case specifically, one option may be to define a css class named .hiddenthat sets display:none;on an element to hide it.
特别是在这种情况下,一种选择可能是定义一个名为的 css 类.hidden,该类display:none;在元素上设置以隐藏它。
Like this:
像这样:
css:
css:
.hidden {
display:none;
}
html:
html:
<div class="hidden">HIDE ME!</div>
capybara:
水豚:
it {should have_css('div.hidden') }
This capybara just looks for a div that has the hiddenclass -- you can make this matcher more sophisticated if you need.
这个 capybara 只是寻找一个具有hidden该类的 div —— 如果需要,您可以使这个匹配器更加复杂。
But the main point is this -- attach styles to css class names, then tie your tests to the classes, not the styles.
但重点是——将样式附加到 css 类名,然后将您的测试绑定到类,而不是样式。
回答by luacassus
You could use has_css?matcher. It can accept :visibleoptions. For more details you can check docs: http://rdoc.info/github/jnicklas/capybara/Capybara/Node/Matchers#has_css%3F-instance_method
你可以使用has_css?匹配器。它可以接受:visible选项。有关更多详细信息,您可以查看文档:http: //rdoc.info/github/jnicklas/capybara/Capybara/Node/Matchers#has_css%3F-instance_method
For example you can try:
例如,您可以尝试:
it { should have_css('div.with-some-class', :visible => true) }
回答by 18augst
My way to ensure that element have a certain class:
我确保元素具有某个类的方法:
let(:action_items) { page.find('div.action_items') }
it "action items displayed as buttons" do
action_items.all(:css, 'a').each do |ai|
expect(ai[:class]).to match(/btn/)
end
end
or smthing like this
或者像这样
expect(ai[:style]).to match(/color: red/)
I found it here: http://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Element#%5B%5D-instance_method
我在这里找到它:http: //rubydoc.info/github/jnicklas/capybara/Capybara/Node/Element#%5B%5D-instance_method

