implement :label built-in selector partially via expression filter

This commit is contained in:
Thomas Walpole 2016-09-28 11:49:08 -07:00
parent 1af5c17d65
commit 4d029d94f2
2 changed files with 13 additions and 5 deletions

View File

@ -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

View File

@ -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