Created
November 2, 2009 11:28
-
-
Save kornysietsma/224099 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'selenium/client' | |
require 'selenium/rspec/spec_helper' | |
require File.join(BASEDIR,'support/user_config.rb') | |
if defined? JRUBY_VERSION | |
# jruby has it's own process-handling code, as 'fork' is unreliable in java | |
require 'java' | |
end | |
class SeleniumHelper | |
attr_accessor :max_timeout, :browser | |
def initialize(user_config) | |
@user_config = user_config | |
@max_timeout = 45 # maximum allowed timeout | |
@selenium_port = user_config['selenium.port'] | |
@selenium_browser = @user_config['selenium.browser.name'] | |
@selenium_process = nil | |
@browser = nil | |
start_selenium | |
start_browser | |
end | |
def shutdown | |
stop_selenium | |
end | |
private | |
def start_selenium | |
selenium_jar_path = File.expand_path(File.join(PRJDIR,"lib","selenium","selenium-server.jar")) | |
raise "Can't find #{selenium_jar_path}" unless File.exists?(selenium_jar_path) | |
cmd = "java -jar #{selenium_jar_path} -timeout #{@max_timeout} -port #{@selenium_port}" | |
if defined? JRUBY_VERSION # java magic to run a process | |
@selenium_process = java.lang.ProcessBuilder.new(cmd.split(" ")).redirectErrorStream(true).start | |
# spawn a thread to redirect background process to log file | |
$stderr.puts "selenium process started in background" | |
output_stream_to_log(@selenium_process.getInputStream, "selenium.log") | |
else # not java - use fork | |
@selenium_process = Process.fork do | |
# Note: you need to redirect stdout this way | |
# - if you try using "cmd > selenium.log" ruby spawns a subprocess, which you can't kill | |
$stdout.reopen(File.new("selenium.log", "w")) | |
exec cmd | |
end | |
$stderr.puts "selenium process started with pid #{@selenium_process}" | |
end | |
sleep 2 # give it a chance to start | |
$stderr.puts "Selenium output sent to selenium.log" | |
end | |
def start_browser | |
begin | |
base_url = "http://localhost" | |
@browser = Selenium::Client::Driver.new("localhost", @selenium_port, @selenium_browser, base_url, @max_timeout) | |
@browser.start_new_browser_session | |
rescue Exception | |
stop_selenium | |
raise | |
end | |
end | |
def stop_selenium | |
$stderr.puts "killing background selenium task" | |
# could open http://localhost:selenium_port/selenium-server/driver/?cmd=shutDown - but this is more reliable! | |
if defined? JRUBY_VERSION | |
@selenium_process.destroy | |
$stderr.puts "and waiting..." | |
@selenium_process.waitFor | |
else | |
Process.kill("HUP", @selenium_process) | |
$stderr.puts "and waiting..." | |
Process.wait | |
end | |
$stderr.puts "dead." | |
end | |
def output_stream_to_log(inputStream, logfilename) | |
Thread.new do | |
File.open(logfilename,"w") do |f| | |
output = java.io.BufferedReader.new(java.io.InputStreamReader.new(inputStream)) | |
while (line = output.readLine) != nil | |
f.puts line | |
end | |
output.close | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment