capybara: page.should have_no_content doesn't

2019-01-24 10:09发布

I would like to use page.should have_no_content to check if the page doesn't display the label to user, here what it is in HTML:

<li id="account_input" style="display: none;">
    <label for="account_name">My Account</label>
    ...
</li>

So when I use page.should have_no_content("My Account"), it returns false instead of true.

4条回答
beautiful°
2楼-- · 2019-01-24 10:56

I found a solution using:

Then /^"([^\"]+)" should not be visible$/ do |text|
  paths = [
    "//*[@class='hidden']/*[contains(.,'#{text}')]",
    "//*[@class='invisible']/*[contains(.,'#{text}')]",
    "//*[@style='display: none;']/*[contains(.,'#{text}')]"
  ]
  xpath = paths.join '|'
  page.should have_xpath(xpath)
end
查看更多
再贱就再见
3楼-- · 2019-01-24 11:01

So when I use page.should have_no_content("My Account"), it returns false instead of true.

It should be false. Think about it this way: if your page does have the content "My account", then have_content would return True, thus have_no_content should return False. And the reason - it is not true to say that the HTML does not have the content "My account" in it. Thus, it is False.

查看更多
Root(大扎)
4楼-- · 2019-01-24 11:03

You could use this statement

find('#account_input').should_not be_visible
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-24 11:07

I found another way to implement should not have

page.should_not( have_content(arg1))

查看更多
登录 后发表回答