@@ -653,6 +653,26 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort)
653
653
return asString ;
654
654
} ;
655
655
656
+ // Return a promise that resolves to whether the port is available.
657
+ var checkPortAvailability = function ( port ) {
658
+ var net = require ( 'net' ) ;
659
+ var deferred = webdriver . promise . defer ( ) ;
660
+ var tester = net . connect ( { port : port } , function ( ) {
661
+ deferred . reject ( 'Port ' + port + ' is already in use.' ) ;
662
+ } ) ;
663
+ tester . once ( 'error' , function ( err ) {
664
+ if ( err . code === 'ECONNREFUSED' ) {
665
+ tester . once ( 'close' , function ( ) {
666
+ deferred . fulfill ( ) ;
667
+ } ) . end ( ) ;
668
+ } else {
669
+ deferred . reject ( 'Unexpected failure testing for port ' + port + ': ' +
670
+ err . message ) ;
671
+ }
672
+ } ) ;
673
+ return deferred . promise ;
674
+ } ;
675
+
656
676
var vm_ = require ( 'vm' ) ;
657
677
var browserUnderDebug = this ;
658
678
var flow = webdriver . promise . controlFlow ( ) ;
@@ -661,11 +681,13 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort)
661
681
log . puts ( 'Starting WebDriver debugger in a child process. Pause is ' +
662
682
'still beta, please report issues at github.com/angular/protractor\n' ) ;
663
683
process . debugPort = opt_debugPort || process . debugPort ;
664
- var nodedebug = require ( 'child_process' ) .
665
- fork ( debuggerClientPath , [ process . pid , process . debugPort ] ) ;
666
- process . on ( 'exit' , function ( ) {
667
- nodedebug . kill ( 'SIGTERM' ) ;
668
- } ) ;
684
+ return checkPortAvailability ( process . debugPort ) . then ( function ( ) {
685
+ var nodedebug = require ( 'child_process' ) . fork (
686
+ debuggerClientPath , [ process . pid , process . debugPort ] ) ;
687
+ process . on ( 'exit' , function ( ) {
688
+ nodedebug . kill ( 'SIGTERM' ) ;
689
+ } ) ;
690
+ } ) ;
669
691
} ) ;
670
692
671
693
var pausePromise = flow . timeout ( 1000 , 'waiting for debugger to attach' )
0 commit comments