@@ -36,6 +36,9 @@ For given versions of Electron you must depend on a very specific version range
36
36
| ` ^11.0.0 ` | ` ^13.0.0 ` |
37
37
| ` ^12.0.0 ` | ` ^14.0.0 ` |
38
38
| ` ^13.0.0 ` | ` ^15.0.0 ` |
39
+ | ` ^14.0.0 ` | ` ^16.0.0 ` |
40
+ | ` ^15.0.0 ` | ` ^17.0.0 ` |
41
+ | ` ^16.0.0 ` | ` ^18.0.0 ` |
39
42
40
43
Learn more from [ this presentation] ( https://speakerdeck.com/kevinsawicki/testing-your-electron-apps-with-chromedriver ) .
41
44
@@ -67,15 +70,15 @@ cd test
67
70
Then simply include the following in your first ` spec.js ` .
68
71
69
72
``` js
70
- const Application = require (' spectron' ). Application
73
+ const { Application } = require (' spectron' )
71
74
const assert = require (' assert' )
72
75
const electronPath = require (' electron' ) // Require Electron from the binaries included in node_modules.
73
76
const path = require (' path' )
74
77
75
78
describe (' Application launch' , function () {
76
79
this .timeout (10000 )
77
80
78
- beforeEach (function () {
81
+ beforeEach (async function () {
79
82
this .app = new Application ({
80
83
// Your electron path can be any binary
81
84
// i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
@@ -97,21 +100,20 @@ describe('Application launch', function () {
97
100
// and the package.json located 1 level above.
98
101
args: [path .join (__dirname , ' ..' )]
99
102
})
100
- return this .app .start ()
103
+ await this .app .start ()
101
104
})
102
105
103
- afterEach (function () {
106
+ afterEach (async function () {
104
107
if (this .app && this .app .isRunning ()) {
105
- return this .app .stop ()
108
+ await this .app .stop ()
106
109
}
107
110
})
108
111
109
- it (' shows an initial window' , function () {
110
- return this .app .client .getWindowCount ().then (function (count ) {
111
- assert .equal (count, 1 )
112
- // Please note that getWindowCount() will return 2 if `dev tools` are opened.
113
- // assert.equal(count, 2)
114
- })
112
+ it (' shows an initial window' , async function () {
113
+ const count = await this .app .client .getWindowCount ()
114
+ assert .equal (count, 1 )
115
+ // Please note that getWindowCount() will return 2 if `dev tools` are opened.
116
+ // assert.equal(count, 2)
115
117
})
116
118
})
117
119
```
@@ -221,7 +223,7 @@ Spectron uses [WebdriverIO](https://webdriver.io) and exposes the managed
221
223
` client ` property on the created ` Application ` instances.
222
224
223
225
The ` client ` API is WebdriverIO's ` browser ` object. Documentation can be found
224
- [ here] ( https://webdriver.io/docs/api ) .
226
+ [ here] ( https://webdriver.io/docs/browserobject/ ) .
225
227
226
228
Several additional commands are provided specific to Electron.
227
229
@@ -230,11 +232,9 @@ All the commands return a `Promise`.
230
232
So if you wanted to get the text of an element you would do:
231
233
232
234
``` js
233
- app .client .$ (' #error-alert' ).then (function (element ) {
234
- element .getText ().then (function (errorText ) {
235
- console .log (' The #error-alert text content is ' + errorText)
236
- })
237
- })
235
+ const element = await app .client .$ (' #error-alert' )
236
+ const errorText = await element .getText ()
237
+ console .log (' The #error-alert text content is ' + errorText)
238
238
```
239
239
240
240
#### electron
@@ -265,9 +265,8 @@ So if you wanted to check if the current window is visible in your tests you
265
265
would do:
266
266
267
267
``` js
268
- app .browserWindow .isVisible ().then (function (visible ) {
269
- console .log (' window is visible? ' + visible)
270
- })
268
+ const visible = await app .browserWindow .isVisible ()
269
+ console .log (' window is visible? ' + visible)
271
270
```
272
271
273
272
It is named ` browserWindow ` instead of ` window ` so that it doesn't collide
@@ -280,9 +279,8 @@ returns a `Promise` that resolves to a `Buffer` that is the image data of
280
279
screenshot.
281
280
282
281
``` js
283
- app .browserWindow .capturePage ().then (function (imageBuffer ) {
284
- fs .writeFile (' page.png' , imageBuffer)
285
- })
282
+ const imageBuffer = await app .browserWindow .capturePage ()
283
+ fs .writeFile (' page.png' , imageBuffer)
286
284
```
287
285
288
286
#### webContents
@@ -308,12 +306,12 @@ returns a `Promise` that will raise any errors and resolve to `undefined` when
308
306
complete.
309
307
310
308
``` js
311
- app . webContents . savePage ( ' /Users/kevin/page.html ' , ' HTMLComplete ' )
312
- . then ( function () {
313
- console .log (' page saved' )
314
- }). catch ( function (error ) {
315
- console .error (' saving page failed' , error .message )
316
- })
309
+ try {
310
+ await app . webContents . savePage ( ' /Users/kevin/page.html ' , ' HTMLComplete ' )
311
+ console .log (' page saved' )
312
+ catch (error) {
313
+ console .error (' saving page failed' , error .message )
314
+ }
317
315
` ` `
318
316
319
317
##### executeJavaScript
@@ -322,10 +320,8 @@ returns a `Promise` that will resolve with the result of the last statement of t
322
320
script.
323
321
324
322
` ` ` js
325
- app .webContents .executeJavaScript (' 1 + 2' )
326
- .then (function (result ) {
327
- console .log (result) // prints 3
328
- })
323
+ const result = await app .webContents .executeJavaScript (' 1 + 2' )
324
+ console .log (result) // prints 3
329
325
` ` `
330
326
331
327
#### mainProcess
@@ -339,9 +335,8 @@ So if you wanted to get the `argv` for the main process in your tests you would
339
335
do:
340
336
341
337
` ` ` js
342
- app .mainProcess .argv ().then (function (argv ) {
343
- console .log (' main process args: ' + argv)
344
- })
338
+ const argv = await app .mainProcess .argv ()
339
+ console .log (' main process args: ' + argv)
345
340
` ` `
346
341
347
342
Properties on the ` process ` are exposed as functions that return promises so
@@ -359,9 +354,8 @@ So if you wanted to get the environment variables for the renderer process in
359
354
your tests you would do:
360
355
361
356
` ` ` js
362
- app .rendererProcess .env ().then (function (env ) {
363
- console .log (' renderer process env variables: ' + env)
364
- })
357
+ const env = await app .rendererProcess .env ()
358
+ console .log (' renderer process env variables: ' + env)
365
359
` ` `
366
360
367
361
### Methods
@@ -403,10 +397,9 @@ after they are returned.
403
397
Returns a ` Promise ` that resolves to an array of string log messages
404
398
405
399
` ` ` js
406
- app .client .getMainProcessLogs ().then (function (logs ) {
407
- logs .forEach (function (log ) {
408
- console .log (log)
409
- })
400
+ const logs = await app .client .getMainProcessLogs ()
401
+ logs .forEach (function (log ) {
402
+ console .log (log)
410
403
})
411
404
` ` `
412
405
@@ -418,12 +411,11 @@ after they are returned.
418
411
Returns a ` Promise ` that resolves to an array of log objects.
419
412
420
413
` ` ` js
421
- app .client .getRenderProcessLogs ().then (function (logs ) {
422
- logs .forEach (function (log ) {
423
- console .log (log .message )
424
- console .log (log .source )
425
- console .log (log .level )
426
- })
414
+ const logs = await app .client .getRenderProcessLogs ()
415
+ logs .forEach (function (log ) {
416
+ console .log (log .message )
417
+ console .log (log .source )
418
+ console .log (log .level )
427
419
})
428
420
` ` `
429
421
@@ -432,9 +424,8 @@ app.client.getRenderProcessLogs().then(function (logs) {
432
424
Get the selected text in the current window.
433
425
434
426
` ` ` js
435
- app .client .getSelectedText ().then (function (selectedText ) {
436
- console .log (selectedText)
437
- })
427
+ const selectedText = await app .client .getSelectedText ()
428
+ console .log (selectedText)
438
429
` ` `
439
430
440
431
#### client.getWindowCount()
@@ -443,9 +434,8 @@ Gets the number of open windows.
443
434
` < webview> ` tags are also counted as separate windows.
444
435
445
436
` ` ` js
446
- app .client .getWindowCount ().then (function (count ) {
447
- console .log (count)
448
- })
437
+ const count = await app .client .getWindowCount ()
438
+ console .log (count)
449
439
` ` `
450
440
451
441
#### client.waitUntilTextExists(selector, text, [timeout])
@@ -517,11 +507,10 @@ Returns an `audit` Object with the following properties:
517
507
* ` url` - A String URL providing more details about the failed rule
518
508
519
509
` ` ` js
520
- app .client .auditAccessibility ().then (function (audit ) {
521
- if (audit .failed ) {
522
- console .error (audit .message )
523
- }
524
- })
510
+ const audit = await app .client .auditAccessibility ()
511
+ if (audit .failed ) {
512
+ console .error (audit .message )
513
+ }
525
514
` ` `
526
515
527
516
See https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules
@@ -532,24 +521,20 @@ page and the `<webview>`'s page then you will need to do the following:
532
521
533
522
` ` ` js
534
523
// Focus main page and audit it
535
- app .client .windowByIndex (0 ). then ( function () {
536
- app .client .auditAccessibility (). then ( function ( audit ) {
537
- if (audit .failed ) {
538
- console .error (' Main page failed audit' )
539
- console .error (audit .message )
540
- }
524
+ await app .client .windowByIndex (0 )
525
+ const audit = await app .client .auditAccessibility ()
526
+ if (audit .failed ) {
527
+ console .error (' Main page failed audit' )
528
+ console .error (audit .message )
529
+ }
541
530
542
- // Focus <webview> tag and audit it
543
- app .client .windowByIndex (1 ).then (function () {
544
- app .client .auditAccessibility ().then (function (audit ) {
545
- if (audit .failed ) {
546
- console .error (' <webview> page failed audit' )
547
- console .error (audit .message )
548
- }
549
- })
550
- })
551
- })
552
- })
531
+ // Focus <webview> tag and audit it
532
+ await app .client .windowByIndex (1 )
533
+ const audit = await app .client .auditAccessibility ()
534
+ if (audit .failed ) {
535
+ console .error (' <webview> page failed audit' )
536
+ console .error (audit .message )
537
+ }
553
538
` ` `
554
539
555
540
## Continuous Integration
0 commit comments