Cleaning up Selenium support. Mongrel and Selenium servers start/shutdown automatically

This commit is contained in:
Bryan Helmkamp 2008-11-23 19:46:37 -05:00
parent e6cb40b66e
commit da68aa8bce
6 changed files with 92 additions and 33 deletions

View File

@ -29,7 +29,3 @@ end
Webrat.require_xml Webrat.require_xml
require "webrat/core" require "webrat/core"
# TODO: This is probably not a good idea.
# Probably better for webrat users to require "webrat/rails" etc. directly
require "webrat/rails" if defined?(RAILS_ENV)

View File

@ -45,7 +45,8 @@ module Webrat
:set_hidden_field, :submit_form, :set_hidden_field, :submit_form,
:request_page, :current_dom, :request_page, :current_dom,
:selects_date, :selects_time, :selects_datetime, :selects_date, :selects_time, :selects_datetime,
:select_date, :select_time, :select_datetime :select_date, :select_time, :select_datetime,
:wait_for_page_to_load
end end

View File

@ -1,4 +1,4 @@
require "webrat/core" require "webrat"
require "cgi" require "cgi"
gem "extlib" gem "extlib"

View File

@ -1,3 +1,5 @@
require "webrat"
module Webrat module Webrat
class RailsSession < Session #:nodoc: class RailsSession < Session #:nodoc:

View File

@ -1,3 +1,4 @@
require "webrat"
gem "selenium-client", ">=1.2.9" gem "selenium-client", ">=1.2.9"
require "selenium/client" require "selenium/client"
require "webrat/selenium/selenium_session" require "webrat/selenium/selenium_session"
@ -13,19 +14,46 @@ module Webrat
end end
def self.start_selenium_server def self.start_selenium_server
remote_control = Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444, 5) remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444, 5)
remote_control.jar_file = File.expand_path(__FILE__ + "../../../../vendor/selenium-server.jar") remote_control.jar_file = File.expand_path(__FILE__ + "../../../../vendor/selenium-server.jar")
remote_control.start :background => true remote_control.start :background => true
puts "Waiting for Remote Control to be up and running..."
TCPSocket.wait_for_service :host => "0.0.0.0", :port => 4444 TCPSocket.wait_for_service :host => "0.0.0.0", :port => 4444
puts "Selenium Remote Control at 0.0.0.0:4444 ready"
end end
def self.stop_selenium_server def self.stop_selenium_server
puts "Stopping Selenium Remote Control running at 0.0.0.0:4444..." remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444, 5)
remote_control = Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444, 5)
remote_control.stop remote_control.stop
puts "Stopped Selenium Remote Control running at 0.0.0.0:4444"
end end
def self.start_app_server
pid_file = File.expand_path(RAILS_ROOT + "/tmp/pids/mongrel_selenium.pid")
system("mongrel_rails start -d --chdir=#{RAILS_ROOT} --port=3001 --environment=selenium --pid #{pid_file} &")
TCPSocket.wait_for_service :host => "0.0.0.0", :port => 3001
end
def self.stop_app_server
pid_file = File.expand_path(RAILS_ROOT + "/tmp/pids/mongrel_selenium.pid")
system "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"
end
module Selenium
module Rails
class World < ::ActionController::IntegrationTest
def initialize #:nodoc:
@_result = Test::Unit::TestResult.new
end
end
end
end
end
module ::ActionController
module Integration
class Session #:nodoc:
include Webrat::Methods
end
end
end end

View File

