Home > database >  Capybara expect an input placeholder that's within a div to have some text
Capybara expect an input placeholder that's within a div to have some text

Time:03-31

I have a snippet that looks like this and I'm trying to run a test that expects the placeholder.

<div >
  <label  for="article_creator_isni">Creator ISNI</label>
  <input type="text" name="article[creator_group][][creator_isni]" id="article_creator_group__creator_isni"  placeholder="Please enter the creator's ISNI, e.g. 0000 1234 5678 9101." data-field-name="ubiquity_creator_isni">
</div>

I've been able to get it to pass with

expect(page.find(".article_creator_isni input")['placeholder']).to eq 'Please enter the creator\'s ISNI, e.g. 0000 1234 5678 9101.'

Is there a better way to write the expect?

CodePudding user response:

That seems ok to me, you could be more specific by using the id perhaps though?

expect(find("#article_creator_group__creator_isni")["placeholder"]).to eq "Please enter the creator's ISNI, e.g. 0000 1234 5678 9101."

CodePudding user response:

Do not use the basic RSpec matchers (eq, etc) with Capybara objects - it will lead to flaky tests because it disables Capybaras retrying behavior. It might work in this case, if your page isn't dynamic, but getting out of the habit of doing it will benefit your tests longer term. Instead you should use the matchers provided by Capybara. In this case if all you're looking to check for is an input element with a specific placeholder

expect(page).to have_field(placeholder: "Please enter the creator's ISNI, e.g. 0000 1234 5678 9101.")

Note: In your shown HTML the labels for attribute doesn't match the id of the input element it's supposed to be associated with -- you might want to fix that, which would then allow you to match for the input based on the associated too if wanted, like

expect(page).to have_field('Creator ISNI')

Would also lead to better UX.

  • Related