Added preliminary selenium driver (doesn't work)

This commit is contained in:
Jonas Nicklas 2009-11-07 16:30:16 +01:00
parent 80d594a873
commit 9a41cc4ebe
6 changed files with 179 additions and 1 deletions

View File

@ -21,5 +21,6 @@ module Webcat
autoload :Culerity, 'webcat/driver/culerity_driver'
autoload :SafariWatir, 'webcat/driver/safariwatir_driver'
autoload :FireWatir, 'webcat/driver/firewatir_driver'
autoload :Selenium, 'webcat/driver/selenium_driver'
end
end
end

View File

@ -0,0 +1,26 @@
class TCPSocket
def self.wait_for_service_with_timeout(options)
start_time = Time.now
until listening_service?(options)
verbose_wait
if options[:timeout] && (Time.now > start_time + options[:timeout])
raise SocketError.new("Socket did not open within #{options[:timeout]} seconds")
end
end
end
def self.wait_for_service_termination_with_timeout(options)
start_time = Time.now
while listening_service?(options)
verbose_wait
if options[:timeout] && (Time.now > start_time + options[:timeout])
raise SocketError.new("Socket did not terminate within #{options[:timeout]} seconds")
end
end
end
end

View File

@ -0,0 +1,61 @@
module Webcat
module Selenium
class SeleniumRCServer
unless Kernel.respond_to?(:silence_stream)
def silence_stream(stream)
old_stream = stream.dup
stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
stream.sync = true
yield
ensure
stream.reopen(old_stream)
end
end
def booted?
@booted || false
end
def boot
return if booted?
silence_stream(STDOUT) do
remote_control.start :background => true
end
wait
@booted = true
at_exit do
silence_stream(STDOUT) do
remote_control.stop
end
end
end
def remote_control
return @remote_control if @remote_control
@remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 5041, 15)
@remote_control.jar_file = jar_path
return @remote_control
end
def jar_path
File.expand_path("selenium-server.jar", File.dirname(__FILE__))
end
def wait
return true
$stderr.print "==> Waiting for Selenium RC server on port #{Webrat.configuration.selenium_server_port}... "
TCPSocket.wait_for_service_with_timeout(:host => "localhost", :port => 5041, :timeout => 15)
$stderr.print "Ready!\n"
rescue SocketError
$stderr.puts "==> Failed to boot the Selenium RC server... exiting!"
exit
end
end
end
end

Binary file not shown.

View File

@ -0,0 +1,80 @@
require 'selenium/client'
require 'webcat/driver/selenium/rc_server'
require 'webcat/core_ext/tcp_socket'
class Webcat::Driver::Selenium
class Node < Struct.new(:node)
def text
node.text
end
def attribute(name)
value = if name.to_sym == :class
node.class_name
else
node.send(name.to_sym)
end
return value if value and not value.empty?
end
def click
node.click
end
def tag_name
# FIXME: this might be the dumbest way ever of getting the tag name
# there has to be something better...
node.to_xml[/^\s*<([a-z0-9\-\:]+)/, 1]
end
end
attr_reader :app, :rack_server
def self.server
@server ||= Webcat::Selenium::SeleniumRCServer.new
end
def initialize(app)
@app = app
@rack_server = Webcat::Server.new(@app)
@rack_server.boot
self.class.server.boot
end
def visit(path)
browser.open(url(path))
end
def body
browser.html
end
def find(selector)
browser.elements_by_xpath(selector).map { |node| Node.new(node) }
end
private
def url(path)
rack_server.url(path)
end
def browser
unless @_browser
@_browser = Selenium::Client::Driver.new :host => 'localhost',
:port => 4444,
:browser => "*firefox",
:url => rack_server.url('/'),
:timeout_in_second => 10
@_browser.start_new_browser_session
at_exit do
@_browser.close_current_browser_session
end
end
@_browser
end
end

View File

@ -0,0 +1,10 @@
require File.expand_path('spec_helper', File.dirname(__FILE__))
describe Webcat::Driver::Selenium do
before do
@driver = Webcat::Driver::Selenium.new(TestApp)
end
# it_should_behave_like "driver"
# it_should_behave_like "driver with javascript support"
end