After loading a page I have code that runs and hides and shows various items based on data returned by an xhr.
My integration test looks something like this:
it "should not show the blah" do
page.find('#blah').visible?.should be_true
end
When I manually go to the page in the context this test runs, #blah is not visible as I expect. I suspect that Capybara is looking at the initial state of the page (invisible in this case), evaluating the state of the DOM and failing the test before the JS runs.
Yes, I set the :js => true
on the containing describe block :)
Any ideas would be greatly appreciated! I'm hoping I don't have to put an intentional delay in here, that feels flaky and will slow things down.
Using:
I have a Rails application in which there is a link which when clicked should submit an AJAX post request and return a JS response.
Link code:
The JS response (.js.haml file) should toggle the following hidden div on the page the link exists:
js.haml file contents:
I was testing my scenario of sending notification and displaying the notification status message to the user using Cucumber (cucumber-rails gem with built in Capybara support)
I was trying to test that the element having id: notification_status was visible on successful response in my step definition.For this I tried following statements:
Neither of above worked for me and failed my step.Out of the above listed 5 snippets the last 4 failed with following error:
which was strange because following statement was passing correctly:
And in fact I inspected the page source using
which showed up
which was expected.
Finally I found this link capybara assert attributes of an element which showed up how to inspect an element's attribute in raw manner.
Also I found in Capybara documentation for visible? method (http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#visible%3F-instance_method) following information:
Thus I came to the conclusion that when testing visibility of an element do not rely on results of Capybara's visible? method when using a CSS selector and using the solution suggested in link capybara assert attributes of an element
I came up with following:
Usage:
This is another way to do it that works perfectly fine for me:
Especially for more complex finds, such as
which would assert that an element has been hidden.