'Capybara expect an input placeholder that's within a div to have some text
I have a snippet that looks like this and I'm trying to run a test that expects the placeholder
.
<div class="article_creator_isni form-group">
<label class="control-label multi_value optional" for="article_creator_isni">Creator ISNI</label>
<input type="text" name="article[creator_group][][creator_isni]" id="article_creator_group__creator_isni" class="article_creator_group form-control multi-text-field multi_value ubiquity_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?
Solution 1:[1]
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."
Solution 2:[2]
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 labvel too if wanted, like
expect(page).to have_field('Creator ISNI')
Would also lead to better UX.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | J c |
Solution 2 |