1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00
capybara-webkit/lib/capybara/webkit/node.rb
Matthew Horan 09e19e305f Implement Node#all_text
The #text method in Capybara 2.1 differentiates between visible and
invisible text. This requires that Node implement #all_text.

To maintain backwards compatibility with Capybara 2.0, Node#text has
been aliased to #visible_text.
2013-02-18 20:52:32 -05:00

136 lines
2.5 KiB
Ruby

module Capybara::Webkit
class Node < Capybara::Driver::Node
def visible_text
Capybara::Helpers.normalize_whitespace(invoke("text"))
end
alias_method :text, :visible_text
def all_text
Capybara::Helpers.normalize_whitespace(invoke("allText"))
end
def [](name)
value = invoke("attribute", name)
if name == 'checked' || name == 'disabled' || name == 'multiple'
value == 'true'
else
if invoke("hasAttribute", name) == 'true'
value
else
nil
end
end
end
def value
if multiple_select?
self.find(".//option").select(&:selected?).map(&:value)
else
invoke "value"
end
end
def inner_html
invoke 'getInnerHTML'
end
def inner_html=(value)
invoke 'setInnerHTML', value
end
def set(value)
invoke "set", *[value].flatten
end
def select_option
invoke "selectOption"
end
def unselect_option
select = find("ancestor::select").first
if select.multiple_select?
invoke "unselectOption"
else
raise Capybara::UnselectNotAllowed
end
end
def click
invoke("click")
end
def drag_to(element)
invoke 'dragTo', element.native
end
def tag_name
invoke "tagName"
end
def visible?
invoke("visible") == "true"
end
def selected?
invoke("selected") == "true"
end
def checked?
self['checked']
end
def disabled?
self['disabled']
end
def path
invoke "path"
end
def submit(opts = {})
invoke "submit"
end
def trigger(event)
invoke "trigger", event
end
def find(xpath)
invoke("findWithin", xpath).split(',').map do |native|
self.class.new(driver, native)
end
end
def invoke(name, *args)
if allow_unattached_nodes? || attached?
browser.command "Node", name, native, *args
else
raise Capybara::Webkit::NodeNotAttachedError
end
end
def allow_unattached_nodes?
!automatic_reload?
end
def automatic_reload?
Capybara.respond_to?(:automatic_reload) && Capybara.automatic_reload
end
def attached?
browser.command("Node", "isAttached", native) == "true"
end
def browser
driver.browser
end
def multiple_select?
self.tag_name == "select" && self["multiple"]
end
def ==(other)
invoke("equals", other.native) == "true"
end
end
end