capybara:page.should have_no_content无法正常显示display:none元素

Sam*_*ang 8 integration-testing ruby-on-rails cucumber capybara

我想使用page.should have_no_content来检查页面是否没有向用户显示标签,这里是HTML中的内容:

<li id="account_input" style="display: none;">
    <label for="account_name">My Account</label>
    ...
</li>
Run Code Online (Sandbox Code Playgroud)

因此,当我使用page.should have_no_content("我的帐户")时,它返回false而不是true.

Shi*_*hiv 12

您可以使用此声明

find('#account_input').should_not be_visible
Run Code Online (Sandbox Code Playgroud)


Sam*_*ang 3

我找到了一个解决方案,使用:

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
Run Code Online (Sandbox Code Playgroud)