run-headless-chromium
Advanced tools
Comparing version 0.0.14 to 0.1.0
{ | ||
"name": "run-headless-chromium", | ||
"version": "0.0.14", | ||
"version": "0.1.0", | ||
"description": "Run Chromium or Google Chrome in headless mode and forward the JS console output to the standard output.", | ||
"author": "Rob Wu <rob@robwu.nl> (https://robwu.nl/)", | ||
"bin": "./run-headless-chromium.js", | ||
"main": "./headless-chromium.js", | ||
"license": "MIT", | ||
@@ -8,0 +9,0 @@ "repository": "https://github.com/CodeYellowBV/run-headless-chromium", |
@@ -86,4 +86,33 @@ Starts Chromium or Google Chrome in headless mode (using Xvfb) and forwards the output from JavaScript console to stdout. | ||
### Node.js | ||
The following example shows how to start `run-headless-chromium` from Node.js, | ||
and terminate the process in a graceful way. Note that the page from the example | ||
does not call `console.info('All tests completed!0')`, so the browser would stay | ||
around indefinitely if we do not explicitly kill it. | ||
```javascript | ||
// spawn has the same API as child_process.spawn, minus the command argument. | ||
// See the "spawn" method at https://nodejs.org/api/child_process.html | ||
var spawnHeadlessChromium = require('run-headless-chromium').spawn; | ||
var proc = spawnHeadlessChromium([ | ||
// Flags forwarded to Chromium: | ||
'https://example.com/some_page_that_does_not_print_exit_message', | ||
], { | ||
stdio: 'inherit', | ||
}); | ||
proc.on('close', function() { | ||
clearTimeout(delayedExit); | ||
console.log('Headless Chromium exited!'); | ||
}); | ||
var delayedExit = setTimeout(function() { | ||
console.log('Chrome did not exit within a few seconds, sending SIGINT...'); | ||
// Graceful exit - allow run-headless-chromium to exit Chrome and Xvfb. | ||
proc.kill('SIGINT'); | ||
// If you do proc.kill(); then Chrome and Xvfb may still hang around. | ||
}, 5000); | ||
``` | ||
## License | ||
MIT |
#!/usr/bin/env node | ||
'use strict'; | ||
if (require.main !== module) { | ||
throw new Error('This is not a requirable module. Use child_process to launch this module!'); | ||
} | ||
var os = require('os'); | ||
@@ -150,2 +146,11 @@ var path = require('path'); | ||
}); | ||
process.once('SIGINT', function() { | ||
if (!crProcessExited) { | ||
// This should trigger the 'exit' event on crProcess, which in turn | ||
// cleans up Xvfb. | ||
crProcess.kill(); | ||
} | ||
// If `crProcessExited` is true and we received the event, then we | ||
// are still in the process of exiting Xvfb. So ignore the signal. | ||
}); | ||
_toggle_crProcessEvents(true); | ||
@@ -152,0 +157,0 @@ function _toggle_crProcessEvents(register) { |
14453
5
252
118
2