diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index 37233433..49b75aeb 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -429,21 +429,25 @@ end # Capybara.add_selector(:label) do label "label" - xpath do |locator| + xpath(:for) do |locator, options| xpath = XPath.descendant(:label) xpath = xpath[XPath.string.n.is(locator.to_s) | XPath.attr(:id).equals(locator.to_s)] unless locator.nil? + if options.has_key?(:for) && !options[:for].is_a?(Capybara::Node::Element) + xpath = xpath[XPath.attr(:for).equals(options[:for].to_s).or((~XPath.attr(:for)).and(XPath.descendant()[XPath.attr(:id).equals(options[:for].to_s)]))] + end xpath end filter(:for) do |node, field_or_value| if field_or_value.is_a? Capybara::Node::Element - if field_or_value[:id] && (field_or_value[:id] == node[:for]) - true + if node[:for] + field_or_value[:id] == node[:for] else field_or_value.find_xpath('./ancestor::label[1]').include? node.base end else - node[:for] == field_or_value.to_s + #Non element values were handled through the expression filter + true end end diff --git a/lib/capybara/spec/session/selectors_spec.rb b/lib/capybara/spec/session/selectors_spec.rb index 64f4edfe..8df7093d 100644 --- a/lib/capybara/spec/session/selectors_spec.rb +++ b/lib/capybara/spec/session/selectors_spec.rb @@ -12,7 +12,11 @@ Capybara::SpecHelper.spec Capybara::Selector do expect(@session.find(:label, for: 'form_other_title')['for']).to eq 'form_other_title' end - it "finds a label from nested input using :for filter" do + it "finds a label from nested input using :for filter with id string" do + expect(@session.find(:label, for: 'nested_label').text).to eq 'Nested Label' + end + + it "finds a label from nested input using :for filter with element" do input = @session.find(:id, 'nested_label') expect(@session.find(:label, for: input).text).to eq 'Nested Label' end