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/driver/webkit/node.rb

135 lines
2.6 KiB
Ruby
Raw Normal View History

2011-02-18 22:53:06 -05:00
class Capybara::Driver::Webkit
class Node < Capybara::Driver::Node
class ElementNotDisplayedError < StandardError
end
NBSP = "\xC2\xA0"
NBSP.force_encoding("UTF-8") if NBSP.respond_to?(:force_encoding)
2011-02-18 22:53:06 -05:00
def text
invoke("text").gsub(NBSP, ' ').gsub(/\s+/u, ' ').strip
2011-02-18 22:53:06 -05:00
end
def [](name)
value = invoke("attribute", name)
if name == 'checked' || name == 'disabled'
value == 'true'
else
value
end
2011-02-18 22:53:06 -05:00
end
def value
2011-02-26 17:51:47 -05:00
if multiple_select?
self.find(".//option").select do |option|
2011-02-26 17:51:47 -05:00
option["selected"] == "selected"
end.map do |option|
option.value
end
else
invoke "value"
end
2011-02-18 22:53:06 -05:00
end
def set(value)
2011-02-26 16:26:22 -05:00
invoke "set", value
2011-02-18 22:53:06 -05:00
end
def select_option
check_visibility(self)
2011-02-26 17:51:47 -05:00
invoke "selectOption"
2011-02-18 22:53:06 -05:00
end
def unselect_option
2011-07-29 11:40:48 -04:00
select = find("ancestor::select").first
if select.multiple_select?
invoke "unselectOption"
else
raise Capybara::UnselectNotAllowed
end
2011-02-18 22:53:06 -05:00
end
def click
check_visibility(self)
invoke "click"
2011-02-18 22:53:06 -05:00
end
def drag_to(element)
check_visibility(self)
check_visibility(element)
2011-04-23 07:22:16 -04:00
invoke 'dragTo', element.native
2011-02-18 22:53:06 -05:00
end
def tag_name
2011-02-26 14:55:40 -05:00
invoke "tagName"
2011-02-18 22:53:06 -05:00
end
def visible?
2011-02-26 16:20:05 -05:00
invoke("visible") == "true"
2011-02-18 22:53:06 -05:00
end
2011-07-29 11:18:51 -04:00
def selected?
invoke("selected") == "true"
end
2011-06-26 13:47:06 -04:00
def checked?
self['checked']
end
def disabled?
self['disabled']
end
2011-07-29 11:40:48 -04:00
2011-02-18 22:53:06 -05:00
def path
2011-09-24 18:16:36 -04:00
invoke "path"
2011-02-18 22:53:06 -05:00
end
def submit(opts = {})
invoke "submit"
end
2011-02-18 22:53:06 -05:00
def trigger(event)
2011-02-26 15:50:45 -05:00
invoke "trigger", event
2011-02-18 22:53:06 -05:00
end
2011-02-25 00:15:08 -05:00
2011-02-26 16:46:59 -05:00
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?
2011-09-30 17:13:54 -04:00
browser.command "Node", name, native, *args
else
raise Capybara::Driver::Webkit::NodeNotAttachedError
end
end
def allow_unattached_nodes?
!automatic_reload?
end
def automatic_reload?
Capybara.respond_to?(:automatic_reload) && Capybara.automatic_reload
end
2011-09-30 17:13:54 -04:00
def attached?
browser.command("Node", "isAttached", native) == "true"
2011-02-25 00:15:08 -05:00
end
def browser
driver.browser
end
2011-02-26 17:51:47 -05:00
def multiple_select?
self.tag_name == "select" && self["multiple"] == "multiple"
end
def check_visibility(element)
raise(ElementNotDisplayedError, "This element is not visible so it may not be interacted with") unless element.visible?
end
2011-02-18 22:53:06 -05:00
end
end