2011-02-18 22:53:06 -05:00
|
|
|
class Capybara::Driver::Webkit
|
|
|
|
class Node < Capybara::Driver::Node
|
2012-01-18 16:14:47 -05:00
|
|
|
|
|
|
|
class ElementNotDisplayedError < StandardError
|
|
|
|
end
|
|
|
|
|
2011-09-23 11:57:09 -04:00
|
|
|
NBSP = "\xC2\xA0"
|
|
|
|
NBSP.force_encoding("UTF-8") if NBSP.respond_to?(:force_encoding)
|
|
|
|
|
2011-02-18 22:53:06 -05:00
|
|
|
def text
|
2011-09-23 11:57:09 -04:00
|
|
|
invoke("text").gsub(NBSP, ' ').gsub(/\s+/u, ' ').strip
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](name)
|
2011-03-11 11:25:06 -05:00
|
|
|
value = invoke("attribute", name)
|
2011-05-23 09:01:12 -04:00
|
|
|
if name == 'checked' || name == 'disabled'
|
2011-03-11 11:25:06 -05:00
|
|
|
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?
|
2011-02-26 17:52:28 -05:00
|
|
|
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
|
2012-01-19 17:27:30 -05:00
|
|
|
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
|
2012-01-19 17:27:30 -05:00
|
|
|
check_visibility(self)
|
|
|
|
invoke "click"
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def drag_to(element)
|
2012-01-19 17:27:30 -05:00
|
|
|
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
|
|
|
|
|
2011-05-23 09:01:12 -04:00
|
|
|
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
|
|
|
|
|
2011-10-22 19:37:46 -04:00
|
|
|
def submit(opts = {})
|
2011-09-24 18:16:57 -04:00
|
|
|
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
|
|
|
|
|
2011-02-25 23:39:29 -05:00
|
|
|
def invoke(name, *args)
|
2011-09-30 17:25:22 -04:00
|
|
|
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
|
|
|
|
|
2011-09-30 17:25:22 -04:00
|
|
|
def allow_unattached_nodes?
|
2011-09-30 17:55:28 -04:00
|
|
|
!automatic_reload?
|
|
|
|
end
|
|
|
|
|
|
|
|
def automatic_reload?
|
|
|
|
Capybara.respond_to?(:automatic_reload) && Capybara.automatic_reload
|
2011-09-30 17:25:22 -04:00
|
|
|
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
|
2012-01-19 17:27:30 -05:00
|
|
|
|
|
|
|
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
|