。可见?当它应该是假的时候返回true

时间:2016-01-30 14:54:58

标签: ruby-on-rails dom attributes capybara

在rails应用程序中,我有以下css

#step2 { display:none }

我对这种情况的测试在这里

Then /^I should not see "([^\"]*)" because it is hidden$/ do |id|
  s=page.find(id)
  expect(s.visible?).to be false
end

当我手动调出页面时,该部分不可见。

错误是

  expected true
       got false
   (RSpec::Expectations::ExpectationNotMetError)
  ./features/step_definitions/navagation_steps.rb:85:in `/^I should not see "([^\"]*)" because it is hidden$/'
  features/profile.feature:10:in `And I should not see "#step2" because it is hidden'

page.find调用确实可以正常工作。如果我打印出来的对象

#<Capybara::Node::Element:0x007fd42ea30818>

我尝试了各种不同的咒语无济于事。这似乎是最直截了当的。有人在这看到问题吗?这让我很难过。

3 个答案:

答案 0 :(得分:0)

期待(s.visible?)。not_to是eq(true)

这似乎有效。

答案 1 :(得分:0)

使用RSpec提供的动态谓词匹配器将更好地读取并提供更好的错误消息

expect(s).not_to be_visible

注意 - 如果您使用Capybara的默认机架测试驱动程序,它不会处理大多数“外部”CSS,因此除非display:none在elements style属性中,否则它将无法确定可见性

答案 2 :(得分:0)

如果要验证元素在页面上不可见,则可以使用下面提到的方法

#!/bin/bash

# Prompt a user to enter a word and output each letter in turn.


read -p "Please enter a word: " word

for i in $word
do
 echo "Letter $i: $word"
done

或者你也可以像

那样做
Then /^I should not see "([^\"]*)" because it is hidden$/ do |id|
  expect(page).to have_selector(<SELECTOR>,:visible => false)
end

注意: Then /^I should not see "([^\"]*)" because it is hidden$/ do |id| page.find(<SELECTOR>, :visible => false) end 是元素的css选择器。

希望这会有所帮助:)