teamcapybara--capybara/lib/capybara/driver/celerity_driver.rb

165 lines
3.2 KiB
Ruby
Raw Normal View History

class Capybara::Driver::Celerity < Capybara::Driver::Base
class Node < Capybara::Driver::Node
2009-11-04 23:34:11 +00:00
def text
native.text
2009-11-04 23:34:11 +00:00
end
2009-11-07 23:28:32 +00:00
def [](name)
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
def value
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 }
else
native.value
end
end
2009-11-10 21:48:31 +00:00
def set(value)
native.set(value)
2009-11-07 23:28:32 +00:00
end
def select_option
native.click
2009-11-11 21:54:02 +00:00
end
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
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
native.click
2009-11-04 23:42:13 +00:00
end
def drag_to(element)
native.fire_event('mousedown')
element.native.fire_event('mousemove')
element.native.fire_event('mouseup')
end
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...
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?
native.visible?
2009-12-22 20:13:55 +00:00
end
2010-01-01 21:46:05 +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
def path
native.xpath
end
2010-02-15 13:54:11 +00:00
def trigger(event)
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)
noko_node = Nokogiri::HTML(driver.body).xpath(native.xpath).first
all_nodes = noko_node.xpath(locator).map { |n| n.path }.join(' | ')
if all_nodes.empty? then [] else driver.find(all_nodes) end
end
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
attr_reader :app, :rack_server, :options
2010-01-01 21:46:05 +00:00
def initialize(app, options={})
2009-11-04 22:00:05 +00:00
@app = app
@options = options
@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
def current_url
browser.url
end
def source
browser.html
end
2010-01-01 21:46:05 +00:00
def body
browser.document.as_xml
2009-11-04 22:00:05 +00:00
end
2010-01-01 21:46:05 +00:00
def response_headers
browser.response_headers
end
def status_code
browser.status_code
end
2010-01-01 21:46:05 +00:00
2009-11-04 23:34:11 +00:00
def find(selector)
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
def wait?; true; end
2009-11-04 22:00:05 +00:00
def execute_script(script)
browser.execute_script script
nil
end
def evaluate_script(script)
browser.execute_script "#{script}"
end
2010-01-01 19:13:54 +00:00
def browser
unless @_browser
require 'celerity'
@_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!
browser.clear_cookies
end
2009-11-04 22:00:05 +00:00
private
def url(path)
rack_server.url(path)
2009-11-04 22:00:05 +00:00
end
2009-11-07 23:28:32 +00:00
end