have_css matcher fails in ruby 3.2
barrerajl opened this issue · comments
Meta
Capybara Version: 3.39.2
Driver Information (and browser if relevant):
Expected Behavior
Just match the css expression
Actual Behavior
Failure/Error: expect(page).to have_css('.success', text: 'Field was created successfully')
ArgumentError:
wrong number of arguments (given 2, expected 1)
# /usr/local/bundle/gems/capybara-3.39.2/lib/capybara/node/matchers.rb:309:in `has_css?'
# /usr/local/bundle/gems/capybara-3.39.2/lib/capybara/session.rb:773:in `has_css?'
This is happening with all matchers that are dynamically generated in session.rb:773. E.g., have_button, have_select...
Steps to reproduce
Just use have_css matcher with more than one argument as seeing in the backtrace
Workaround:
It is not as expressive as expected but you can use
expect(page.has_css?('.success', text: 'Field was created successfully')).to eq true
This is a possible fix: #2680
I'm 99% sure this has nothing to do with Ruby 3.2 (in that it'll fail on Ruby 3.0, and 3.1 too) - this has to do with that you aren't actually using the Capybara matchers, and are instead using the RSpec have_xxx
matcher, which looks for and calls the predicate has_xxx?
method. If you show more of your stacktrace it will confirm that. You should be following the installation instructions so that the Capybara matchers are actually used.
Sorry for the noise, you are correct. Maybe worth mentioning it in the documentation?
After spending a couple hours on this same issue ArgumentError: wrong number of arguments (given 2, expected 0..1)
The fix was to add
it 'does something cool, type: :feature do
to my tests. This is due to capybara assuming you have a normal dir structure when you do not have a feature dir you have to tell capybara its a feature or you will get get this arg error.
this is mentioned in the README
https://github.com/teamcapybara/capybara?tab=readme-ov-file#using-capybara-with-rspec
But now someone searching the error can reference the readme.
doing expect(page.has_css?('.success', text: 'Field was created successfully')).to eq true
also works but that option kind of sucks ha.