Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
remove :waitTime option and try to handle better firefox startup
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmo committed Feb 20, 2011
1 parent e124692 commit 86dc14d
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions firewatir/lib/firewatir/firefox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,24 @@ class Firefox
#
# Input:
# options - Hash of any of the following options:
# :waitTime - Time to wait for Firefox to start. By default it waits for 2 seconds.
# This is done because if Firefox is not started and we try to connect
# to jssh on port 9997 an exception is thrown.
# :profile - The Firefox profile to use. If none is specified, Firefox will use
# the last used profile.
# :suppress_launch_process - do not create a new firefox process. Connect to an existing one.

# TODO: Start the firefox version given by user.

def initialize(options = {})
if(options.kind_of?(Integer))
options = {:waitTime => options}
end

# check for jssh not running, firefox may be open but not with -jssh
# if its not open at all, regardless of the :suppress_launch_process option start it
# error if running without jssh, we don't want to kill their current window (mac only)
jssh_down = false
begin
set_defaults()
rescue Watir::Exception::UnableToStartJSShException
jssh_down = true
end

if current_os == :macosx && !%x{ps x | grep firefox-bin | grep -v grep}.empty?
# check for jssh not running, firefox may be open but not with -jssh
# if its not open at all, regardless of the :suppress_launch_process option start it
# error if running without jssh, we don't want to kill their current window (mac only)
jssh_down = false
begin
set_defaults()
rescue Watir::Exception::UnableToStartJSShException
jssh_down = true
end

raise "Firefox is running without -jssh" if jssh_down
open_window unless options[:suppress_launch_process]
elsif not options[:suppress_launch_process]
Expand Down Expand Up @@ -70,8 +63,6 @@ def launch_browser(options = {})

bin = path_to_bin()
@t = Thread.new { system("#{bin} -jssh #{profile_opt}") }
sleep options[:waitTime] || 2

end
private :launch_browser

Expand Down Expand Up @@ -144,15 +135,17 @@ def execute_script(source)
private
# This function creates a new socket at port 9997 and sets the default values for instance and class variables.
# Generatesi UnableToStartJSShException if cannot connect to jssh even after 3 tries.
def set_defaults(no_of_tries = 0)
def set_defaults
no_of_tries = 0
# JSSH listens on port 9997. Create a new socket to connect to port 9997.
begin
$jssh_socket = TCPSocket::new(MACHINE_IP, "9997")
$jssh_socket.sync = true
read_socket()
rescue
no_of_tries += 1
retry if no_of_tries < 3
sleep 1
retry if no_of_tries < 10
raise UnableToStartJSShException, "Unable to connect to machine : #{MACHINE_IP} on port 9997. Make sure that JSSh is properly installed and Firefox is running with '-jssh' option"
end
@error_checkers = []
Expand Down

0 comments on commit 86dc14d

Please sign in to comment.