1
1
require 'browserstack/localbinary'
2
2
require 'browserstack/localexception'
3
+ require 'json'
3
4
4
5
module BrowserStack
5
6
@@ -75,36 +76,29 @@ def start(options = {})
75
76
end
76
77
77
78
if defined? spawn
78
- @process = IO . popen ( command_args )
79
+ @process = IO . popen ( start_command_args )
79
80
else
80
- @process = IO . popen ( command )
81
+ @process = IO . popen ( start_command )
81
82
end
82
- @stdout = File . open ( @logfile , "r" )
83
83
84
84
while true
85
85
begin
86
- line = @stdout . readline
86
+ line = @process . readline
87
87
rescue EOFError => e
88
88
sleep 1
89
89
next
90
90
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" ] )
95
96
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" ]
100
99
break
101
100
end
102
101
end
103
-
104
- while true
105
- break if self . isRunning
106
- sleep 1
107
- end
108
102
end
109
103
110
104
def isRunning
@@ -113,20 +107,37 @@ def isRunning
113
107
114
108
def stop
115
109
return if @pid . nil?
116
- Process . kill ( "TERM" , @pid ) rescue Process . kill ( 9 , @pid )
117
110
@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 )
121
115
end
116
+ @process . close
117
+ @pid = nil
122
118
end
123
119
124
120
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
126
137
end
127
138
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 } " ]
130
141
args = args . select { |a | a . to_s != "" }
131
142
args . push ( :err => [ :child , :out ] )
132
143
args
0 commit comments