Skip to content

Commit

Permalink
changed selenium_port to application_port to reflect what the variabl…
Browse files Browse the repository at this point in the history
…e is used for ...
  • Loading branch information
cornel.borcean committed Jan 12, 2009
1 parent 9825aee commit 33d2cdc
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 80 deletions.
30 changes: 15 additions & 15 deletions lib/webrat/core/configuration.rb
@@ -1,60 +1,60 @@
module Webrat

# Configures Webrat. If this is not done, Webrat will be created
# with all of the default settings.
# with all of the default settings.
def self.configure(configuration = Webrat::Configuration.new)
yield configuration if block_given?
@@configuration = configuration
end

def self.configuration # :nodoc:
@@configuration ||= Webrat::Configuration.new
end

# Webrat can be configured using the Webrat.configure method. For example:
#
#
# Webrat.configure do |config|
# config.parse_with_nokogiri = false
# end
class Configuration

# Should XHTML be parsed with Nokogiri? Defaults to true, except on JRuby. When false, Hpricot and REXML are used
attr_writer :parse_with_nokogiri

# Webrat's mode, set automatically when requiring webrat/rails, webrat/merb, etc.
attr_accessor :mode # :nodoc:

# Save and open pages with error status codes (500-599) in a browser? Defualts to true.
attr_writer :open_error_files

# Which environment should the selenium tests be run in? Defaults to selenium.
attr_accessor :selenium_environment

# Which port should the selenium tests be run on? Defaults to 3001.
attr_accessor :selenium_port
attr_accessor :application_port

def initialize # :nodoc:
self.open_error_files = true
self.parse_with_nokogiri = !Webrat.on_java?
self.selenium_environment = :selenium
self.selenium_port = 3001
self.application_port = 3001
end

def parse_with_nokogiri? #:nodoc:
@parse_with_nokogiri ? true : false
end

def open_error_files? #:nodoc:
@open_error_files ? true : false
end

# Allows setting of webrat's mode, valid modes are:
# :rails, :selenium, :rack, :sinatra, :mechanize, :merb
def mode=(mode)
@mode = mode
require("webrat/#{mode}")
end

end

end
18 changes: 9 additions & 9 deletions lib/webrat/selenium.rb
Expand Up @@ -5,41 +5,41 @@
require "webrat/selenium/matchers"

module Webrat

def self.with_selenium_server #:nodoc:
start_selenium_server
yield
stop_selenium_server
end

def self.start_selenium_server #:nodoc:
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.start :background => true
TCPSocket.wait_for_service :host => "0.0.0.0", :port => 4444
end

def self.stop_selenium_server #:nodoc:
remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444, 5)
remote_control.stop
end

def self.start_app_server #:nodoc:
pid_file = File.expand_path(RAILS_ROOT + "/tmp/pids/mongrel_selenium.pid")
system("mongrel_rails start -d --chdir=#{RAILS_ROOT} --port=#{Webrat.configuration.selenium_port} --environment=#{Webrat.configuration.selenium_environment} --pid #{pid_file} &")
TCPSocket.wait_for_service :host => "0.0.0.0", :port => Webrat.configuration.selenium_port.to_i
system("mongrel_rails start -d --chdir=#{RAILS_ROOT} --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.selenium_environment} --pid #{pid_file} &")
TCPSocket.wait_for_service :host => "0.0.0.0", :port => Webrat.configuration.application_port.to_i
end

def self.stop_app_server #:nodoc:
pid_file = File.expand_path(RAILS_ROOT + "/tmp/pids/mongrel_selenium.pid")
system "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"
end

# To use Webrat's Selenium support, you'll need the selenium-client gem installed.
# Activate it with (for example, in your <tt>env.rb</tt>):
#
# require "webrat"
#
#
# Webrat.configure do |config|
# config.mode = :selenium
# end
Expand Down
88 changes: 44 additions & 44 deletions lib/webrat/selenium/selenium_session.rb
Expand Up @@ -3,56 +3,56 @@
module Webrat
class TimeoutError < WebratError
end