@ -1,27 +1,26 @@
module Webrat module Webrat
class SeleniumSession class SeleniumSession
def initialize(selenium_driver) #:nodoc: def initialize(*args) # :nodoc:
@selenium = selenium_driver
extend_selenium extend_selenium
define_location_strategies define_location_strategies
end end
def visit(url) def visit(url)
@selenium.open(url) selenium.open(url)
end end
alias_method :visits, :visit alias_method :visits, :visit
def fill_in(field_identifier, options) def fill_in(field_identifier, options)
locator = "webrat=#{Regexp.escape(field_identifier)}" locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.type(locator, "#{options[:with]}") selenium.type(locator, "#{options[:with]}")
end end
alias_method :fills_in, :fill_in alias_method :fills_in, :fill_in
def response_body #:nodoc: def response_body #:nodoc:
@selenium.get_html_source selenium.get_html_source
end end
def click_button(button_text_or_regexp = nil, options = {}) def click_button(button_text_or_regexp = nil, options = {})
@ -31,7 +30,7 @@ module Webrat
pattern = adjust_if_regexp(button_text_or_regexp) pattern = adjust_if_regexp(button_text_or_regexp)
end end
pattern ||= '*' pattern ||= '*'
@selenium.click("button=#{pattern}") selenium.click("button=#{pattern}")
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
@ -39,14 +38,14 @@ module Webrat
def click_link(link_text_or_regexp, options = {}) def click_link(link_text_or_regexp, options = {})
pattern = adjust_if_regexp(link_text_or_regexp) pattern = adjust_if_regexp(link_text_or_regexp)
@selenium.click("webratlink=#{pattern}") selenium.click("webratlink=#{pattern}")
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
alias_method :clicks_link, :click_link alias_method :clicks_link, :click_link
def click_link_within(selector, link_text, options = {}) def click_link_within(selector, link_text, options = {})
@selenium.click("webratlinkwithin=#{selector}|#{link_text}") selenium.click("webratlinkwithin=#{selector}|#{link_text}")
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
@ -63,15 +62,15 @@ module Webrat
end end
def wait_for_page_to_load(timeout = 15000) def wait_for_page_to_load(timeout = 15000)
@selenium.wait_for_page_to_load(timeout) selenium.wait_for_page_to_load(timeout)
end end
def wait_for_ajax(timeout = 15000) def wait_for_ajax(timeout = 15000)
@selenium.wait_for_condition "Ajax.activeRequestCount == 0", timeout selenium.wait_for_condition "Ajax.activeRequestCount == 0", timeout
end end
def wait_for_effects(timeout = 15000) def wait_for_effects(timeout = 15000)
@selenium.wait_for_condition "window.Effect.Queue.size() == 0", timeout selenium.wait_for_condition "window.Effect.Queue.size() == 0", timeout
end end
def wait_for_ajax_and_effects def wait_for_ajax_and_effects
@ -87,48 +86,81 @@ module Webrat
else else
select_locator = "webratselectwithoption=#{option_text}" select_locator = "webratselectwithoption=#{option_text}"
end end
@selenium.select(select_locator, option_text) selenium.select(select_locator, option_text)
end end
alias_method :selects, :select alias_method :selects, :select
def choose(label_text) def choose(label_text)
@selenium.click("webrat=#{label_text}") selenium.click("webrat=#{label_text}")
end end
alias_method :chooses, :choose alias_method :chooses, :choose
def check(label_text) def check(label_text)
@selenium.check("webrat=#{label_text}") selenium.check("webrat=#{label_text}")
end end
alias_method :checks, :check alias_method :checks, :check
def is_ordered(*args) #:nodoc: def is_ordered(*args) #:nodoc:
@selenium.is_ordered(*args) selenium.is_ordered(*args)
end end
def dragdrop(*args) #:nodoc: def dragdrop(*args) #:nodoc:
@selenium.dragdrop(*args) selenium.dragdrop(*args)
end end
def fire_event(field_identifier, event) def fire_event(field_identifier, event)
locator = "webrat=#{Regexp.escape(field_identifier)}" locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.fire_event(locator, "#{event}") selenium.fire_event(locator, "#{event}")
end end
def key_down(field_identifier, key_code) def key_down(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}" locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.key_down(locator, key_code) selenium.key_down(locator, key_code)
end end
def key_up(field_identifier, key_code) def key_up(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}" locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.key_up(locator, key_code) selenium.key_up(locator, key_code)
end
def browser
return $browser if $browser
setup
$browser
end end
protected protected
def setup
silence_stream(STDOUT) do
Webrat.start_selenium_server
Webrat.start_app_server
end
$browser = ::Selenium::Client::Driver.new("localhost", 4444, "*firefox", "http://0.0.0.0:3001")
$browser.set_speed(0)
$browser.start
teardown_at_exit
end
def selenium
browser
end
def teardown_at_exit
at_exit do
silence_stream(STDOUT) do
$browser.stop
Webrat.stop_app_server
Webrat.stop_selenium_server
end
end
end
def adjust_if_regexp(text_or_regexp) #:nodoc: def adjust_if_regexp(text_or_regexp) #:nodoc:
if text_or_regexp.is_a?(Regexp) if text_or_regexp.is_a?(Regexp)
"evalregex:#{text_or_regexp.inspect}" "evalregex:#{text_or_regexp.inspect}"
@ -140,14 +172,14 @@ module Webrat
def extend_selenium #:nodoc: def extend_selenium #:nodoc:
extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js") extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
extenions_js = File.read(extensions_file) extenions_js = File.read(extensions_file)
@selenium.get_eval(extenions_js) selenium.get_eval(extenions_js)
end end
def define_location_strategies #:nodoc: def define_location_strategies #:nodoc:
Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file| Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
strategy_js = File.read(file) strategy_js = File.read(file)
strategy_name = File.basename(file, '.js') strategy_name = File.basename(file, '.js')
@selenium.add_location_strategy(strategy_name, strategy_js) selenium.add_location_strategy(strategy_name, strategy_js)
end end
end end
end end