From f6bfa0cf29a4f10421b9edf1393cea575ebdc019 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 14 May 2013 10:57:55 -0400 Subject: [PATCH] add timeout option --- lib/persistent_selenium.rb | 6 +++++- lib/persistent_selenium/cli.rb | 5 +++-- lib/persistent_selenium/drb.rb | 17 +++++++++++++++++ lib/persistent_selenium/driver.rb | 6 +++--- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 lib/persistent_selenium/drb.rb diff --git a/lib/persistent_selenium.rb b/lib/persistent_selenium.rb index f00aa8c..65b4091 100644 --- a/lib/persistent_selenium.rb +++ b/lib/persistent_selenium.rb @@ -3,7 +3,7 @@ require 'selenium-webdriver' module PersistentSelenium class << self - attr_writer :port, :browser + attr_writer :port, :browser, :timeout def port @port ||= 9854 @@ -13,6 +13,10 @@ module PersistentSelenium @browser ||= :firefox end + def timeout + @timeout ||= 120 + end + def url "druby://localhost:#{port}" end diff --git a/lib/persistent_selenium/cli.rb b/lib/persistent_selenium/cli.rb index 9723580..625c85c 100644 --- a/lib/persistent_selenium/cli.rb +++ b/lib/persistent_selenium/cli.rb @@ -10,14 +10,15 @@ module PersistentSelenium end desc "start", "Start the server" - method_options :port => PersistentSelenium.port, :browser => PersistentSelenium.browser + method_options :port => PersistentSelenium.port, :browser => PersistentSelenium.browser, :timeout => PersistentSelenium.timeout def start require 'persistent_selenium/browser' - require 'drb' + require 'persistent_selenium/drb' PersistentSelenium.configure do |c| c.port = options[:port] c.browser = options[:browser] + c.timeout = options[:timeout] end puts "Starting persistent_selenium on #{PersistentSelenium.port} with #{PersistentSelenium.browser}" diff --git a/lib/persistent_selenium/drb.rb b/lib/persistent_selenium/drb.rb new file mode 100644 index 0000000..9cd21ca --- /dev/null +++ b/lib/persistent_selenium/drb.rb @@ -0,0 +1,17 @@ +require 'drb' + +module DRb + class DRbTCPSocket + alias :_set_sockopt :set_sockopt + + def set_sockopt(soc) + _set_sockopt(soc) + + optval = [ PersistentSelenium.timeout, 0 ].pack("l_2") + + soc.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval + soc.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval + end + end +end + diff --git a/lib/persistent_selenium/driver.rb b/lib/persistent_selenium/driver.rb index e8f4160..d01b11f 100644 --- a/lib/persistent_selenium/driver.rb +++ b/lib/persistent_selenium/driver.rb @@ -11,10 +11,10 @@ Before do end end -Capybara.register_driver :persistent_selenium do |app| - require 'drb' +require 'persistent_selenium/drb' - DRb.start_service +Capybara.register_driver :persistent_selenium do |app| + service = DRb.start_service browser = DRbObject.new nil, PersistentSelenium.url server = Capybara::Server.new(app)