Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/webrat/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def application_port_for_selenium
# Which server the application is running on for selenium testing? Defaults to localhost
attr_accessor :application_address

# Path to unicorn configuration file if :application_framework == :unicorn
attr_accessor :unicorn_conf_file

# Which server Selenium server is running on. Defaults to nil(server starts in webrat process and runs locally)
attr_accessor :selenium_server_address

Expand Down
3 changes: 3 additions & 0 deletions lib/webrat/selenium/application_server_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def self.app_server_instance
when :rails
require "webrat/selenium/application_servers/rails"
return Webrat::Selenium::ApplicationServers::Rails.new
when :rails_unicorn
require "webrat/selenium/application_servers/rails_unicorn"
return Webrat::Selenium::ApplicationServers::RailsUnicorn.new
when :rack
require "webrat/selenium/application_servers/rack"
return Webrat::Selenium::ApplicationServers::Rack.new
Expand Down
1 change: 1 addition & 0 deletions lib/webrat/selenium/application_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
require "webrat/selenium/application_servers/sinatra"
require "webrat/selenium/application_servers/merb"
require "webrat/selenium/application_servers/rails"
require "webrat/selenium/application_servers/rails_unicorn"
require "webrat/selenium/application_servers/external"
64 changes: 64 additions & 0 deletions lib/webrat/selenium/application_servers/rails_unicorn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require "webrat/selenium/application_servers/base"

module Webrat
module Selenium
module ApplicationServers
class RailsUnicorn < Webrat::Selenium::ApplicationServers::Base

def start
system start_command
end

def stop
silence_stream(STDOUT) do
system stop_command
end if File.exist?(pid_file)
end

def fail
$stderr.puts
$stderr.puts
$stderr.puts "==> Failed to boot the Rails Unicorn application server... exiting!"
$stderr.puts
$stderr.puts "Verify you can start a Rails Unicorn server on port #{Webrat.configuration.application_port} with the following command:"
$stderr.puts
$stderr.puts " #{start_command}"
exit
end

def pid_file
prepare_pid_file("#{::Rails.root}/tmp/pids", "unicorn.pid")
end

def start_command
"cd '#{::Rails.root}' && #{bundler}#{unicorn} -D -p #{Webrat.configuration.application_port} -E #{Webrat.configuration.application_environment}#{unicorn_config}"
end

def stop_command
"kill `cat '#{pid_file}'`"
end

private

def bundler
File.exist?("./Gemfile") ? "bundle exec " : ""
end

def unicorn
if ::Rails.version && ::Rails.version[0,2] == "2."
"unicorn_rails"
else
"unicorn"
end
end

def unicorn_config
conf_file = Webrat.configuration.unicorn_conf_file
conf_file.blank? ? "" : " -c #{conf_file}"
end

end
end
end
end

4 changes: 4 additions & 0 deletions spec/private/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
@config.selenium_browser_startup_timeout = 10
@config.selenium_browser_startup_timeout.should == 10
end

it 'should default unicorn configuration file path to nil' do
@config.unicorn_conf_file.should be_nil
end
end

end
7 changes: 7 additions & 0 deletions spec/private/selenium/application_servers/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
# require "rubygems"; require "ruby-debug"; Debugger.start; debugger
@server.stub!(:system)
@server.stub!(:at_exit)


class Rails
def self.root
RAILS_ROOT
end
end
end

describe "boot" do
Expand Down
8 changes: 8 additions & 0 deletions spec/public/selenium/application_server_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
Webrat::Selenium::ApplicationServerFactory.app_server_instance
end

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

it "should require and create a rails server in external mode" do
server = mock(Webrat::Selenium::ApplicationServers::External)
Webrat.configuration.application_framework = :external
Expand Down
72 changes: 72 additions & 0 deletions spec/public/selenium/application_servers/rails_unicorn_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require File.expand_path(File.dirname(__FILE__) + "/../../../spec_helper")
require "webrat/selenium/application_servers/rails_unicorn"

RAILS_ROOT = "." unless defined?(RAILS_ROOT)


describe Webrat::Selenium::ApplicationServers::RailsUnicorn do
include Webrat::Selenium::SilenceStream

before do
@server = Webrat::Selenium::ApplicationServers::RailsUnicorn.new
# require "rubygems"; require "ruby-debug"; Debugger.start; debugger
@server.stub!(:system)
@server.stub!(:at_exit)

# Fake the top-level Rails object
class Rails
cattr_accessor :root
cattr_accessor :version
end

Rails.root = RAILS_ROOT
Rails.version = "3."
end

describe "boot" do
it "should wait for the server to start on 0.0.0.0" do
TCPSocket.should_receive(:wait_for_service_with_timeout).
with(hash_including(:host => "0.0.0.0"))

silence_stream(STDERR) do
@server.boot
end
end
end

describe "start command" do
context "when run under Rails v2" do
before do
::Rails.version = "2.x.x"
end

it "should invoke unicorn_rails" do
@server.start_command.should == "cd '#{RAILS_ROOT}' && bundle exec unicorn_rails -D -p 3001 -E test"
end
end

context "when run under Rails v3" do
before do
::Rails.version = "3.x.x"
end

context "with the default configuration" do
it "should invoke unicorn" do
@server.start_command.should == "cd '#{RAILS_ROOT}' && bundle exec unicorn -D -p 3001 -E test"
end
end

context "when configuration specifies a :unicorn_conf_file" do
before do
Webrat.configure do |conf|
conf.unicorn_conf_file = "/foo/bar.rb"
end
end

it "should invoke unicorn" do
@server.start_command.should == "cd '#{RAILS_ROOT}' && bundle exec unicorn -D -p 3001 -E test -c /foo/bar.rb"
end
end
end
end
end
2 changes: 2 additions & 0 deletions webrat.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ Most Ruby web frameworks and testing frameworks are supported.}
"spec/private/rails/attaches_file_spec.rb",
"spec/private/rails/rails_adapter_spec.rb",
"spec/private/selenium/application_servers/rails_spec.rb",
"spec/private/selenium/application_servers/rails_unicorn_spec.rb",
"spec/public/basic_auth_spec.rb",
"spec/public/check_spec.rb",
"spec/public/choose_spec.rb",
Expand Down Expand Up @@ -304,6 +305,7 @@ Most Ruby web frameworks and testing frameworks are supported.}
"spec/private/rails/attaches_file_spec.rb",
"spec/private/rails/rails_adapter_spec.rb",
"spec/private/selenium/application_servers/rails_spec.rb",
"spec/private/selenium/application_servers/rails_unicorn_spec.rb",
"spec/public/basic_auth_spec.rb",
"spec/public/check_spec.rb",
"spec/public/choose_spec.rb",
Expand Down