1- import subprocess , os , time
1+ import subprocess , os , time , json , psutil
22from browserstack .local_binary import LocalBinary
33from browserstack .bserrors import BrowserStackLocalError
44
@@ -17,12 +17,17 @@ def __xstr(self, key, value):
1717 return ['-' + key , value ]
1818
1919 def _generate_cmd (self ):
20- cmd = [self .binary_path , '-logFile' , self .local_logfile_path , self .key ]
20+ cmd = [self .binary_path , '-d' , 'start' , '- logFile' , self .local_logfile_path , self .key ]
2121 for o in self .options .keys ():
2222 if self .options .get (o ) is not None :
2323 cmd = cmd + self .__xstr (o , self .options .get (o ))
2424 return cmd
2525
26+ def _generate_stop_cmd (self ):
27+ cmd = self ._generate_cmd ()
28+ cmd [2 ] = 'stop'
29+ return cmd
30+
2631 def start (self , ** kwargs ):
2732 self .options = kwargs
2833
@@ -43,34 +48,29 @@ def start(self, **kwargs):
4348 if "onlyCommand" in kwargs and kwargs ["onlyCommand" ]:
4449 return
4550
46- self .proc = subprocess .Popen (self ._generate_cmd (), stdout = subprocess .PIPE )
47- self . stderr = self .proc .stderr
51+ self .proc = subprocess .Popen (self ._generate_cmd (), stdout = subprocess .PIPE , stderr = subprocess . PIPE )
52+ ( out , err ) = self .proc .communicate ()
4853
4954 os .system ('echo "" > "' + self .local_logfile_path + '"' )
50- with open (self .local_logfile_path , 'r' ) as local_logfile :
51- while True :
52- line = local_logfile .readline ()
53- if 'Error:' in line .strip ():
54- raise BrowserStackLocalError (line )
55- elif line .strip () == 'Press Ctrl-C to exit' :
56- break
55+ try :
56+ if out :
57+ data = json .loads (out .decode ())
58+ else :
59+ data = json .loads (err .decode ())
5760
58- while True :
59- if self .isRunning ():
60- break
61- time .sleep (1 )
61+ if data ['state' ] != "connected" :
62+ raise BrowserStackLocalError (data ["message" ])
63+ else :
64+ self .pid = data ['pid' ]
65+ except ValueError :
66+ raise BrowserStackLocalError ('Error parsing JSON output from daemon' )
6267
6368 def isRunning (self ):
64- if (hasattr (self , 'proc' )):
65- return True if self .proc .poll () is None else False
66- return False
69+ return hasattr (self , 'pid' ) and psutil .pid_exists (self .pid )
6770
6871 def stop (self ):
6972 try :
70- self .proc .terminate ()
71- while True :
72- if not self .isRunning ():
73- break
74- time .sleep (1 )
73+ proc = subprocess .Popen (self ._generate_stop_cmd (), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
74+ (out , err ) = proc .communicate ()
7575 except Exception as e :
76- return
76+ return
0 commit comments