Skip to content

Commit

Permalink
Add boolean methods for some boolean attribute selectors to handle ma…
Browse files Browse the repository at this point in the history
…rionette differences
  • Loading branch information
twalpole committed Jun 23, 2016
1 parent 292e007 commit c08a186
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
18 changes: 13 additions & 5 deletions lib/capybara/driver/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ def unselect_option
def click
raise NotImplementedError
end

def right_click
raise NotImplementedError
end

def double_click
raise NotImplementedError
end

def send_keys(*args)
raise NotImplementedError
end

def hover
raise NotImplementedError
end

def drag_to(element)
raise NotImplementedError
end
Expand All @@ -83,6 +83,14 @@ def disabled?
raise NotImplementedError
end

def readonly?
!!self[:readonly]
end

def multiple?
!!self[:multiple]
end

def path
raise NotSupportedByDriverError, 'Capybara::Driver::Node#path'
end
Expand Down
20 changes: 20 additions & 0 deletions lib/capybara/node/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,26 @@ def disabled?
synchronize { base.disabled? }
end

##
#
# Whether or not the element is readonly.
#
# @return [Boolean] Whether the element is readonly
#
def readonly?
synchronize { base.readonly? }
end

##
#
# Whether or not the element supports multiple results.
#
# @return [Boolean] Whether the element supports multiple results.
#
def multiple?
synchronize { base.multiple? }
end

##
#
# An XPath expression describing where on the page the element can be found
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/rack_test/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def value
end

def set(value)
if (Array === value) && !self[:multiple]
if (Array === value) && !multiple?
raise ArgumentError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
end

Expand Down
5 changes: 2 additions & 3 deletions lib/capybara/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def locate_field(xpath, locator)
filter(:checked, boolean: true) { |node, value| not(value ^ node.checked?) }
filter(:unchecked, boolean: true) { |node, value| (value ^ node.checked?) }
filter(:disabled, default: false, boolean: true, skip_if: :all) { |node, value| not(value ^ node.disabled?) }
filter(:multiple, boolean: true) { |node, value| !(value ^ node[:multiple]) }
filter(:multiple, boolean: true) { |node, value| !(value ^ node.multiple?) }

describe do |options|
desc, states = String.new, []
Expand All @@ -150,7 +150,7 @@ def locate_field(xpath, locator)

filter_set(:_field)

filter(:readonly, boolean: true) { |node, value| not(value ^ node[:readonly]) }
filter(:readonly, boolean: true) { |node, value| not(value ^ node.readonly?) }
filter(:with) { |node, with| node.value == with.to_s }
filter(:type) do |node, type|
if ['textarea', 'select'].include?(type)
Expand All @@ -159,7 +159,6 @@ def locate_field(xpath, locator)
node[:type] == type
end
end
# filter(:multiple, boolean: true) { |node, value| !(value ^ node[:multiple]) }
describe do |options|
desc, states = String.new, []
desc << " of type #{options[:type].inspect}" if options[:type]
Expand Down
20 changes: 12 additions & 8 deletions lib/capybara/selenium/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def [](name)
end

def value
if tag_name == "select" and self[:multiple] and not self[:multiple] == "false"
if tag_name == "select" and multiple?
native.find_elements(:xpath, ".//option").select { |n| n.selected? }.map { |n| n[:value] || n.text }
else
native[:value]
Expand All @@ -38,7 +38,7 @@ def value
def set(value, options={})
tag_name = self.tag_name
type = self[:type]
if (Array === value) && !self[:multiple]
if (Array === value) && !multiple?
raise ArgumentError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
end
if tag_name == 'input' and type == 'radio'
Expand Down Expand Up @@ -131,12 +131,21 @@ def selected?
selected = native.selected?
selected and selected != "false"
end
alias :checked? :selected?

def disabled?
!native.enabled?
end

alias :checked? :selected?
def readonly?
readonly = self[:readonly]
readonly and readonly != "false"
end

def multiple?
multiple = self[:multiple]
multiple and multiple != "false"
end

def find_xpath(locator)
native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
Expand Down Expand Up @@ -175,11 +184,6 @@ def path
end

private
def readonly?
readonly = self[:readonly]
readonly and readonly != "false"
end

# a reference to the select node if this is an option node
def select_node
find_xpath('./ancestor::select').first
Expand Down

0 comments on commit c08a186

Please sign in to comment.