Search code examples
testingruby-on-rails-4rspeccapybaraassert

Capybara says can't find CSS selector even though I see it


Truly befuddled, this is the test output:

Failure/Error: page.assert_selector("#borrower cancel")
 Capybara::ExpectationNotMet:
   expected to find css "#borrower cancel" but there were no matches
 # ./spec/features/borrow_dashboard_spec.rb:354:in `block (3 levels) in <top (required)>'

I did a save_and_open_page (yes for the right test) and this is the relevant excerpt from the Inspect Element:

enter image description here

What is happening?! FYI in a similar situation, other tests that look for #borrowed item also say it can't be found. I tried putting the id in both <td> and <span> and then also tried making it one string, e.g., borrowed_item. Nothing has worked so far.


Solution

  • As @AndreyBotalov points out, the problem is with id attribute including spaces, which is not valid html. The best solution would be to fix the id attribute to not use spaces.

    If that is not an option, you can use an attribute selector instead of the id selector. In the attribute selector, make sure to include quotations around the id attribute value:

    page.assert_selector('[id="borrower cancel"]')