Capybara HaveSelector does not work with RSpec expect what I expect. I am new to Capybara and RSpec, so I may be mistaken about RSpec or Capybara, or it may be a disadvantage of Capybara (version 2.0.2). Please help me understand my error or handle the error message / function request.
In my RSpec, I wrote:
expect { click('.special-div .submit') }.to have_css('.submitted')
I expected this to be functionally equivalent
click('.special-div .submit') page.should have_css('.submitted')
but this is not so. Instead, matcher have_css tries to combine with the string conversion of the proc object, not the result of invoking the proc object. (In other words, click('.special-div .submit') never executed.)
Capybara Behavior:
- Pretty reasonable
- Capybara missing function example
- Error in Capybara 2.0.2
- Something else?
Also, I obviously can do what I want using the 2-line version above, but our team is trying to standardize expect {} , so is there a way to use the expect {} form and get it to do what I want?
EDIT
I inherited the code that I work with, so I did not understand that, as Andrey Botalov noted, click not a standard part of Capybara. It seems to be, but again click already heavily used for other things, so it might be better that Capybara does not add another definition.
Since some people seem skeptical, let me assure you that this code is working fine:
click('.special-div .submit') page.should have_css('.submitted')
For those interested in have_css() , i.e. RSpec magic for has_css? . For those who are wondering about click , in my project, someone conveniently created the click function as follows:
def click(css) page.execute_script("$('#{css}').first().trigger('click');") end
Why? Because none of the obvious alternatives worked.
click_on('.special-div .submit')
Moving on, @zetetic asked if
expect(click('.special-div .submit')).to have_css('.submitted')
will work. No, this will not work for us, because we are still on RSpec 2.9, and this syntax was introduced in 2.11, but even if we updated it, it still will not work, because click does not return the object. Perhaps this will work if we upgrade to version 2.11 and change click to return page .