api-web-contents-spec.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as chai from 'chai'
  2. import * as chaiAsPromised from 'chai-as-promised'
  3. import * as path from 'path'
  4. import { BrowserWindow, ipcMain, webContents } from 'electron'
  5. import { emittedOnce } from './events-helpers';
  6. import { closeWindow } from './window-helpers';
  7. const { expect } = chai
  8. chai.use(chaiAsPromised)
  9. const fixturesPath = path.resolve(__dirname, '../spec/fixtures')
  10. describe('webContents module', () => {
  11. let w: BrowserWindow = (null as unknown as BrowserWindow)
  12. beforeEach(() => {
  13. w = new BrowserWindow({
  14. show: false,
  15. width: 400,
  16. height: 400,
  17. webPreferences: {
  18. backgroundThrottling: false,
  19. nodeIntegration: true,
  20. sandbox: false,
  21. contextIsolation: false,
  22. webviewTag: true
  23. }
  24. })
  25. })
  26. afterEach(async () => {
  27. await closeWindow(w)
  28. w = (null as unknown as BrowserWindow)
  29. })
  30. describe('getAllWebContents() API', () => {
  31. it('returns an array of web contents', async () => {
  32. w.loadFile(path.join(fixturesPath, 'pages', 'webview-zoom-factor.html'))
  33. await emittedOnce(w.webContents, 'did-attach-webview')
  34. w.webContents.openDevTools()
  35. await emittedOnce(w.webContents, 'devtools-opened')
  36. const all = webContents.getAllWebContents().sort((a, b) => {
  37. return a.id - b.id
  38. })
  39. expect(all).to.have.length(3)
  40. expect(all[0].getType()).to.equal('window')
  41. expect(all[all.length - 2].getType()).to.equal('webview')
  42. expect(all[all.length - 1].getType()).to.equal('remote')
  43. })
  44. })
  45. describe('webContents.send(channel, args...)', () => {
  46. it('does not block node async APIs when sent before document is ready', (done) => {
  47. // Please reference https://github.com/electron/electron/issues/19368 if
  48. // this test fails.
  49. ipcMain.once('async-node-api-done', () => {
  50. done()
  51. })
  52. w.loadFile(path.join(fixturesPath, 'pages', 'send-after-node.html'))
  53. setTimeout(() => {
  54. w.webContents.send("test")
  55. }, 50)
  56. })
  57. })
  58. })