api-web-frame-spec.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { expect } from 'chai';
  2. import * as path from 'path';
  3. import { BrowserWindow, ipcMain } from 'electron/main';
  4. import { closeAllWindows } from './window-helpers';
  5. import { emittedOnce } from './events-helpers';
  6. describe('webFrame module', () => {
  7. const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures');
  8. afterEach(closeAllWindows);
  9. it('can use executeJavaScript', async () => {
  10. const w = new BrowserWindow({
  11. show: true,
  12. webPreferences: {
  13. nodeIntegration: true,
  14. contextIsolation: true,
  15. preload: path.join(fixtures, 'pages', 'world-safe-preload.js')
  16. }
  17. });
  18. const isSafe = emittedOnce(ipcMain, 'executejs-safe');
  19. w.loadURL('about:blank');
  20. const [, wasSafe] = await isSafe;
  21. expect(wasSafe).to.equal(true);
  22. });
  23. it('can use executeJavaScript and catch conversion errors', async () => {
  24. const w = new BrowserWindow({
  25. show: true,
  26. webPreferences: {
  27. nodeIntegration: true,
  28. contextIsolation: true,
  29. preload: path.join(fixtures, 'pages', 'world-safe-preload-error.js')
  30. }
  31. });
  32. const execError = emittedOnce(ipcMain, 'executejs-safe');
  33. w.loadURL('about:blank');
  34. const [, error] = await execError;
  35. expect(error).to.not.equal(null, 'Error should not be null');
  36. expect(error).to.have.property('message', 'Uncaught Error: An object could not be cloned.');
  37. });
  38. it('calls a spellcheck provider', async () => {
  39. const w = new BrowserWindow({
  40. show: false,
  41. webPreferences: {
  42. nodeIntegration: true,
  43. contextIsolation: false
  44. }
  45. });
  46. await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'));
  47. w.focus();
  48. await w.webContents.executeJavaScript('document.querySelector("input").focus()', true);
  49. const spellCheckerFeedback =
  50. new Promise<[string[], boolean]>(resolve => {
  51. ipcMain.on('spec-spell-check', (e, words, callbackDefined) => {
  52. if (words.length === 5) {
  53. // The API calls the provider after every completed word.
  54. // The promise is resolved only after this event is received with all words.
  55. resolve([words, callbackDefined]);
  56. }
  57. });
  58. });
  59. const inputText = 'spleling test you\'re ';
  60. for (const keyCode of inputText) {
  61. w.webContents.sendInputEvent({ type: 'char', keyCode });
  62. }
  63. const [words, callbackDefined] = await spellCheckerFeedback;
  64. expect(words.sort()).to.deep.equal(['spleling', 'test', 'you\'re', 'you', 're'].sort());
  65. expect(callbackDefined).to.be.true();
  66. });
  67. });