api-web-frame-spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { expect } from 'chai';
  2. import * as path from 'node:path';
  3. import { BrowserWindow, ipcMain, WebContents } from 'electron/main';
  4. import { defer } from './lib/spec-helpers';
  5. import { once } from 'node:events';
  6. describe('webFrame module', () => {
  7. const fixtures = path.resolve(__dirname, 'fixtures');
  8. it('can use executeJavaScript', async () => {
  9. const w = new BrowserWindow({
  10. show: false,
  11. webPreferences: {
  12. nodeIntegration: true,
  13. contextIsolation: true,
  14. preload: path.join(fixtures, 'pages', 'world-safe-preload.js')
  15. }
  16. });
  17. defer(() => w.close());
  18. const isSafe = once(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: false,
  26. webPreferences: {
  27. nodeIntegration: true,
  28. contextIsolation: true,
  29. preload: path.join(fixtures, 'pages', 'world-safe-preload-error.js')
  30. }
  31. });
  32. defer(() => w.close());
  33. const execError = once(ipcMain, 'executejs-safe');
  34. w.loadURL('about:blank');
  35. const [, error] = await execError;
  36. expect(error).to.not.equal(null, 'Error should not be null');
  37. expect(error).to.have.property('message', 'Uncaught Error: An object could not be cloned.');
  38. });
  39. it('calls a spellcheck provider', async () => {
  40. const w = new BrowserWindow({
  41. show: false,
  42. webPreferences: {
  43. nodeIntegration: true,
  44. contextIsolation: false
  45. }
  46. });
  47. defer(() => w.close());
  48. await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'));
  49. w.focus();
  50. await w.webContents.executeJavaScript('document.querySelector("input").focus()', true);
  51. const spellCheckerFeedback =
  52. new Promise<[string[], boolean]>(resolve => {
  53. ipcMain.on('spec-spell-check', (e, words, callbackDefined) => {
  54. if (words.length === 5) {
  55. // The API calls the provider after every completed word.
  56. // The promise is resolved only after this event is received with all words.
  57. resolve([words, callbackDefined]);
  58. }
  59. });
  60. });
  61. const inputText = 'spleling test you\'re ';
  62. for (const keyCode of inputText) {
  63. w.webContents.sendInputEvent({ type: 'char', keyCode });
  64. }
  65. const [words, callbackDefined] = await spellCheckerFeedback;
  66. expect(words.sort()).to.deep.equal(['spleling', 'test', 'you\'re', 'you', 're'].sort());
  67. expect(callbackDefined).to.be.true();
  68. });
  69. describe('api', () => {
  70. let w: WebContents;
  71. before(async () => {
  72. const win = new BrowserWindow({ show: false, webPreferences: { contextIsolation: false, nodeIntegration: true } });
  73. await win.loadURL('data:text/html,<iframe name="test"></iframe>');
  74. w = win.webContents;
  75. await w.executeJavaScript(`
  76. var { webFrame } = require('electron');
  77. var isSameWebFrame = (a, b) => a.context === b.context;
  78. childFrame = webFrame.firstChild;
  79. null
  80. `);
  81. });
  82. describe('top', () => {
  83. it('is self for top frame', async () => {
  84. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.top, webFrame)');
  85. expect(equal).to.be.true();
  86. });
  87. it('is self for child frame', async () => {
  88. const equal = await w.executeJavaScript('isSameWebFrame(childFrame.top, webFrame)');
  89. expect(equal).to.be.true();
  90. });
  91. });
  92. describe('opener', () => {
  93. it('is null for top frame', async () => {
  94. const equal = await w.executeJavaScript('webFrame.opener === null');
  95. expect(equal).to.be.true();
  96. });
  97. });
  98. describe('parent', () => {
  99. it('is null for top frame', async () => {
  100. const equal = await w.executeJavaScript('webFrame.parent === null');
  101. expect(equal).to.be.true();
  102. });
  103. it('is top frame for child frame', async () => {
  104. const equal = await w.executeJavaScript('isSameWebFrame(childFrame.parent, webFrame)');
  105. expect(equal).to.be.true();
  106. });
  107. });
  108. describe('firstChild', () => {
  109. it('is child frame for top frame', async () => {
  110. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.firstChild, childFrame)');
  111. expect(equal).to.be.true();
  112. });
  113. it('is null for child frame', async () => {
  114. const equal = await w.executeJavaScript('childFrame.firstChild === null');
  115. expect(equal).to.be.true();
  116. });
  117. });
  118. describe('getFrameForSelector()', () => {
  119. it('does not crash when not found', async () => {
  120. const equal = await w.executeJavaScript('webFrame.getFrameForSelector("unexist-selector") === null');
  121. expect(equal).to.be.true();
  122. });
  123. it('returns the webFrame when found', async () => {
  124. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.getFrameForSelector("iframe"), childFrame)');
  125. expect(equal).to.be.true();
  126. });
  127. });
  128. describe('findFrameByName()', () => {
  129. it('does not crash when not found', async () => {
  130. const equal = await w.executeJavaScript('webFrame.findFrameByName("unexist-name") === null');
  131. expect(equal).to.be.true();
  132. });
  133. it('returns the webFrame when found', async () => {
  134. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.findFrameByName("test"), childFrame)');
  135. expect(equal).to.be.true();
  136. });
  137. });
  138. describe('findFrameByRoutingId()', () => {
  139. it('does not crash when not found', async () => {
  140. const equal = await w.executeJavaScript('webFrame.findFrameByRoutingId(-1) === null');
  141. expect(equal).to.be.true();
  142. });
  143. it('returns the webFrame when found', async () => {
  144. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.findFrameByRoutingId(childFrame.routingId), childFrame)');
  145. expect(equal).to.be.true();
  146. });
  147. });
  148. describe('setZoomFactor()', () => {
  149. it('works', async () => {
  150. const equal = await w.executeJavaScript('childFrame.setZoomFactor(2.0); childFrame.getZoomFactor() === 2.0');
  151. expect(equal).to.be.true();
  152. });
  153. });
  154. describe('setZoomLevel()', () => {
  155. it('works', async () => {
  156. const equal = await w.executeJavaScript('childFrame.setZoomLevel(5); childFrame.getZoomLevel() === 5');
  157. expect(equal).to.be.true();
  158. });
  159. });
  160. describe('getResourceUsage()', () => {
  161. it('works', async () => {
  162. const result = await w.executeJavaScript('childFrame.getResourceUsage()');
  163. expect(result).to.have.property('images').that.is.an('object');
  164. expect(result).to.have.property('scripts').that.is.an('object');
  165. expect(result).to.have.property('cssStyleSheets').that.is.an('object');
  166. expect(result).to.have.property('xslStyleSheets').that.is.an('object');
  167. expect(result).to.have.property('fonts').that.is.an('object');
  168. expect(result).to.have.property('other').that.is.an('object');
  169. });
  170. });
  171. describe('executeJavaScript', () => {
  172. it('executeJavaScript() yields results via a promise and a sync callback', async () => {
  173. const { callbackResult, callbackError, result } = await w.executeJavaScript(`new Promise(resolve => {
  174. let callbackResult, callbackError;
  175. childFrame
  176. .executeJavaScript('1 + 1', (result, error) => {
  177. callbackResult = result;
  178. callbackError = error;
  179. }).then(result => resolve({callbackResult, callbackError, result}))
  180. })`);
  181. expect(callbackResult).to.equal(2);
  182. expect(callbackError).to.be.undefined();
  183. expect(result).to.equal(2);
  184. });
  185. it('executeJavaScriptInIsolatedWorld() yields results via a promise and a sync callback', async () => {
  186. const { callbackResult, callbackError, result } = await w.executeJavaScript(`new Promise(resolve => {
  187. let callbackResult, callbackError;
  188. childFrame
  189. .executeJavaScriptInIsolatedWorld(999, [{code: '1 + 1'}], (result, error) => {
  190. callbackResult = result;
  191. callbackError = error;
  192. }).then(result => resolve({callbackResult, callbackError, result}))
  193. })`);
  194. expect(callbackResult).to.equal(2);
  195. expect(callbackError).to.be.undefined();
  196. expect(result).to.equal(2);
  197. });
  198. it('executeJavaScript() yields errors via a promise and a sync callback', async () => {
  199. const { callbackResult, callbackError, error } = await w.executeJavaScript(`new Promise(resolve => {
  200. let callbackResult, callbackError;
  201. childFrame
  202. .executeJavaScript('thisShouldProduceAnError()', (result, error) => {
  203. callbackResult = result;
  204. callbackError = error;
  205. }).then(result => {throw new Error}, error => resolve({callbackResult, callbackError, error}))
  206. })`);
  207. expect(callbackResult).to.be.undefined();
  208. expect(callbackError).to.be.an('error');
  209. expect(error).to.be.an('error');
  210. });
  211. it('executeJavaScriptInIsolatedWorld() yields errors via a promise and a sync callback', async () => {
  212. const { callbackResult, callbackError, error } = await w.executeJavaScript(`new Promise(resolve => {
  213. let callbackResult, callbackError;
  214. childFrame
  215. .executeJavaScriptInIsolatedWorld(999, [{code: 'thisShouldProduceAnError()'}], (result, error) => {
  216. callbackResult = result;
  217. callbackError = error;
  218. }).then(result => {throw new Error}, error => resolve({callbackResult, callbackError, error}))
  219. })`);
  220. expect(callbackResult).to.be.undefined();
  221. expect(callbackError).to.be.an('error');
  222. expect(error).to.be.an('error');
  223. });
  224. it('executeJavaScript(InIsolatedWorld) can be used without a callback', async () => {
  225. expect(await w.executeJavaScript('webFrame.executeJavaScript(\'1 + 1\')')).to.equal(2);
  226. expect(await w.executeJavaScript('webFrame.executeJavaScriptInIsolatedWorld(999, [{code: \'1 + 1\'}])')).to.equal(2);
  227. });
  228. });
  229. });
  230. });