Skip to content

Commit

Permalink
Merge pull request #2423 from teamcapybara/multiple_label
Browse files Browse the repository at this point in the history
Support multiple labels when using allow_label_click on a checkbox - …
  • Loading branch information
twalpole committed Nov 10, 2020
2 parents 2225238 + 9d0720f commit fe5940c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/capybara/node/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ def _reset_style(element)
def _check_with_label(selector, checked, locator,
allow_label_click: session_options.automatic_label_click, **options)
options[:allow_self] = true if locator.nil?

synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do
el = find(selector, locator, **options)
el.set(checked)
Expand All @@ -372,7 +371,7 @@ def _check_with_label(selector, checked, locator,

begin
el ||= find(selector, locator, **options.merge(visible: :all))
el.session.find(:label, for: el, visible: true).click unless el.checked? == checked
el.session.find(:label, for: el, visible: true, match: :first).click unless el.checked? == checked
rescue StandardError # swallow extra errors - raise original
raise e
end
Expand Down
6 changes: 6 additions & 0 deletions lib/capybara/spec/session/check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('bugatti')
end

it 'should check via label if multiple labels' do
expect(@session).to have_field('multi_label_checkbox', checked: false, visible: :hidden)
@session.check('Label to click', allow_label_click: true)
expect(@session).to have_field('multi_label_checkbox', checked: true, visible: :hidden)
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/capybara/spec/views/form.erb
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,15 @@ New line after and before textarea tag
<p>
<input id="special" {custom}="abcdef" value="custom attribute"/>
</p>


<label for="multi_label_checkbox">
Label to click
</label>
<div>Something random that justifies the usage of a separate label</div>
<label>
<div>
<input type="checkbox" id="multi_label_checkbox" style="display: none"/>
<div>Visual representation of the checkbox</div>
</div>
</label>

0 comments on commit fe5940c

Please sign in to comment.