Skip to content

Commit

Permalink
Merge f3f7a67 into 9e63367
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Dec 30, 2018
2 parents 9e63367 + f3f7a67 commit 6772e16
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 151 deletions.
87 changes: 53 additions & 34 deletions lib/watir/elements/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def clear
#

def include?(str_or_rx)
option(text: str_or_rx).exist? || option(label: str_or_rx).exist?
option(text: str_or_rx).exist? || option(label: str_or_rx).exist? || option(value: str_or_rx).exists?
end

#
Expand All @@ -29,9 +29,15 @@ def include?(str_or_rx)
# @return [String] The text of the option selected. If multiple options match, returns the first match.
#

def select(*str_or_rx)
results = str_or_rx.flatten.map { |v| select_by v }
results.first
def select(*both, text: nil, value: nil, label: nil)
selection = {both: both, text: text, value: value, label: label}.select do |_k, v|
!v.nil? && ![v].flatten.empty?
end
raise "Can not select by more than one method: #{selection.inspect}" if selection.size > 1

value = normalize_value(selection.values.first)

[value].flatten.map { |v| select_by v }.first
end

#
Expand All @@ -43,8 +49,9 @@ def select(*str_or_rx)
#

def select_all(*str_or_rx)
results = str_or_rx.flatten.map { |v| select_all_by v }
results.first
Watir.logger.deprecate 'Select#select_all', 'Select#select with an Array or multiple parameters',
ids: [:select_all]
select_all_by(*str_or_rx)
end

#
Expand All @@ -54,9 +61,15 @@ def select_all(*str_or_rx)
# @raise [Watir::Exception::NoValueFoundException] if the value does not exist.
#

def select!(*str_or_rx)
results = str_or_rx.flatten.map { |v| select_by!(v, :single) }
results.first
def select!(*both, text: nil, value: nil, label: nil)
selection = {both: both, text: text, value: value, label: label}.select do |_k, v|
!v.nil? && ![v].flatten.empty?
end
raise 'Can not select by more than one method' if selection.size > 1

value = normalize_value(selection.values.first)

[value].flatten.map { |v| select_by! v, :multiple }.first
end

#
Expand All @@ -67,8 +80,9 @@ def select!(*str_or_rx)
#

def select_all!(*str_or_rx)
results = str_or_rx.flatten.map { |v| select_by!(v, :multiple) }
results.first
Watir.logger.deprecate 'Select#select_all!', 'Select#select! with an Array or multiple parameters',
ids: [:select_all]
select!(*str_or_rx)
end

#
Expand All @@ -81,9 +95,10 @@ def select_all!(*str_or_rx)
# @return [String] The option selected. If multiple options match, returns the first match
#

def select_value(str_or_rx)
Watir.logger.deprecate '#select_value', '#select', ids: [:select_value]
select_by str_or_rx
def select_value(value)
Watir.logger.deprecate '#select_value', '#select directly or with :value keyword',
ids: [:select_value]
select(value: value)
end

#
Expand Down Expand Up @@ -114,8 +129,7 @@ def selected?(str_or_rx)
#

def value
option = selected_options.first
option&.value
selected_options.first&.value
end

#
Expand All @@ -126,8 +140,7 @@ def value
#

def text
option = selected_options.first
option&.text
selected_options.first&.text
end

# Returns an array of currently selected options.
Expand All @@ -136,18 +149,14 @@ def text
#

def selected_options
element_call { execute_js :selectedOptions, self }
element_call { execute_js :selectedOptions, @element }
end

private

def select_by(str_or_rx)
found = find_options(:value, str_or_rx)

if found.size > 1
Watir.logger.deprecate 'Selecting Multiple Options with #select', '#select_all',
ids: [:select_by]
end
select_matching(found)
end

Expand All @@ -164,7 +173,7 @@ def select_by!(str_or_rx, number)

def process_str_or_rx(str_or_rx)
case str_or_rx
when String
when Numeric, String
"^#{str_or_rx}$"
when Regexp
str_or_rx.inspect.sub('\\A', '^')
Expand All @@ -174,8 +183,23 @@ def process_str_or_rx(str_or_rx)
.sub(%r{\/[a-z]*$}, '')
.gsub(/\(\?#.+\)/, '')
.gsub(/\(\?-\w+:/, '(')
end
end

def normalize_value(value)
msg = "expected String, Numeric or Regexp, got #{value.inspect}:#{value.class}"

case value
when Array
raise TypeError, msg if value.empty?

value.map(&method(:normalize_value))
when Numeric
value.to_s
when String, Regexp
value
else
raise TypeError, "expected String or Regexp, got #{str_or_rx.inspect}:#{str_or_rx.class}"
raise TypeError, msg
end
end

Expand All @@ -200,15 +224,10 @@ def select_all_by(str_or_rx)

def find_options(how, str_or_rx)
wait_while do
case str_or_rx
when String, Numeric, Regexp
@found = how == :value ? options(value: str_or_rx) : []
@found = options(text: str_or_rx) if @found.empty?
@found = options(label: str_or_rx) if @found.empty?
@found.empty? && Watir.relaxed_locate?
else
raise TypeError, "expected String or Regexp, got #{str_or_rx.inspect}:#{str_or_rx.class}"
end
@found = how == :value ? options(value: str_or_rx) : []
@found = options(text: str_or_rx) if @found.empty?
@found = options(label: str_or_rx) if @found.empty?
@found.empty? && Watir.relaxed_locate?
end
# TODO: Remove conditional when remove relaxed_locate toggle
return @found unless @found.empty?
Expand Down

0 comments on commit 6772e16

Please sign in to comment.