in my rspec feature I try to check a checkbox with it related label because the input is hidden. My label contains link (<a>
tag), so every time I use check('user[agree_terms]', allow_label_click: true)
it never checks the box but instead open the link contains in the label.
Here's my view generated from ERB
<p >
<input name="user[agree_terms]" type="hidden" value="0">
<input required="required" aria-required="true" type="checkbox" value="1" name="user[agree_terms]" id="user_agree_terms">
<label for="user_agree_terms">
<abbr title="required">*</abbr>
J'ai accepté les <a target="_blank" href="https://somelink">Conditions Générales d'Utilisation</a> et la <a target="_blank" href="https://somelink">Politique de Confidentialité</a>
</label>
</p>
Here's my spec
# I tried both but its behave the same way : open the <a> tag
find('label[for="influencer_agree_terms"]').click
check('influencer[agree_terms]', allow_label_click: true)
Do you know another way to check that box ?
By the way the UI on browser work correctly when I click the label.
CodePudding user response:
When you tell Capybara to click on an element it clicks in the middle of that element which in this case is where you have the conditions
link, so that takes the click before the label element. One option would be to specifically click on the abbr element, which is in the label but away from the link
find('label[for="influencer_agree_terms"] abbr').click
Another option would be to take advantage of the fact that allow_label_click supports specifying offsets for the click location
check('influencer[agree_terms]', allow_label_click: { x: 10, y: 10 })
Depending on the setting of Capybara.configure w3c_click_offset
that offset could be from the center or from the top left corner of the label element so adjust the values as necessary. Technically you can do the same thing when calling click
find('label[for="influencer_agree_terms"]').click(x: 10, y: 10)
but it's generally better to use check
when dealing with a checkbox
CodePudding user response:
By default capybara #click
method will click in the middle of the element, right where my link is located.
Fortunately #click
has offset option as follow #click(x: 0, y: 0)
.
So to check a box with the help of its label containing link :
find('label[for="influencer_agree_terms"]').click(x: 0, y: 0)