Reputation: 5916
I'm trying to write some RSpec to verify if a div
is visible or not under defined circumstances but I'm having a hard time doing so.
In order to figure out how to make it work (in the real code I have a piece of JS that add the CSS class) I have hard-coded everything but still the visibility check is not working.
The code looks like
#CSS
.hidden {
display: none;
}
.visible {
display: block;
}
#HTML
<div id="blah" class="hidden">blah</div>
#RSpec
scenario 'blah blah' do
expect(find('#blah').visible?).to be false
end
An I get
Failure/Error: expect(find('#blah').visible?).to be false
expected false
got true
UPDATE1: I'm using the Selenium webdriver.
#rails_helper
...
require 'selenium-webdriver'
...
UPDATE2: updated HTML
Am I missing anything here? How can I check for visibility?
Thanks (politeness first)
Upvotes: 2
Views: 5896
Reputation: 49870
Since it seems like you're starting out with Capybara, I'm guessing you're using the rack-test driver. The rack-test driver is very basic and doesn't process most CSS. The only things it will process as far as visibility goes are the hidden
attribute and inline display: none
style . If you use one of the more advanced drivers (selenium, poltergeist, capybara-webkit) then CSS gets fully processed.
The next thing to note is that (by default) Capybara will only find visible elements, so find('#blah')
will either return a visible element or raise a Capybara::ElementNotFound exception. If you need to assert for the existence/non-existence of an element on a page you could do something like
expect(page).to have_css('#blah') #check the element exists and is visible
expect(page).not_to have_css('#blah') #check element doesn't exist or is non-visible
Another option would be to use the visible
option to change the default behavior of only finding visible elements, although this would generally be a non-necessary test since from the users perspective it doesn't matter whether the element exists or not if it's not visible (hence Capybaras default behavior of only finding visible elements).
expect(page).to have_css('#blah', visible: :hidden) # check but the element exists but is non-visible
Upvotes: 4