2009-12-16 20:55:12 +00:00
|
|
|
class Capybara::Driver::Celerity < Capybara::Driver::Base
|
2010-07-09 18:08:33 +00:00
|
|
|
class Node < Capybara::Driver::Node
|
2009-11-04 23:34:11 +00:00
|
|
|
def text
|
2010-07-15 18:55:12 +00:00
|
|
|
native.text
|
2009-11-04 23:34:11 +00:00
|
|
|
end
|
2009-12-11 21:41:12 +00:00
|
|
|
|
2009-11-07 23:28:32 +00:00
|
|
|
def [](name)
|
2010-11-21 18:00:28 +00:00
|
|
|
value = native.attribute_value(name.to_sym)
|
2010-01-30 19:43:56 +00:00
|
|
|
return value if value and not value.to_s.empty?
|
2009-11-04 23:34:11 +00:00
|
|
|
end
|
2009-11-07 23:28:32 +00:00
|
|
|
|
2010-02-22 12:54:48 +00:00
|
|
|
def value
|
2010-07-15 18:55:12 +00:00
|
|
|
if tag_name == "select" and native.multiple?
|
2010-12-23 23:36:22 +00:00
|
|
|
find(".//option[@selected]").map { |n| if n.has_value? then n.value else n.text end }
|
2010-02-22 12:54:48 +00:00
|
|
|
else
|
2010-11-21 18:00:28 +00:00
|
|
|
native.value
|
2010-02-22 12:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-10 21:48:31 +00:00
|
|
|
def set(value)
|
2010-07-15 18:55:12 +00:00
|
|
|
native.set(value)
|
2009-11-07 23:28:32 +00:00
|
|
|
end
|
2009-12-11 21:41:12 +00:00
|
|
|
|
2010-08-14 11:42:53 +00:00
|
|
|
def select_option
|
|
|
|
native.click
|
2009-11-11 21:54:02 +00:00
|
|
|
end
|
2009-12-11 21:41:12 +00:00
|
|
|
|
2010-08-14 11:42:53 +00:00
|
|
|
def unselect_option
|
|
|
|
unless select_node.native.multiple?
|
|
|
|
raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box."
|
2010-02-20 02:13:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# FIXME: couldn't find a clean way to unselect, so clear and reselect
|
2010-08-14 11:42:53 +00:00
|
|
|
selected_nodes = select_node.find('.//option[@selected]')
|
|
|
|
select_node.native.clear
|
|
|
|
selected_nodes.each { |n| n.click unless n.path == path }
|
2010-02-20 02:13:23 +00:00
|
|
|
end
|
|
|
|
|
2009-11-04 23:42:13 +00:00
|
|
|
def click
|
2010-07-15 18:55:12 +00:00
|
|
|
native.click
|
2009-11-04 23:42:13 +00:00
|
|
|
end
|
2009-12-11 21:41:12 +00:00
|
|
|
|
2009-12-05 18:40:15 +00:00
|
|
|
def drag_to(element)
|
2010-07-15 18:55:12 +00:00
|
|
|
native.fire_event('mousedown')
|
|
|
|
element.native.fire_event('mousemove')
|
|
|
|
element.native.fire_event('mouseup')
|
2009-12-05 18:40:15 +00:00
|
|
|
end
|
2009-12-11 21:41:12 +00:00
|
|
|
|
2009-11-05 14:10:18 +00:00
|
|
|
def tag_name
|
|
|
|
# FIXME: this might be the dumbest way ever of getting the tag name
|
|
|
|
# there has to be something better...
|
2010-07-15 18:55:12 +00:00
|
|
|
native.to_xml[/^\s*<([a-z0-9\-\:]+)/, 1]
|
2009-11-05 14:10:18 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-12-22 20:13:55 +00:00
|
|
|
def visible?
|
2010-07-15 18:55:12 +00:00
|
|
|
native.visible?
|
2009-12-22 20:13:55 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2010-12-23 18:38:44 +00:00
|
|
|
def checked?
|
|
|
|
native.checked?
|
|
|
|
rescue # https://github.com/langalex/culerity/issues/issue/33
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def selected?
|
|
|
|
native.selected?
|
|
|
|
rescue # https://github.com/langalex/culerity/issues/issue/33
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2009-12-24 05:41:34 +00:00
|
|
|
def path
|
2010-07-15 18:55:12 +00:00
|
|
|
native.xpath
|
2009-12-24 05:41:34 +00:00
|
|
|
end
|
2010-06-24 13:51:44 +00:00
|
|
|
|
2010-02-15 13:54:11 +00:00
|
|
|
def trigger(event)
|
2010-07-15 18:55:12 +00:00
|
|
|
native.fire_event(event.to_s)
|
2010-02-15 13:54:11 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2010-07-09 23:58:34 +00:00
|
|
|
def find(locator)
|
2010-07-15 18:55:12 +00:00
|
|
|
noko_node = Nokogiri::HTML(driver.body).xpath(native.xpath).first
|
2010-02-26 17:38:22 +00:00
|
|
|
all_nodes = noko_node.xpath(locator).map { |n| n.path }.join(' | ')
|
2010-07-10 11:00:34 +00:00
|
|
|
if all_nodes.empty? then [] else driver.find(all_nodes) end
|
2010-02-26 17:38:22 +00:00
|
|
|
end
|
|
|
|
|
2010-08-14 11:42:53 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# a reference to the select node if this is an option node
|
|
|
|
def select_node
|
|
|
|
find('./ancestor::select').first
|
|
|
|
end
|
|
|
|
|
2010-12-23 23:36:22 +00:00
|
|
|
def has_value?
|
|
|
|
native.object.hasAttribute('value')
|
|
|
|
end
|
2009-11-04 23:34:11 +00:00
|
|
|
end
|
2009-12-11 21:41:12 +00:00
|
|
|
|
2010-09-15 20:38:38 +00:00
|
|
|
attr_reader :app, :rack_server, :options
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2010-09-15 20:38:38 +00:00
|
|
|
def initialize(app, options={})
|
2009-11-04 22:00:05 +00:00
|
|
|
@app = app
|
2010-09-15 20:38:38 +00:00
|
|
|
@options = options
|
2010-01-17 16:40:26 +00:00
|
|
|
@rack_server = Capybara::Server.new(@app)
|
|
|
|
@rack_server.boot if Capybara.run_server
|
2009-11-04 22:00:05 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-11-04 22:05:11 +00:00
|
|
|
def visit(path)
|
2009-11-04 22:00:05 +00:00
|
|
|
browser.goto(url(path))
|
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-12-18 04:10:42 +00:00
|
|
|
def current_url
|
|
|
|
browser.url
|
|
|
|
end
|
2010-06-24 13:51:44 +00:00
|
|
|
|
2010-01-01 22:58:59 +00:00
|
|
|
def source
|
|
|
|
browser.html
|
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-12-18 17:50:55 +00:00
|
|
|
def body
|
2010-01-01 22:58:59 +00:00
|
|
|
browser.document.as_xml
|
2009-11-04 22:00:05 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-12-18 17:50:55 +00:00
|
|
|
def response_headers
|
|
|
|
browser.response_headers
|
|
|
|
end
|
2010-06-24 13:51:44 +00:00
|
|
|
|
2010-06-07 03:14:40 +00:00
|
|
|
def status_code
|
2010-06-07 02:57:14 +00:00
|
|
|
browser.status_code
|
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-11-04 23:34:11 +00:00
|
|
|
def find(selector)
|
2009-11-17 22:52:22 +00:00
|
|
|
browser.elements_by_xpath(selector).map { |node| Node.new(self, node) }
|
2009-11-04 23:34:11 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-12-12 20:46:08 +00:00
|
|
|
def wait?; true; end
|
2009-11-04 22:00:05 +00:00
|
|
|
|
2010-06-29 20:41:33 +00:00
|
|
|
def execute_script(script)
|
|
|
|
browser.execute_script script
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2009-12-11 21:41:12 +00:00
|
|
|
def evaluate_script(script)
|
|
|
|
browser.execute_script "#{script}"
|
|
|
|
end
|
|
|
|
|
2010-01-01 19:13:54 +00:00
|
|
|
def browser
|
|
|
|
unless @_browser
|
|
|
|
require 'celerity'
|
2010-11-17 08:36:50 +00:00
|
|
|
@_browser = ::Celerity::Browser.new(options)
|
2010-01-01 19:13:54 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2010-01-01 19:13:54 +00:00
|
|
|
@_browser
|
|
|
|
end
|
|
|
|
|
2010-07-29 13:25:45 +00:00
|
|
|
def reset!
|
2010-06-24 13:51:44 +00:00
|
|
|
browser.clear_cookies
|
|
|
|
end
|
|
|
|
|
2009-11-04 22:00:05 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def url(path)
|
2010-01-17 16:40:26 +00:00
|
|
|
rack_server.url(path)
|
2009-11-04 22:00:05 +00:00
|
|
|
end
|
2009-12-11 21:41:12 +00:00
|
|
|
|
2009-11-07 23:28:32 +00:00
|
|
|
end
|