Skip to content

Commit

Permalink
moving app server lookup out into a factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Gaffney committed Jun 4, 2009
1 parent 6816c46 commit a6a4a7c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 29 deletions.
28 changes: 0 additions & 28 deletions lib/webrat/selenium/application_server.rb
Expand Up @@ -5,34 +5,6 @@ class ApplicationServer

include Webrat::Selenium::SilenceStream

def self.boot
case Webrat.configuration.application_framework
when :sinatra
require "webrat/selenium/sinatra_application_server"
SinatraApplicationServer.new.boot
when :merb
require "webrat/selenium/merb_application_server"
MerbApplicationServer.new.boot
when :rails
require "webrat/selenium/rails_application_server"
RailsApplicationServer.new.boot
else
raise WebratError.new(<<-STR)
Unknown Webrat application_framework: #{Webrat.configuration.application_framework.inspect}
Please ensure you have a Webrat configuration block that specifies an application_framework
in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
For example:
Webrat.configure do |config|
# ...
config.application_framework = :rails
end
STR
end
end

def boot
start
wait
Expand Down
37 changes: 37 additions & 0 deletions lib/webrat/selenium/application_server_factory.rb
@@ -0,0 +1,37 @@
module Webrat
module Selenium

class ApplicationServerFactory

def self.app_server_instance
case Webrat.configuration.application_framework
when :sinatra
require "webrat/selenium/sinatra_application_server"
return SinatraApplicationServer.new
when :merb
require "webrat/selenium/merb_application_server"
return MerbApplicationServer.new
when :rails
require "webrat/selenium/rails_application_server"
return RailsApplicationServer.new
else
raise WebratError.new(<<-STR)
Unknown Webrat application_framework: #{Webrat.configuration.application_framework.inspect}
Please ensure you have a Webrat configuration block that specifies an application_framework
in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
For example:
Webrat.configure do |config|
# ...
config.application_framework = :rails
end
STR
end
end

end

end
end
3 changes: 2 additions & 1 deletion lib/webrat/selenium/selenium_session.rb
@@ -1,5 +1,6 @@
require "webrat/core/save_and_open_page"
require "webrat/selenium/selenium_rc_server"
require "webrat/selenium/application_server_factory"
require "webrat/selenium/application_server"

module Webrat
Expand Down Expand Up @@ -198,7 +199,7 @@ def lib_defined?(library)

def setup #:nodoc:
Webrat::Selenium::SeleniumRCServer.boot
Webrat::Selenium::ApplicationServer.boot
Webrat::Selenium::ApplicationServerFactory.app_server_instance.boot

create_browser
$browser.start
Expand Down
3 changes: 3 additions & 0 deletions spec/integration/rails/test/integration/webrat_test.rb
Expand Up @@ -34,6 +34,9 @@ class WebratTest < ActionController::IntegrationTest
fill_in "Text field", :with => "value"
click_button

automate do
selenium.wait_for_page_to_load
end
assert response.body !~ /value/
assert response.body =~ /custom_param/
end
Expand Down
44 changes: 44 additions & 0 deletions spec/public/selenium/application_server_factory_spec.rb
@@ -0,0 +1,44 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

require "webrat/selenium/silence_stream"
require "webrat/selenium/application_server_factory"
require "webrat/selenium/application_server"

require "webrat/selenium/sinatra_application_server"
require "webrat/selenium/merb_application_server"
require "webrat/selenium/rails_application_server"

describe Webrat::Selenium::ApplicationServerFactory do

it "should require and create a sinatra server in sinatra mode" do
server = mock(Webrat::Selenium::SinatraApplicationServer)
Webrat.configuration.application_framework = :sinatra
Webrat::Selenium::ApplicationServerFactory.should_receive(:require).with("webrat/selenium/sinatra_application_server")
Webrat::Selenium::SinatraApplicationServer.should_receive(:new).and_return(server)
Webrat::Selenium::ApplicationServerFactory.app_server_instance.should == server
end

it "should require and create a merb server in merb mode" do
server = mock(Webrat::Selenium::MerbApplicationServer)
Webrat.configuration.application_framework = :merb
Webrat::Selenium::ApplicationServerFactory.should_receive(:require).with("webrat/selenium/merb_application_server")
Webrat::Selenium::MerbApplicationServer.should_receive(:new).and_return(server)
Webrat::Selenium::ApplicationServerFactory.app_server_instance
end

it "should require and create a rails server in rails mode" do
server = mock(Webrat::Selenium::RailsApplicationServer)
Webrat.configuration.application_framework = :rails
Webrat::Selenium::ApplicationServerFactory.should_receive(:require).with("webrat/selenium/rails_application_server")
Webrat::Selenium::RailsApplicationServer.should_receive(:new).and_return(server)
Webrat::Selenium::ApplicationServerFactory.app_server_instance
end

it "should handle unknown servers with an exception in unknown modes" do
Webrat.configuration.application_framework = :unknown
lambda {
Webrat::Selenium::ApplicationServerFactory.app_server_instance
}.should raise_error(Webrat::WebratError)
end

end

0 comments on commit a6a4a7c

Please sign in to comment.