11require 'browserstack/localbinary'
22require 'browserstack/localexception'
3+ require 'json'
34
45module BrowserStack
56
@@ -75,36 +76,29 @@ def start(options = {})
7576 end
7677
7778 if defined? spawn
78- @process = IO . popen ( command_args )
79+ @process = IO . popen ( start_command_args )
7980 else
80- @process = IO . popen ( command )
81+ @process = IO . popen ( start_command )
8182 end
82- @stdout = File . open ( @logfile , "r" )
8383
8484 while true
8585 begin
86- line = @stdout . readline
86+ line = @process . readline
8787 rescue EOFError => e
8888 sleep 1
8989 next
9090 end
91- break if line . nil?
92- if line . match ( /\* \* \* Error\: / )
93- @stdout . close
94- raise BrowserStack ::LocalException . new ( line )
91+
92+ data = JSON . parse ( line ) rescue { "message" => "Unable to parse daemon mode JSON output" }
93+ if data [ 'state' ] . to_s != "connected"
94+ @process . close
95+ raise BrowserStack ::LocalException . new ( data [ "message" ] )
9596 return
96- end
97- if line . strip == "Press Ctrl-C to exit"
98- @pid = @process . pid
99- @stdout . close
97+ else
98+ @pid = data [ "pid" ]
10099 break
101100 end
102101 end
103-
104- while true
105- break if self . isRunning
106- sleep 1
107- end
108102 end
109103
110104 def isRunning
@@ -113,20 +107,37 @@ def isRunning
113107
114108 def stop
115109 return if @pid . nil?
116- Process . kill ( "TERM" , @pid ) rescue Process . kill ( 9 , @pid )
117110 @process . close
118- @pid = nil if @is_windows
119- while self . isRunning
120- sleep 1
111+ if defined? spawn
112+ @process = IO . popen ( stop_command_args )
113+ else
114+ @process = IO . popen ( stop_command )
121115 end
116+ @process . close
117+ @pid = nil
122118 end
123119
124120 def command
125- "#{ @exec } #{ @binary_path } -logFile '#{ @logfile } ' #{ @folder_flag } #{ @key } #{ @folder_path } #{ @force_local_flag } #{ @local_identifier_flag } #{ @only_flag } #{ @only_automate_flag } #{ @proxy_host } #{ @proxy_port } #{ @proxy_user } #{ @proxy_pass } #{ @force_proxy_flag } #{ @force_flag } #{ @verbose_flag } #{ @hosts } #{ @user_arguments . join ( " " ) } " . strip
121+ start_command
122+ end
123+
124+ def start_command
125+ "#{ @binary_path } -d start -logFile '#{ @logfile } ' #{ @folder_flag } #{ @key } #{ @folder_path } #{ @force_local_flag } #{ @local_identifier_flag } #{ @only_flag } #{ @only_automate_flag } #{ @proxy_host } #{ @proxy_port } #{ @proxy_user } #{ @proxy_pass } #{ @force_proxy_flag } #{ @force_flag } #{ @verbose_flag } #{ @hosts } #{ @user_arguments . join ( " " ) } 2>&1" . strip
126+ end
127+
128+ def start_command_args
129+ args = [ "#{ @binary_path } " , "-d" , "start" , "-logFile" , "#{ @logfile } " , "#{ @key } " , "#{ @folder_flag } " , "#{ @folder_path } " , "#{ @force_local_flag } " , "#{ @local_identifier_flag } " , "#{ @only_flag } " , "#{ @only_automate_flag } " , "#{ @proxy_host } " , "#{ @proxy_port } " , "#{ @proxy_user } " , "#{ @proxy_pass } " , "#{ @force_proxy_flag } " , "#{ @force_flag } " , "#{ @verbose_flag } " , "#{ @hosts } " , "#{ @user_arguments . join ( " " ) } " ]
130+ args = args . select { |a | a . to_s != "" }
131+ args . push ( :err => [ :child , :out ] )
132+ args
133+ end
134+
135+ def stop_command
136+ "#{ @binary_path } -d stop #{ @local_identifier_flag } " . strip
126137 end
127138
128- def command_args
129- args = [ "#{ @binary_path } " , "-logFile " , "#{ @logfile } " , "#{ @key } " , " #{ @folder_flag } " , " #{ @folder_path } " , " #{ @force_local_flag } " , " #{ @ local_identifier_flag} " , " #{ @only_flag } " , " #{ @only_automate_flag } " , " #{ @proxy_host } " , " #{ @proxy_port } " , " #{ @proxy_user } " , " #{ @proxy_pass } " , " #{ @force_proxy_flag } " , " #{ @force_flag } " , " #{ @verbose_flag } " , " #{ @hosts } " , " #{ @user_arguments . join ( " " ) } "]
139+ def stop_command_args
140+ args = [ "#{ @binary_path } " , "-d " , "stop " , "#{ @local_identifier_flag } " ]
130141 args = args . select { |a | a . to_s != "" }
131142 args . push ( :err => [ :child , :out ] )
132143 args
0 commit comments