benderjs / browser-launcher2

☠ This package is no longer maintained ☠
Other
24 stars 13 forks source link

Question: Why does the browser have such short lifespan #19

Closed daviddias closed 7 months ago

daviddias commented 9 years ago

Hi!

Following your example, I get exactly the same result, however, I want the browser process to die when I call the .stop() function, I'm sure there should be a way because the browser process is just dying instantly, but I can't figure out why, halp?

Thank you!

daviddias commented 9 years ago

One thing I've realized now is that apparently each launch instance only works to effectively launch one browser (all the others next will exit will 1), so it has to be called like this for several browsers

var launcher = require( 'browser-launcher2' );

var instances = []

launcher(function(err, launch) {
  if ( err ) {
    return console.error( err );
  }

  launch( 'http://cksource.com/', 'chrome', function( err, instance ) {
    if ( err ) {
      return console.error( err );
    }
    console.log( 'Instance started with PID:', instance.pid );

    instance.on( 'stop', function( code ) {
      console.log( 'Instance stopped with exit code && pid', code , ' && ', instance.pid );
    });

  });
});

launcher(function(err, launch) {
  if ( err ) {
    return console.error( err );
  }

  launch( 'http://cksource.com/', 'chrome', function( err, instance ) {
  if ( err ) {
    return console.error( err );
  }

  console.log( 'Instance started with PID:', instance.pid );

  instance.on( 'stop', function( code ) {
    console.log( 'Instance stopped with exit code && pid', code , ' && ', instance.pid );
  });
  });
});

setTimeout(function(){console.log('Dying now')}, 5000);

Nevertheless, the browsers still close immediately, most of the times they don't even load the page

Instance started with PID: 77306
Instance stopped with exit code && pid 0  &&  77306
Instance started with PID: 77312
Instance stopped with exit code && pid 0  &&  77312
gregpabian commented 9 years ago

Hi, sorry for the late answer. Could you clarify on what OS this problem occurs?

daviddias commented 9 years ago

It's on Mac OS X Yosemite

gregpabian commented 9 years ago

@diasdavid again sorry for the delay. I checked what's going on on Mac. So, the problem here is that in order to launch a browser the open command is used and this is the actual child process (Firefox and PhantomJS are exceptions here - those are run using their own binaries). It also appears that in case you already had an instance of the browser opened (with the same user profile), the open command stops immediately - that's why it appears the browser dies right after it's started, but you should still be able to stop the browser using instance.stop() because it uses osascript to stop it.

Please try closing a browser completely (Quit from context menu) and running the provided launch.js example. This should keep running until you completely quit the browser. I'm still thinking of a better solution, but OSX is not my primary OS so a little help here would be appreciated :)

And regarding the latter issue - I can confirm that there are some problems on OSX with starting multiple instances of the same browser, but running two or more instances of different browsers should work fine (and honestly that was our initial goal). I still believe this could be worked out, though it would require using different profiles for each instance and I'm still not sure how to stop those...