capybaracapybara-webkit

Multiple selectors using have_selector


I wish to convert my find's to using have_selector. Presently I have this working as:

expect(find('div[some-attr=true]'))['some-data-attr']).to eq('Hello World')

I wish to convert this to use have_selector and I tried:

expect(page).to have_selector('div[some-attr=true][some-data-attr]')

What am I doing wrong here?


Solution

  • This should be

    expect(page).to have_css('div[some-attr=true][some-data-attr="Hello World"]')
    

    This will only work if 'some-data-attr' isn't actually a property that has been modified since the page load. The selectors match on attribute values, but since your original method calls #[] on a returned element it could have been accessing a property with a changed value. Without knowing what those attribute names actually are and how they've been used it's impossible to say for sure. If it is actually a property and you need to use it a lot in your app you could write a custom filter on the :css selector

    If Capybara.default_selector == :css then have_selector and have_css mean the same thing, but if you're using a css selector then you're better off just using have_css