@@ -70,15 +70,15 @@ cd test
7070Then simply include the following in your first ` spec.js ` .
7171
7272``` js
73- const Application = require (' spectron' ). Application
73+ const { Application } = require (' spectron' )
7474const assert = require (' assert' )
7575const electronPath = require (' electron' ) // Require Electron from the binaries included in node_modules.
7676const path = require (' path' )
7777
7878describe (' Application launch' , function () {
7979 this .timeout (10000 )
8080
81- beforeEach (function () {
81+ beforeEach (async function () {
8282 this .app = new Application ({
8383 // Your electron path can be any binary
8484 // i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
@@ -100,21 +100,20 @@ describe('Application launch', function () {
100100 // and the package.json located 1 level above.
101101 args: [path .join (__dirname , ' ..' )]
102102 })
103- return this .app .start ()
103+ await this .app .start ()
104104 })
105105
106- afterEach (function () {
106+ afterEach (async function () {
107107 if (this .app && this .app .isRunning ()) {
108- return this .app .stop ()
108+ await this .app .stop ()
109109 }
110110 })
111111
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)
118117 })
119118})
120119```
@@ -233,11 +232,9 @@ All the commands return a `Promise`.
233232So if you wanted to get the text of an element you would do:
234233
235234``` 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)
241238```
242239
243240#### electron
@@ -268,9 +265,8 @@ So if you wanted to check if the current window is visible in your tests you
268265would do:
269266
270267``` 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)
274270```
275271
276272It 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
283279screenshot.
284280
285281``` 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)
289284```
290285
291286#### webContents
@@ -311,12 +306,12 @@ returns a `Promise` that will raise any errors and resolve to `undefined` when
311306complete.
312307
313308``` 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+ }
320315` ` `
321316
322317##### executeJavaScript
@@ -325,10 +320,8 @@ returns a `Promise` that will resolve with the result of the last statement of t
325320script.
326321
327322` ` ` 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
332325` ` `
333326
334327#### mainProcess
@@ -342,9 +335,8 @@ So if you wanted to get the `argv` for the main process in your tests you would
342335do:
343336
344337` ` ` 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)
348340` ` `
349341
350342Properties 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
362354your tests you would do:
363355
364356` ` ` 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)
368359` ` `
369360
370361### Methods
@@ -406,10 +397,9 @@ after they are returned.
406397Returns a ` Promise ` that resolves to an array of string log messages
407398
408399` ` ` 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)
413403})
414404` ` `
415405
@@ -421,12 +411,11 @@ after they are returned.
421411Returns a ` Promise ` that resolves to an array of log objects.
422412
423413` ` ` 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 )
430419})
431420` ` `
432421
@@ -435,9 +424,8 @@ app.client.getRenderProcessLogs().then(function (logs) {
435424Get the selected text in the current window.
436425
437426` ` ` js
438- app .client .getSelectedText ().then (function (selectedText ) {
439- console .log (selectedText)
440- })
427+ const selectedText = await app .client .getSelectedText ()
428+ console .log (selectedText)
441429` ` `
442430
443431#### client.getWindowCount()
@@ -446,9 +434,8 @@ Gets the number of open windows.
446434` < webview> ` tags are also counted as separate windows.
447435
448436` ` ` js
449- app .client .getWindowCount ().then (function (count ) {
450- console .log (count)
451- })
437+ const count = await app .client .getWindowCount ()
438+ console .log (count)
452439` ` `
453440
454441#### client.waitUntilTextExists(selector, text, [timeout])
@@ -520,11 +507,10 @@ Returns an `audit` Object with the following properties:
520507 * ` url` - A String URL providing more details about the failed rule
521508
522509` ` ` 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+ }
528514` ` `
529515
530516See 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:
535521
536522` ` ` js
537523// 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+ }
544530
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+ }
556538` ` `
557539
558540## Continuous Integration
0 commit comments