probe.gl
Version:
JavaScript Console Instrumentation and Benchmarking for Browser and Node
194 lines (173 loc) • 5.76 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _color = require("../../lib/utils/color");
var _log = _interopRequireDefault(require("../../lib/log"));
var log = new _log.default({
id: 'render-test'
});
var DEFAULT_CONFIG = {
process: './node_modules/.bin/webpack-dev-server',
parameters: ['--config', 'webpack.config.js'],
port: 5000,
options: {
maxBuffer: 5000 * 1024
}
};
var DEFAULT_PUPPETEER_OPTIONS = {
headless: false
};
var BrowserDriver = function () {
function BrowserDriver() {
(0, _classCallCheck2.default)(this, BrowserDriver);
this.execFile = module.require('child_process').execFile;
this.puppeteer = module.require('puppeteer');
this.console = module.require('console');
this.process = module.require('process');
this.child = null;
this.browser = null;
this.page = null;
this.port = null;
this.shellStatus = 0;
}
(0, _createClass2.default)(BrowserDriver, [{
key: "setShellStatus",
value: function setShellStatus(success) {
this.shellStatus = success ? 0 : 1;
}
}, {
key: "startBrowser",
value: function startBrowser() {
var _this = this;
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_PUPPETEER_OPTIONS;
if (this.browser) {
return Promise.resolve(this.browser);
}
return this.puppeteer.launch(options).then(function (browser) {
_this.browser = browser;
});
}
}, {
key: "newPage",
value: function newPage() {
var _this2 = this;
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$url = _ref.url,
url = _ref$url === void 0 ? 'http://localhost' : _ref$url,
_ref$width = _ref.width,
width = _ref$width === void 0 ? 1550 : _ref$width,
_ref$height = _ref.height,
height = _ref$height === void 0 ? 850 : _ref$height;
log.log({
message: "Connecting to port: ".concat(this.port),
color: _color.COLOR.YELLOW
})();
return this.startBrowser().then(function (_) {
return _this2.browser.newPage();
}).then(function (page) {
_this2.page = page;
}).then(function (_) {
return _this2.page.waitFor(1000);
}).then(function (_) {
return _this2.page.goto("".concat(url, ":").concat(_this2.port));
}).then(function (_) {
return _this2.page.setViewport({
width: 1550,
height: 850
});
});
}
}, {
key: "exposeFunction",
value: function exposeFunction() {
var _this3 = this;
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'testDriverDone';
return new Promise(function (resolve) {
_this3.page.exposeFunction(name, resolve);
});
}
}, {
key: "stopBrowser",
value: function stopBrowser() {
var _this4 = this;
return Promise.resolve().then(function (_) {
return _this4.page.waitFor(1000);
}).then(function (_) {
return _this4.browser.close();
});
}
}, {
key: "startServer",
value: function startServer() {
var _this5 = this;
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var maxRetryTimes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 30;
var newConfig = Object.assign({}, DEFAULT_CONFIG, config);
return new Promise(function (resolve, reject) {
log.log({
message: "Binding to port: ".concat(newConfig.port),
color: _color.COLOR.YELLOW
})();
var timeout = setTimeout(function () {
resolve();
}, 2000);
_this5.child = _this5.execFile(newConfig.process, (0, _toConsumableArray2.default)(newConfig.parameters).concat(['--port', "".concat(newConfig.port)]), newConfig.options, function (error) {
if (error) {
clearTimeout(timeout);
log.log({
message: "Failed to bind port: ".concat(newConfig.port),
color: _color.COLOR.YELLOW
})();
reject(error);
}
});
}).then(function () {
_this5.port = newConfig.port;
}).catch(function (error) {
if (maxRetryTimes > 0) {
newConfig.port++;
return _this5.startServer(newConfig, maxRetryTimes - 1);
} else {
log.log({
message: 'Failed to start server, use \'killall node\' to stop existing services',
color: _color.COLOR.RED
})();
throw error;
}
});
}
}, {
key: "stopServer",
value: function stopServer() {
if (this.child) {
this.child.kill();
this.child = null;
}
}
}, {
key: "exitProcess",
value: function exitProcess() {
this.process.exit(this.shellStatus);
}
}, {
key: "exit",
value: function exit() {
var _this6 = this;
return Promise.resolve().then(function () {
return _this6.stopBrowser();
}).then(function () {
_this6.stopServer();
_this6.exitProcess();
});
}
}]);
return BrowserDriver;
}();
exports.default = BrowserDriver;
//# sourceMappingURL=browser-driver.js.map