Skip to content

Commit 72bddca

Browse files
authored
Merge branch 'master' into promise-types
2 parents 4e937f6 + c4b429f commit 72bddca

File tree

19 files changed

+8771
-1865
lines changed

19 files changed

+8771
-1865
lines changed

README.md

+62-77
Original file line numberDiff line numberDiff line change
@@ -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

4043
Learn more from [this presentation](https://speakerdeck.com/kevinsawicki/testing-your-electron-apps-with-chromedriver).
4144

@@ -67,15 +70,15 @@ cd test
6770
Then simply include the following in your first `spec.js`.
6871

6972
```js
70-
const Application = require('spectron').Application
73+
const { Application } = require('spectron')
7174
const assert = require('assert')
7275
const electronPath = require('electron') // Require Electron from the binaries included in node_modules.
7376
const path = require('path')
7477

7578
describe('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

223225
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/).
225227

226228
Several additional commands are provided specific to Electron.
227229

@@ -230,11 +232,9 @@ All the commands return a `Promise`.
230232
So 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
265265
would 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

273272
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
280279
screenshot.
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
308306
complete.
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
322320
script.
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
339335
do:
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
347342
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
359354
your 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.
403397
Returns 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.
418411
Returns 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) {
432424
Get 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
527516
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:
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

lib/api.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Api.prototype.loadApi = function () {
7979
);
8080
}
8181
const electron = window[requireName]('electron');
82+
electron.remote = window[requireName]('@electron/remote');
8283
const process = window[requireName]('process');
8384

8485
const api = {
@@ -350,9 +351,8 @@ Api.prototype.addCapturePageSupport = function () {
350351
async function (rect, requireName, done) {
351352
const args = [];
352353
if (rect != null) args.push(rect);
353-
const browserWindow = window[requireName](
354-
'electron'
355-
).remote.getCurrentWindow();
354+
const browserWindow =
355+
window[requireName]('@electron/remote').getCurrentWindow();
356356
const image = await browserWindow.capturePage.apply(
357357
browserWindow,
358358
args
@@ -413,9 +413,8 @@ Api.prototype.addSavePageSupport = function () {
413413
app.client.addCommand('webContents.savePage', function (fullPath, saveType) {
414414
return this.executeAsync(
415415
async function (fullPath, saveType, requireName, done) {
416-
const webContents = window[requireName](
417-
'electron'
418-
).remote.getCurrentWebContents();
416+
const webContents =
417+
window[requireName]('@electron/remote').getCurrentWebContents();
419418
await webContents.savePage(fullPath, saveType);
420419
done();
421420
},
@@ -445,9 +444,8 @@ Api.prototype.addExecuteJavaScriptSupport = function () {
445444
function (code, useGesture) {
446445
return this.executeAsync(
447446
async function (code, useGesture, requireName, done) {
448-
const webContents = window[requireName](
449-
'electron'
450-
).remote.getCurrentWebContents();
447+
const webContents =
448+
window[requireName]('@electron/remote').getCurrentWebContents();
451449
const result = await webContents.executeJavaScript(code, useGesture);
452450
done(result);
453451
},
@@ -538,7 +536,7 @@ function callRenderApi(moduleName, api, args, requireName) {
538536
}
539537

540538
function callMainApi(moduleName, api, args, requireName) {
541-
let module = window[requireName]('electron').remote;
539+
let module = window[requireName]('@electron/remote');
542540
if (moduleName) {
543541
module = module[moduleName];
544542
}
@@ -550,16 +548,14 @@ function callMainApi(moduleName, api, args, requireName) {
550548
}
551549

552550
function callWebContentsApi(name, args, requireName) {
553-
const webContents = window[requireName](
554-
'electron'
555-
).remote.getCurrentWebContents();
551+
const webContents =
552+
window[requireName]('@electron/remote').getCurrentWebContents();
556553
return webContents[name].apply(webContents, args);
557554
}
558555

559556
function callBrowserWindowApi(name, args, requireName) {
560-
const browserWindow = window[requireName](
561-
'electron'
562-
).remote.getCurrentWindow();
557+
const browserWindow =
558+
window[requireName]('@electron/remote').getCurrentWindow();
563559
return browserWindow[name].apply(browserWindow, args);
564560
}
565561

0 commit comments

Comments
 (0)