UNPKG

4.99 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _jsdom = require('jsdom');
10
11var _jsdom2 = _interopRequireDefault(_jsdom);
12
13var _path = require('path');
14
15var _path2 = _interopRequireDefault(_path);
16
17var _fs = require('fs');
18
19var _fs2 = _interopRequireDefault(_fs);
20
21var _promisePoller = require('promise-poller');
22
23var _promisePoller2 = _interopRequireDefault(_promisePoller);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29var Browser = function () {
30 function Browser(config) {
31 _classCallCheck(this, Browser);
32
33 this.config = {
34 verbose: config.verbose,
35 baseUrl: config.baseUrl,
36 jQuery: config.jQuery,
37 setupWindow: config.setupWindow || function (w) {
38 return w;
39 },
40 readyWhen: config.readyWhen || function (w) {
41 return true;
42 },
43 readyWhenInterval: config.readyWhenInterval || 10,
44 readyWhenTimeout: config.readyWhenTimeout || 2000
45 };
46
47 if (!this.config.baseUrl) {
48 throw new Error('No baseUrl set for Browser');
49 }
50 }
51
52 _createClass(Browser, [{
53 key: 'start',
54 value: function start() {
55 // Setup jQuery
56 if (this.config.jQuery) {
57 // TODO: Use readFile-not-sync
58 // TODO: Use jquery from package
59 var jQueryPath = _path2.default.resolve(__dirname, './jquery/jquery.js');
60 this.jQuery = _fs2.default.readFileSync(jQueryPath).toString();
61 }
62
63 // Setup console
64 this.virtualConsole = _jsdom2.default.createVirtualConsole();
65 var logError = function logError() {
66 var _console;
67
68 for (var _len = arguments.length, output = Array(_len), _key = 0; _key < _len; _key++) {
69 output[_key] = arguments[_key];
70 }
71
72 return (_console = console).log.apply(_console, ['\x1b[31m'].concat(output, ['\x1b[0m']));
73 };
74 if (this.config.verbose) {
75 this.virtualConsole.sendTo(Object.assign({}, console, { error: logError }));
76 } else {
77 this.virtualConsole.sendTo({ error: logError });
78 }
79
80 return Promise.resolve();
81 }
82 }, {
83 key: 'go',
84 value: function go(path) {
85 var _this = this;
86
87 var url = this.config.baseUrl + path;
88 this.url = url;
89
90 if (this.config.verbose) {
91 console.log('Going to url:', url);
92 }
93
94 return this._getDOM(url).then(function (window) {
95 return _this.window = window;
96 }).then(function () {
97 return _this._waitForReady(_this.window);
98 }).then(function () {
99 return _this.config.setupWindow(_this.window);
100 }).then(function () {
101 return _this.window;
102 }).catch(function (err) {
103 console.error('Something went wrong at: ' + url);
104 console.trace(err);
105 throw err;
106 });
107 }
108 }, {
109 key: 'reset',
110 value: function reset() {
111 if (this.window) {
112 this.window.close();
113 delete this.window;
114 }
115 }
116 }, {
117 key: '_getDOM',
118 value: function _getDOM(url) {
119 var _this2 = this;
120
121 return new Promise(function (resolve, reject) {
122 _jsdom2.default.env({
123 url: url,
124 src: [_this2.jQuery],
125 features: {
126 FetchExternalResources: ['script'],
127 ProcessExternalResources: ['script']
128 },
129 virtualConsole: _this2.virtualConsole,
130 done: function done(err, window) {
131 if (err) {
132 return reject(err);
133 }
134 if (!window) {
135 return reject(new Error('no window'));
136 }
137 resolve(window);
138 }
139 });
140 });
141 }
142 }, {
143 key: '_waitForReady',
144 value: function _waitForReady(window) {
145 var _this3 = this;
146
147 return (0, _promisePoller2.default)({
148 taskFn: function taskFn() {
149 return _this3.config.readyWhen(window) ? Promise.resolve() : Promise.reject();
150 },
151 interval: this.config.readyWhenInterval,
152 // timeout: this.config.readyWhenTimeout,
153 retries: this.config.readyWhenTimeout / this.config.readyWhenInterval }).catch(function (err) {
154 return Promise.reject(new Error('browser timeout'));
155 });
156 }
157 }]);
158
159 return Browser;
160}();
161
162exports.default = Browser;
\No newline at end of file