Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 129ad7711b2dedbe3cd0b0a4c8fd001af3ccbb24 @bernerdschaefer bernerdschaefer committed May 3, 2010
10 bin/akephalos
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# vim:set filetype=ruby:
+
+require 'pathname'
+
+root = Pathname(__FILE__).expand_path.dirname.parent
+jruby = root + "src/jruby-complete-1.4.0.jar"
+server = root + "lib/akephalos/server"
+
+exec "java -jar #{jruby} -r#{server} -e 'Akephalos::Server.start!'"
2 lib/akephalos.rb
@@ -0,0 +1,2 @@
+module Akephalos
+end
160 lib/akephalos/drivers/capybara.rb
@@ -0,0 +1,160 @@
+class Capybara::Driver::Akephalos < Capybara::Driver::Base
+
+ class Node < Capybara::Node
+
+ def [](name)
+ name = name.to_s
+ case name
+ when 'checked'
+ node.isChecked
+ else
+ node[name.to_s]
+ end
+ end
+
+ def text
+ node.asText
+ end
+
+ def value
+ if tag_name == "select" && self[:multiple]
+ node.getSelectedOptions.map { |n| n.asText }
+ elsif tag_name == "select"
+ node.getSelectedOptions.first.asText
+ else
+ super
+ end
+ end
+
+ def set(value)
+ if tag_name == 'textarea'
+ node.setText(value.to_s)
+ elsif tag_name == 'input' and %w(text password hidden file).include?(type)
+ node.setValueAttribute(value.to_s)
+ elsif tag_name == 'input' and type == 'radio'
+ node.click
+ elsif tag_name == 'input' and type == 'checkbox'
+ node.click
+ end
+ end
+
+ def select(option)
+ result = node.select_option(option)
+
+ if result == nil
+ options = node.getOptions.map { |opt| opt.asText }.join(", ")
+ raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
+ else
+ result
+ end
+ end
+
+ def unselect(option)
+ unless self[:multiple]
+ raise Capybara::UnselectNotAllowed, "Cannot unselect option '#{option}' from single select box."
+ end
+
+ result = node.unselect_option(option)
+
+ if result == nil
+ options = node.getOptions.map { |opt| opt.asText }.join(", ")
+ raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
+ else
+ result
+ end
+ end
+
+ def trigger(event)
+ node.fire_event(event.to_s)
+ end
+
+ def tag_name
+ node.getNodeName
+ end
+
+ def visible?
+ node.isDisplayed
+ end
+
+ def drag_to(element)
+ node.fire_event('mousedown')
+ element.node.fire_event('mousemove')
+ element.node.fire_event('mouseup')
+ end
+
+ def click
+ node.click
+ end
+
+ private
+
+ def all_unfiltered(selector)
+ node.getByXPath(selector).map { |n| self.class.new(driver, n) }
+ end
+
+ def type
+ node[:type]
+ end
+ end
+
+ attr_reader :app, :rack_server
+
+ def self.driver
+ if RUBY_PLATFORM == "java"
+ require '/usr/local/projects/personal/htmlunit-ruby/jruby'
+ @driver ||= WebClient.new
+ else
+ @driver ||= begin
+ uri = "drbunix:///tmp/htmlunit.sock"
+ DRb.start_service
+ client_class = DRbObject.new_with_uri(uri)
+ at_exit { client_class.cleanup! }
+ client_class.new
+ end
+ end
+ end
+
+ def initialize(app)
+ @app = app
+ @rack_server = Capybara::Server.new(@app)
+ @rack_server.boot if Capybara.run_server
+ end
+
+ def visit(path)
+ browser.visit(url(path))
+ end
+
+ def source
+ page.source
+ end
+ alias body source
+
+ def current_url
+ page.current_url
+ end
+
+ def find(selector)
+ page.find(selector).map { |node| Node.new(self, node) }
+ end
+
+ def evaluate_script(script)
+ page.execute_script script
+ end
+
+ def page
+ browser.page
+ end
+
+ def browser
+ self.class.driver
+ end
+
+ def wait?; true end
+
+private
+
+ def url(path)
+ rack_server.url(path)
+ end
+
+end
33 lib/akephalos/htmlunit.rb
@@ -0,0 +1,33 @@
+require "pathname"
+require "java"
+
+$:.unshift((Pathname(__FILE__).dirname + "../../src").expand_path)
+require "commons-codec-1.4.jar"
+require "commons-collections-3.2.1.jar"
+require "commons-httpclient-3.1.jar"
+require "commons-io-1.4.jar"
+require "commons-lang-2.4.jar"
+require "commons-logging-1.1.1.jar"
+require "cssparser-0.9.5.jar"
+require "htmlunit-2.6.jar"
+require "htmlunit-core-js-2.6.jar"
+require "nekohtml-1.9.13.jar"
+require "sac-1.3.jar"
+require "serializer-2.7.1.jar"
+require "xalan-2.7.1.jar"
+require "xercesImpl-2.9.1.jar"
+require "xml-apis-1.3.04.jar"
+
+java_import 'java.io.StringWriter'
+java_import 'java.io.PrintWriter'
+java_import "com.gargoylesoftware.htmlunit.WebClient"
+java_import "com.gargoylesoftware.htmlunit.html.HtmlPage"
+
+com.gargoylesoftware.htmlunit.BrowserVersion.setDefault(
+ com.gargoylesoftware.htmlunit.BrowserVersion::FIREFOX_3
+)
+
+require Pathname(__FILE__).dirname + "htmlunit/html_element"
+require Pathname(__FILE__).dirname + "htmlunit/html_page"
+require Pathname(__FILE__).dirname + "htmlunit/html_select"
+require Pathname(__FILE__).dirname + "htmlunit/web_client"
11 lib/akephalos/htmlunit/html_element.rb
@@ -0,0 +1,11 @@
+module Akephalos
+ module Htmlunit
+ module HtmlElement
+ def [](name)
+ hasAttribute(name.to_s) ? getAttribute(name.to_s) : nil
+ end
+
+ com.gargoylesoftware.htmlunit.html.HtmlElement.send(:include, self)
+ end
+ end
+end
27 lib/akephalos/htmlunit/html_page.rb
@@ -0,0 +1,27 @@
+module Akephalos
+ module Htmlunit
+ module HtmlPage
+ def source
+ writer = StringWriter.new
+ printer = PrintWriter.new(writer)
+ printXml('', printer)
+ printer.close
+ writer.toString
+ end
+
+ def current_url
+ getWebResponse.getRequestSettings.getUrl.toString
+ end
+
+ def find(selector)
+ getByXPath(selector)
+ end
+
+ def execute_script(script)
+ executeJavaScript(script).getJavaScriptResult
+ end
+
+ com.gargoylesoftware.htmlunit.html.HtmlPage.send(:include, self)
+ end
+ end
+end
19 lib/akephalos/htmlunit/html_select.rb
@@ -0,0 +1,19 @@
+module Akephalos
+ module Htmlunit
+ module HtmlSelect
+ def select_option(option)
+ opt = getOptions.detect { |o| o.asText == option }
+
+ opt && opt.setSelected(true)
+ end
+
+ def unselect_option(option)
+ opt = getOptions.detect { |o| o.asText == option }
+
+ opt && opt.setSelected(false)
+ end
+
+ com.gargoylesoftware.htmlunit.html.HtmlSelect.send(:include, self)
+ end
+ end
+end
16 lib/akephalos/htmlunit/web_client.rb
@@ -0,0 +1,16 @@
+module Akephalos
+ module Htmlunit
+ module WebClient
+
+ def visit(url)
+ getPage(url)
+ end
+
+ def page
+ getCurrentWindow.getEnclosedPage
+ end
+
+ com.gargoylesoftware.htmlunit.WebClient.send(:include, self)
+ end
+ end
+end
47 lib/akephalos/server.rb
@@ -0,0 +1,47 @@
+require "pathname"
+require "drb/drb"
+require Pathname(__FILE__).expand_path.dirname + "htmlunit"
+
+[
+ java.net.URL,
+ java.util.List,
+ com.gargoylesoftware.htmlunit.html.DomNode,
+ com.gargoylesoftware.htmlunit.html.DomElement,
+ com.gargoylesoftware.htmlunit.html.HtmlAnchor,
+ com.gargoylesoftware.htmlunit.html.HtmlElement,
+ com.gargoylesoftware.htmlunit.html.HtmlPage,
+ org.w3c.dom.Node,
+ org.w3c.dom.NamedNodeMap,
+ com.gargoylesoftware.htmlunit.WebClient,
+ com.gargoylesoftware.htmlunit.WebResponse,
+ com.gargoylesoftware.htmlunit.WebRequestSettings
+].each { |klass| klass.send(:include, DRbUndumped) }
+
+class WebClient
+ def cleanup!
+ exit
+ end
+
+ def page
+ @page = getCurrentWindow.getEnclosedPage
+ end
+end
+
+class HtmlPage
+ def find(selector)
+ nodes = getByXPath(selector)
+ (@nodes ||= []).push(*nodes)
+ nodes
+ end
+end
+
+module Akephalos
+ class Server
+ def self.start!
+ puts "Starting Akephalos::Server"
+ client = WebClient.new
+ DRb.start_service("drbunix:///tmp/htmlunit.sock", client)
+ DRb.thread.join
+ end
+ end
+end
BIN src/commons-codec-1.4.jar
Binary file not shown.
BIN src/commons-collections-3.2.1.jar
Binary file not shown.
BIN src/commons-httpclient-3.1.jar
Binary file not shown.
BIN src/commons-io-1.4.jar
Binary file not shown.
BIN src/commons-lang-2.4.jar
Binary file not shown.
BIN src/commons-logging-1.1.1.jar
Binary file not shown.
BIN src/cssparser-0.9.5.jar
Binary file not shown.
BIN src/htmlunit-2.6.jar
Binary file not shown.
BIN src/htmlunit-core-js-2.6.jar
Binary file not shown.
BIN src/jruby-complete-1.4.0.jar
Binary file not shown.
BIN src/nekohtml-1.9.13.jar
Binary file not shown.
BIN src/sac-1.3.jar
Binary file not shown.
BIN src/serializer-2.7.1.jar
Binary file not shown.
BIN src/xalan-2.7.1.jar
Binary file not shown.
BIN src/xercesImpl-2.9.1.jar
Binary file not shown.
BIN src/xml-apis-1.3.04.jar
Binary file not shown.

0 comments on commit 129ad77

Please sign in to comment.