@@ -36,6 +36,9 @@ For given versions of Electron you must depend on a very specific version range
3636| ` ^11.0.0 ` | ` ^13.0.0 ` |
3737| ` ^12.0.0 ` | ` ^14.0.0 ` |
3838| ` ^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 ` |
3942
4043Learn more from [ this presentation] ( https://speakerdeck.com/kevinsawicki/testing-your-electron-apps-with-chromedriver ) .
4144
@@ -67,15 +70,15 @@ cd test
6770Then simply include the following in your first ` spec.js ` .
6871
6972``` js
70- const Application = require (' spectron' ). Application
73+ const { Application } = require (' spectron' )
7174const assert = require (' assert' )
7275const electronPath = require (' electron' ) // Require Electron from the binaries included in node_modules.
7376const path = require (' path' )
7477
7578describe (' Application launch' , function () {
7679 this .timeout (10000 )
7780
78- beforeEach (function () {
81+ beforeEach (async function () {
7982 this .app = new Application ({
8083 // Your electron path can be any binary
8184 // i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
@@ -97,21 +100,20 @@ describe('Application launch', function () {
97100 // and the package.json located 1 level above.
98101 args: [path .join (__dirname , ' ..' )]
99102 })
100- return this .app .start ()
103+ await this .app .start ()
101104 })
102105
103- afterEach (function () {
106+ afterEach (async function () {
104107 if (this .app && this .app .isRunning ()) {
105- return this .app .stop ()
108+ await this .app .stop ()
106109 }
107110 })
108111
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)
115117 })
116118})
117119```
@@ -221,7 +223,7 @@ Spectron uses [WebdriverIO](https://webdriver.io) and exposes the managed
221223` client ` property on the created ` Application ` instances.
222224
223225The ` 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/ ) .
225227
226228Several additional commands are provided specific to Electron.
227229
@@ -230,11 +232,9 @@ All the commands return a `Promise`.
230232So if you wanted to get the text of an element you would do:
231233
232234``` 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)
238238```
239239
240240#### electron
@@ -265,9 +265,8 @@ So if you wanted to check if the current window is visible in your tests you
265265would do:
266266
267267``` 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)
271270```
272271
273272It 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
280279screenshot.
281280
282281``` 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)
286284```
287285
288286#### webContents
@@ -308,12 +306,12 @@ returns a `Promise` that will raise any errors and resolve to `undefined` when
308306complete.
309307
310308``` 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+ }
317315` ` `
318316
319317##### executeJavaScript
@@ -322,10 +320,8 @@ returns a `Promise` that will resolve with the result of the last statement of t
322320script.
323321
324322` ` ` 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
329325` ` `
330326
331327#### mainProcess
@@ -339,9 +335,8 @@ So if you wanted to get the `argv` for the main process in your tests you would
339335do:
340336
341337` ` ` 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)
345340` ` `
346341
347342Properties 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
359354your tests you would do:
360355
361356` ` ` 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)
365359` ` `
366360
367361### Methods
@@ -403,10 +397,9 @@ after they are returned.
403397Returns a ` Promise ` that resolves to an array of string log messages
404398
405399` ` ` 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)
410403})
411404` ` `
412405
@@ -418,12 +411,11 @@ after they are returned.
418411Returns a ` Promise ` that resolves to an array of log objects.
419412
420413` ` ` 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 )
427419})
428420` ` `
429421
@@ -432,9 +424,8 @@ app.client.getRenderProcessLogs().then(function (logs) {
432424Get the selected text in the current window.
433425
434426` ` ` js
435- app .client .getSelectedText ().then (function (selectedText ) {
436- console .log (selectedText)
437- })
427+ const selectedText = await app .client .getSelectedText ()
428+ console .log (selectedText)
438429` ` `
439430
440431#### client.getWindowCount()
@@ -443,9 +434,8 @@ Gets the number of open windows.
443434` < webview> ` tags are also counted as separate windows.
444435
445436` ` ` js
446- app .client .getWindowCount ().then (function (count ) {
447- console .log (count)
448- })
437+ const count = await app .client .getWindowCount ()
438+ console .log (count)
449439` ` `
450440
451441#### client.waitUntilTextExists(selector, text, [timeout])
@@ -517,11 +507,10 @@ Returns an `audit` Object with the following properties:
517507 * ` url` - A String URL providing more details about the failed rule
518508
519509` ` ` 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+ }
525514` ` `
526515
527516See 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:
532521
533522` ` ` js
534523// 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+ }
541530
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+ }
553538` ` `
554539
555540## Continuous Integration
0 commit comments