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