class SeleniumResponse
attr_reader :body
attr_reader :session

def initialize(session, body)
@session = session
@body = body
end

def selenium
session.selenium
end
end

class SeleniumSession
include Webrat::SaveAndOpenPage

def initialize(*args) # :nodoc:
end

def simulate
end

def automate
yield
end

def visit(url)
selenium.open(url)
end

webrat_deprecate :visits, :visit

def fill_in(field_identifier, options)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.wait_for_element locator, 5
selenium.type(locator, "#{options[:with]}")
end

webrat_deprecate :fills_in, :fill_in

def response
SeleniumResponse.new(self, response_body)
end

def response_body #:nodoc:
selenium.get_html_source
end

def click_button(button_text_or_regexp = nil, options = {})
if button_text_or_regexp.is_a?(Hash) && options == {}
pattern, options = nil, button_text_or_regexp
Expand All @@ -61,11 +61,11 @@ def click_button(button_text_or_regexp = nil, options = {})
end
pattern ||= '*'
locator = "button=#{pattern}"

selenium.wait_for_element locator, 5
selenium.click locator
end

webrat_deprecate :clicks_button, :click_button

def click_link(link_text_or_regexp, options = {})
Expand All @@ -74,53 +74,53 @@ def click_link(link_text_or_regexp, options = {})
selenium.wait_for_element locator, 5
selenium.click locator
end

webrat_deprecate :clicks_link, :click_link

def click_link_within(selector, link_text, options = {})
locator = "webratlinkwithin=#{selector}|#{link_text}"
selenium.wait_for_element locator, 5
selenium.click locator
end

webrat_deprecate :clicks_link_within, :click_link_within

def select(option_text, options = {})
id_or_name_or_label = options[:from]

if id_or_name_or_label
select_locator = "webrat=#{id_or_name_or_label}"
else
select_locator = "webratselectwithoption=#{option_text}"
end

selenium.wait_for_element select_locator, 5
selenium.select(select_locator, option_text)
end

webrat_deprecate :selects, :select

def choose(label_text)
locator = "webrat=#{label_text}"
selenium.wait_for_element locator, 5
selenium.click locator
end

webrat_deprecate :chooses, :choose

def check(label_text)
locator = "webrat=#{label_text}"
selenium.wait_for_element locator, 5
selenium.check locator
end

webrat_deprecate :checks, :check

def fire_event(field_identifier, event)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.fire_event(locator, "#{event}")
end

def key_down(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.key_down(locator, key_code)
Expand All @@ -130,7 +130,7 @@ def key_up(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.key_up(locator, key_code)
end

def wait_for(params={})
timeout = params[:timeout] || 5
message = params[:message] || "Timeout exceeded"
Expand All @@ -154,47 +154,47 @@ def wait_for(params={})
raise Webrat::TimeoutError.new(message + " (after #{timeout} sec)")
true
end

def selenium
return $browser if $browser
setup
$browser
end

webrat_deprecate :browser, :selenium


def save_and_open_screengrab
return unless File.exist?(saved_page_dir)

filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png"

if $browser.chrome_backend?
$browser.capture_entire_page_screenshot(filename, '')
else
$browser.capture_screenshot(filename)
end
open_in_browser(filename)
end

protected

def setup #:nodoc:
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

extend_selenium
define_location_strategies
$browser.window_maximize
end

def teardown_at_exit #:nodoc:
at_exit do
silence_stream(STDOUT) do
Expand All @@ -204,21 +204,21 @@ def teardown_at_exit #:nodoc:
end
end
end

def adjust_if_regexp(text_or_regexp) #:nodoc:
if text_or_regexp.is_a?(Regexp)
"evalregex:#{text_or_regexp.inspect}"
else
"evalregex:/#{text_or_regexp}/"
end
end
end

def extend_selenium #:nodoc:
extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
extenions_js = File.read(extensions_file)
selenium.get_eval(extenions_js)
end

def define_location_strategies #:nodoc:
Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
strategy_js = File.read(file)
Expand Down

0 comments on commit 33d2cdc

Please sign in to comment.