From 1a8baa72a00d983eba5c0bdb7b09380a2cc2eacf Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Tue, 29 Sep 2020 22:16:29 -0400 Subject: [PATCH] fix rubocop violations --- lib/capybara/selector/definition/button.rb | 26 +++++++++++----------- lib/capybara/selector/selector.rb | 6 ++++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/capybara/selector/definition/button.rb b/lib/capybara/selector/definition/button.rb index 970884bc..89944166 100644 --- a/lib/capybara/selector/definition/button.rb +++ b/lib/capybara/selector/definition/button.rb @@ -9,24 +9,24 @@ Capybara.add_selector(:button, locator_type: [String, Symbol]) do unless locator.nil? locator = locator.to_s - attr_matchers = XPath.attr(:id).equals(locator) | - XPath.attr(:name).equals(locator) | - XPath.attr(:value).is(locator) | - XPath.attr(:title).is(locator) | - (XPath.attr(:id) == XPath.anywhere(:label)[XPath.string.n.is(locator)].attr(:for)) - attr_matchers |= XPath.attr(:'aria-label').is(locator) if enable_aria_label - attr_matchers |= XPath.attr(test_id) == locator if test_id + locator_matchers = XPath.attr(:id).equals(locator) | + XPath.attr(:name).equals(locator) | + XPath.attr(:value).is(locator) | + XPath.attr(:title).is(locator) | + (XPath.attr(:id) == XPath.anywhere(:label)[XPath.string.n.is(locator)].attr(:for)) + locator_matchers |= XPath.attr(:'aria-label').is(locator) if enable_aria_label + locator_matchers |= XPath.attr(test_id) == locator if test_id - input_btn_xpath = input_btn_xpath[attr_matchers] + - XPath.descendant(:label)[XPath.string.n.is(locator)].descendant(input_btn_xpath) + input_btn_xpath = input_btn_xpath[locator_matchers] + locate_label(locator).descendant(input_btn_xpath) - btn_xpath = btn_xpath[attr_matchers | XPath.string.n.is(locator) | XPath.descendant(:img)[XPath.attr(:alt).is(locator)]] + - XPath.descendant(:label)[XPath.string.n.is(locator)].descendant(btn_xpath) + btn_xpath = btn_xpath[locator_matchers | + XPath.string.n.is(locator) | + XPath.descendant(:img)[XPath.attr(:alt).is(locator)] + ] + locate_label(locator).descendant(btn_xpath) alt_matches = XPath.attr(:alt).is(locator) alt_matches |= XPath.attr(:'aria-label').is(locator) if enable_aria_label - image_btn_xpath = image_btn_xpath[alt_matches] + - XPath.descendant(:label)[XPath.string.n.is(locator)].descendant(image_btn_xpath) + image_btn_xpath = image_btn_xpath[alt_matches] + locate_label(locator).descendant(image_btn_xpath) end %i[value title type].inject(input_btn_xpath.union(btn_xpath).union(image_btn_xpath)) do |memo, ef| diff --git a/lib/capybara/selector/selector.rb b/lib/capybara/selector/selector.rb index f9779fe0..2c91a0b8 100644 --- a/lib/capybara/selector/selector.rb +++ b/lib/capybara/selector/selector.rb @@ -132,7 +132,11 @@ module Capybara attr_matchers |= XPath.attr(test_id) == locator if test_id locate_xpath = locate_xpath[attr_matchers] - locate_xpath + XPath.descendant(:label)[XPath.string.n.is(locator)].descendant(xpath) + locate_xpath + locate_label(locator).descendant(xpath) + end + + def locate_label(locator) + XPath.descendant(:label)[XPath.string.n.is(locator)] end def find_by_attr(attribute, value)