Skip to content

Instantly share code, notes, and snippets.

@nruth
Created May 24, 2010 02:10
Show Gist options
  • Save nruth/411453 to your computer and use it in GitHub Desktop.
Save nruth/411453 to your computer and use it in GitHub Desktop.
capybara hidden element has_content example
@ignore-hidden-elements
Feature: Div help content hiding and revealing
In order to keep help text available but neatly tucked away when not needed
As a designer
I want javascript to detect div.help and hide it and its contents, replacing with a link to reveal it again
Background:
#this is a rails app with a Page model and a path_to entry for visiting them in features like this (using Pickle for cucumber / machinist integration)
Given a page exists with content: "<div class='help'>foobar</div>"
@rack-test
Scenario: no javascript, help is always visible
When I go to the page
Then I should see "foobar" within "div.help"
@javascript
Scenario: javascript should hide it
When I go to the page
#the next test passes
Then the page should not match css selector "div.help"
#but this one fails, it matches the content despite it being hidden & Capybara being told to ignore hidden elements
Then I should not see "foobar"
#opt-in certain scenarios to treat hidden elements 'correctly'
# just in case there is some reason why this isn't the global default behaviour already
Before('@ignore-hidden-elements') do
Capybara.ignore_hidden_elements = true
end
After('@ignore-hidden-elements') do
Capybara.ignore_hidden_elements = false
end
Then /^the page should match css selector "([^\"]*)"$/ do |selector|
page.should have_css(selector)
end
Then /^the page should not match css selector "([^\"]*)"$/ do |selector|
page.should have_no_css(selector)
end
#and from cucumber rails:
Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
regexp = Regexp.new(regexp)
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
page.should have_xpath('//*', :text => regexp)
else
assert page.has_xpath?('//*', :text => regexp)
end
end
end
Then /^(?:|I )should not see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
with_scope(selector) do
if defined?(Spec::Rails::Matchers)
page.should have_no_content(text)
else
assert page.has_no_content?(text)
end
end
end
@gduquesnay
Copy link

Have you found a solution for this?

@nruth
Copy link
Author

nruth commented Mar 16, 2011

there was a long-standing ticket on capybara's issue tracker for it and various people trying to fix it, I don't know if it's still there or not, I haven't tried to use that feature for a long time.

@gduquesnay
Copy link

gduquesnay commented Mar 16, 2011 via email

@cmeiklejohn
Copy link

Got a link to the issue?

@gduquesnay
Copy link

It's been awhile but I think it was this one teamcapybara/capybara#81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment