-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathapplication.js
More file actions
327 lines (289 loc) · 8.82 KB
/
application.js
File metadata and controls
327 lines (289 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const Accessibility = require('./accessibility');
const Api = require('./api');
const ChromeDriver = require('./chrome-driver');
const DevNull = require('dev-null');
const fs = require('fs');
const path = require('path');
const WebDriver = require('webdriverio');
function Application(options) {
options = options || {};
this.host = options.host || '127.0.0.1';
this.port = parseInt(options.port, 10) || 9515;
this.quitTimeout = parseInt(options.quitTimeout, 10) || 1000;
this.startTimeout = parseInt(options.startTimeout, 10) || 5000;
this.waitTimeout = parseInt(options.waitTimeout, 10) || 5000;
this.connectionRetryCount = parseInt(options.connectionRetryCount, 10) || 10;
this.connectionRetryTimeout =
parseInt(options.connectionRetryTimeout, 10) || 30000;
this.nodePath = options.nodePath || process.execPath;
this.path = options.path;
this.args = options.args || [];
this.chromeDriverArgs = options.chromeDriverArgs || [];
this.env = options.env || {};
this.workingDirectory = options.cwd || process.cwd();
this.debuggerAddress = options.debuggerAddress;
this.chromeDriverLogPath = options.chromeDriverLogPath;
this.webdriverLogPath = options.webdriverLogPath;
this.webdriverOptions = options.webdriverOptions || {};
this.requireName = options.requireName || 'require';
this.api = new Api(this, this.requireName);
this.setupPromiseness();
}
Application.prototype.setupPromiseness = function () {
const self = this;
self.transferPromiseness = function (target, promise) {
self.api.transferPromiseness(target, promise);
};
};
Application.prototype.start = function () {
const self = this;
return self
.exists()
.then(function () {
return self.startChromeDriver();
})
.then(function () {
return self.createClient();
})
.then(function () {
return self.api.initialize();
})
.then(function () {
return self.client.setTimeouts(
self.waitTimeout,
self.waitTimeout,
self.waitTimeout
);
})
.then(function () {
self.running = true;
})
.then(function () {
return self;
});
};
Application.prototype.stop = function () {
const self = this;
if (!self.isRunning()) {
return Promise.reject(Error('Application not running'));
}
return new Promise(function (resolve, reject) {
const endClient = function () {
setTimeout(function () {
self.chromeDriver.stop();
self.running = false;
resolve(self);
}, self.quitTimeout);
};
if (self.api.nodeIntegration) {
// Check if electron remote app has really a quit() function
if (
Object.prototype.hasOwnProperty.call(self.client.electron, 'remote') &&
Object.prototype.hasOwnProperty.call(
self.client.electron.remote,
'app'
) &&
typeof self.client.electron.remote.app.quit === 'function'
) {
self.client.electron.remote.app.quit().then(endClient, reject);
} else {
const fakeAppQuit = new Promise(function (resolve, reject) {
try {
resolve();
} catch (error) {
reject(Error('fakeAppQuit Promise not valid'));
}
});
fakeAppQuit.then(endClient, reject);
}
} else {
self.client
.windowByIndex(0)
.then(function () {
self.client.closeWindow();
})
.then(endClient, reject);
}
});
};
Application.prototype.restart = function () {
const self = this;
return self.stop().then(function () {
return self.start();
});
};
Application.prototype.isRunning = function () {
return this.running;
};
Application.prototype.getSettings = function () {
return {
host: this.host,
port: this.port,
quitTimeout: this.quitTimeout,
startTimeout: this.startTimeout,
waitTimeout: this.waitTimeout,
connectionRetryCount: this.connectionRetryCount,
connectionRetryTimeout: this.connectionRetryTimeout,
nodePath: this.nodePath,
path: this.path,
args: this.args,
chromeDriverArgs: this.chromeDriverArgs,
env: this.env,
workingDirectory: this.workingDirectory,
debuggerAddress: this.debuggerAddress,
chromeDriverLogPath: this.chromeDriverLogPath,
webdriverLogPath: this.webdriverLogPath,
webdriverOptions: this.webdriverOptions,
requireName: this.requireName
};
};
Application.prototype.exists = function () {
const self = this;
return new Promise(function (resolve, reject) {
// Binary path is ignored by ChromeDriver if debuggerAddress is set
if (self.debuggerAddress) return resolve();
if (typeof self.path !== 'string') {
return reject(Error('Application path must be a string'));
}
fs.stat(self.path, function (error, stat) {
if (error) return reject(error);
if (stat.isFile()) return resolve();
reject(Error('Application path specified is not a file: ' + self.path));
});
});
};
Application.prototype.startChromeDriver = function () {
this.chromeDriver = new ChromeDriver(
this.host,
this.port,
this.nodePath,
this.startTimeout,
this.workingDirectory,
this.chromeDriverLogPath
);
return this.chromeDriver.start();
};
Application.prototype.createClient = function () {
const self = this;
return new Promise(function (resolve, reject) {
const args = [];
args.push('spectron-path=' + self.path);
self.args.forEach(function (arg, index) {
args.push('spectron-arg' + index + '=' + arg);
});
for (const name in self.env) {
if (Object.prototype.hasOwnProperty.call(self.env, name)) {
args.push('spectron-env-' + name + '=' + self.env[name]);
}
}
self.chromeDriverArgs.forEach(function (arg) {
args.push(arg);
});
const isWin = process.platform === 'win32';
const launcherPath = path.join(
__dirname,
isWin ? 'launcher.bat' : 'launcher.js'
);
if (process.env.APPVEYOR) {
args.push('no-sandbox');
}
const options = {
hostname: self.host,
port: self.port,
waitforTimeout: self.waitTimeout,
connectionRetryCount: self.connectionRetryCount,
connectionRetryTimeout: self.connectionRetryTimeout,
logLevel: 'silent',
capabilities: {
'goog:chromeOptions': {
binary: launcherPath,
args: args,
debuggerAddress: self.debuggerAddress,
windowTypes: ['app', 'webview']
}
},
logOutput: DevNull()
};
if (self.webdriverLogPath) {
options.outputDir = self.webdriverLogPath;
options.logLevel = 'trace';
}
Object.assign(options, self.webdriverOptions);
self.client = WebDriver.remote(options).then(function (remote) {
self.client = remote;
self.addCommands();
resolve();
}, reject);
});
};
Application.prototype.addCommands = function () {
this.client.addCommand('waitUntilTextExists', function (
selector,
text,
timeout
) {
const self = this;
return self
.waitUntil(async function () {
const elem = await self.$(selector);
const exists = await elem.isExisting();
if (!exists) {
return false;
}
const selectorText = await elem.getText();
return Array.isArray(selectorText)
? selectorText.some((s) => s.includes(text))
: selectorText.includes(text);
}, timeout)
.then(
function () {},
function (error) {
error.message = 'waitUntilTextExists ' + error.message;
throw error;
}
);
});
this.client.addCommand('waitUntilWindowLoaded', function (timeout) {
const self = this;
return self
.waitUntil(function () {
return self.webContents.isLoading().then(function (loading) {
return !loading;
});
}, timeout)
.then(
function () {},
function (error) {
error.message = 'waitUntilWindowLoaded ' + error.message;
throw error;
}
);
});
this.client.addCommand('getWindowCount', function () {
return this.getWindowHandles().then(function (handles) {
return handles.length;
});
});
this.client.addCommand('windowByIndex', function (index) {
const self = this;
return self.getWindowHandles().then(function (handles) {
return self.switchToWindow(handles[index]);
});
});
this.client.addCommand('getSelectedText', function () {
return this.execute(function () {
return window.getSelection().toString();
});
});
this.client.addCommand('getRenderProcessLogs', function () {
return this.getLogs('browser');
});
const self = this;
this.client.addCommand('getMainProcessLogs', function () {
const logs = self.chromeDriver.getLogs();
self.chromeDriver.clearLogs();
return logs;
});
Accessibility.addCommand(this.client, this.requireName);
};
module.exports = Application;