This repository has been archived by the owner on Nov 23, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/browser-abstraction' into develop
- Loading branch information
Showing
11 changed files
with
186 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
var async = require('async'); | ||
var noop = require('../../lib/noop'); | ||
var spawn = require('child_process').spawn; | ||
var logger = require('../../lib/logger')('browser.log'); | ||
var chromeCtrl = require('chrome-remote-interface'); | ||
var chromeConnection; | ||
var chromeInstance; | ||
|
||
var params = {}; | ||
var currentUrl; | ||
var initialized = false; | ||
|
||
function initChromeInstance(cb) { | ||
chromeInstance = spawn(params.path, params.args.concat([params.url])); | ||
|
||
chromeInstance.on('error', function (err) { | ||
cb(err); | ||
}); | ||
chromeInstance.on('close', function (code) { | ||
logger.warn('Chrome instance exited with code ' + code); | ||
initChromeInstance(); | ||
}); | ||
chromeInstance.stdout.on('data', function (data) { | ||
logger.info(data.toString()); | ||
}); | ||
|
||
chromeInstance.stderr.on('data', function (data) { | ||
logger.error(data.toString()); | ||
}); | ||
|
||
// wait to let chrome start gracefully | ||
setTimeout(cb, params.startupGracePeriod); | ||
} | ||
|
||
module.exports = { | ||
isInitialized: function () { | ||
return initialized; | ||
}, | ||
init: function (options, cb) { | ||
cb = cb || noop; | ||
|
||
if (!initialized) { | ||
params = options || {}; | ||
currentUrl = params.url; | ||
|
||
async.waterfall([ | ||
function (next) { | ||
initChromeInstance(function (err) { | ||
next(err); | ||
}); | ||
}, | ||
// setup Chrome Protocol | ||
function (next) { | ||
chromeCtrl(function (chrome) { | ||
chromeConnection = chrome; | ||
next(); | ||
}).on('error', function () { | ||
next('Init chrome failed'); | ||
}); | ||
} | ||
], function (err, result) { | ||
if (!err) { | ||
initialized = true; | ||
} | ||
else { | ||
logger.error(err); | ||
} | ||
|
||
cb(err, result); | ||
}); | ||
} | ||
}, | ||
navigate: function (url, cb) { | ||
cb = cb || noop; | ||
|
||
if (chromeConnection) { | ||
chromeConnection.Page.navigate({ | ||
url: url | ||
}, function (err) { | ||
if (err) { | ||
logger.error(err); | ||
} | ||
else { | ||
cb(); | ||
} | ||
}); | ||
} | ||
else { | ||
logger.error('Cannot connect to Chrome'); | ||
} | ||
} | ||
}; | ||
|
||
function exitHandler(options, err) { | ||
if (options.cleanup) { | ||
console.log('clean'); | ||
} | ||
if (err) { | ||
console.log(err.stack); | ||
} | ||
if (options.exit) { | ||
process.exit(); | ||
} | ||
} | ||
|
||
//do something when app is closing | ||
process.on('exit', exitHandler); | ||
|
||
//catches ctrl+c event | ||
process.on('SIGINT', exitHandler); | ||
|
||
//catches uncaught exceptions | ||
process.on('uncaughtException', exitHandler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var path = require('path'); | ||
var mkdirp = require('mkdirp'); | ||
var rootLogDir = (function (logPath) { | ||
mkdirp.sync(logPath); | ||
|
||
return logPath; | ||
})(path.join(process.cwd(), 'logs')); | ||
|
||
var winston = require('winston'); | ||
|
||
/** | ||
* @param {String} filename | ||
* @return {winston.Logger} | ||
*/ | ||
module.exports = function (filename) { | ||
return new (winston.Logger)({ | ||
transports: [ | ||
new (winston.transports.Console)({ | ||
timestamp: true | ||
}), | ||
new (winston.transports.File)({ | ||
filename: path.join(rootLogDir, filename), | ||
zippedArchive: true, | ||
timestamp: true | ||
}) | ||
] | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.