api-web-frame-spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { BrowserWindow, ipcMain, WebContents } from 'electron/main';
  2. import { expect } from 'chai';
  3. import { once } from 'node:events';
  4. import * as path from 'node:path';
  5. import { defer } from './lib/spec-helpers';
  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. let win: BrowserWindow;
  72. before(async () => {
  73. win = new BrowserWindow({ show: false, webPreferences: { contextIsolation: false, nodeIntegration: true } });
  74. await win.loadURL('data:text/html,<iframe name="test"></iframe>');
  75. w = win.webContents;
  76. await w.executeJavaScript(`
  77. var { webFrame } = require('electron');
  78. var isSameWebFrame = (a, b) => a.context === b.context;
  79. childFrame = webFrame.firstChild;
  80. null
  81. `);
  82. });
  83. after(() => {
  84. win.close();
  85. win = null as unknown as BrowserWindow;
  86. });
  87. describe('top', () => {
  88. it('is self for top frame', async () => {
  89. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.top, webFrame)');
  90. expect(equal).to.be.true();
  91. });
  92. it('is self for child frame', async () => {
  93. const equal = await w.executeJavaScript('isSameWebFrame(childFrame.top, webFrame)');
  94. expect(equal).to.be.true();
  95. });
  96. });
  97. describe('opener', () => {
  98. it('is null for top frame', async () => {
  99. const equal = await w.executeJavaScript('webFrame.opener === null');
  100. expect(equal).to.be.true();
  101. });
  102. });
  103. describe('parent', () => {
  104. it('is null for top frame', async () => {
  105. const equal = await w.executeJavaScript('webFrame.parent === null');
  106. expect(equal).to.be.true();
  107. });
  108. it('is top frame for child frame', async () => {
  109. const equal = await w.executeJavaScript('isSameWebFrame(childFrame.parent, webFrame)');
  110. expect(equal).to.be.true();
  111. });
  112. });
  113. describe('firstChild', () => {
  114. it('is child frame for top frame', async () => {
  115. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.firstChild, childFrame)');
  116. expect(equal).to.be.true();
  117. });
  118. it('is null for child frame', async () => {
  119. const equal = await w.executeJavaScript('childFrame.firstChild === null');
  120. expect(equal).to.be.true();
  121. });
  122. });
  123. describe('getFrameForSelector()', () => {
  124. it('does not crash when not found', async () => {
  125. const equal = await w.executeJavaScript('webFrame.getFrameForSelector("unexist-selector") === null');
  126. expect(equal).to.be.true();
  127. });
  128. it('returns the webFrame when found', async () => {
  129. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.getFrameForSelector("iframe"), childFrame)');
  130. expect(equal).to.be.true();
  131. });
  132. });
  133. describe('findFrameByName()', () => {
  134. it('does not crash when not found', async () => {
  135. const equal = await w.executeJavaScript('webFrame.findFrameByName("unexist-name") === null');
  136. expect(equal).to.be.true();
  137. });
  138. it('returns the webFrame when found', async () => {
  139. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.findFrameByName("test"), childFrame)');
  140. expect(equal).to.be.true();
  141. });
  142. });
  143. describe('findFrameByRoutingId()', () => {
  144. it('does not crash when not found', async () => {
  145. const equal = await w.executeJavaScript('webFrame.findFrameByRoutingId(-1) === null');
  146. expect(equal).to.be.true();
  147. });
  148. it('returns the webFrame when found', async () => {
  149. const equal = await w.executeJavaScript('isSameWebFrame(webFrame.findFrameByRoutingId(childFrame.routingId), childFrame)');
  150. expect(equal).to.be.true();
  151. });
  152. });
  153. describe('setZoomFactor()', () => {
  154. it('works', async () => {
  155. const zoom = await w.executeJavaScript('childFrame.setZoomFactor(2.0); childFrame.getZoomFactor()');
  156. expect(zoom).to.equal(2.0);
  157. });
  158. });
  159. describe('setZoomLevel()', () => {
  160. it('works', async () => {
  161. const zoom = await w.executeJavaScript('childFrame.setZoomLevel(5); childFrame.getZoomLevel()');
  162. expect(zoom).to.equal(5);
  163. });
  164. });
  165. describe('getResourceUsage()', () => {
  166. it('works', async () => {
  167. const result = await w.executeJavaScript('childFrame.getResourceUsage()');
  168. expect(result).to.have.property('images').that.is.an('object');
  169. expect(result).to.have.property('scripts').that.is.an('object');
  170. expect(result).to.have.property('cssStyleSheets').that.is.an('object');
  171. expect(result).to.have.property('xslStyleSheets').that.is.an('object');
  172. expect(result).to.have.property('fonts').that.is.an('object');
  173. expect(result).to.have.property('other').that.is.an('object');
  174. });
  175. });
  176. describe('executeJavaScript', () => {
  177. it('executeJavaScript() yields results via a promise and a sync callback', async () => {
  178. const { callbackResult, callbackError, result } = await w.executeJavaScript(`new Promise(resolve => {
  179. let callbackResult, callbackError;
  180. childFrame
  181. .executeJavaScript('1 + 1', (result, error) => {
  182. callbackResult = result;
  183. callbackError = error;
  184. }).then(result => resolve({callbackResult, callbackError, result}))
  185. })`);
  186. expect(callbackResult).to.equal(2);
  187. expect(callbackError).to.be.undefined();
  188. expect(result).to.equal(2);
  189. });
  190. it('executeJavaScriptInIsolatedWorld() yields results via a promise and a sync callback', async () => {
  191. const { callbackResult, callbackError, result } = await w.executeJavaScript(`new Promise(resolve => {
  192. let callbackResult, callbackError;
  193. childFrame
  194. .executeJavaScriptInIsolatedWorld(999, [{code: '1 + 1'}], (result, error) => {
  195. callbackResult = result;
  196. callbackError = error;
  197. }).then(result => resolve({callbackResult, callbackError, result}))
  198. })`);
  199. expect(callbackResult).to.equal(2);
  200. expect(callbackError).to.be.undefined();
  201. expect(result).to.equal(2);
  202. });
  203. it('executeJavaScript() yields errors via a promise and a sync callback', async () => {
  204. const { callbackResult, callbackError, error } = await w.executeJavaScript(`new Promise(resolve => {
  205. let callbackResult, callbackError;
  206. childFrame
  207. .executeJavaScript('thisShouldProduceAnError()', (result, error) => {
  208. callbackResult = result;
  209. callbackError = error;
  210. }).then(result => {throw new Error}, error => resolve({callbackResult, callbackError, error}))
  211. })`);
  212. expect(callbackResult).to.be.undefined();
  213. expect(callbackError).to.be.an('error');
  214. expect(error).to.be.an('error');
  215. });
  216. it('executeJavaScriptInIsolatedWorld() yields errors via a promise and a sync callback', async () => {
  217. const { callbackResult, callbackError, error } = await w.executeJavaScript(`new Promise(resolve => {
  218. let callbackResult, callbackError;
  219. childFrame
  220. .executeJavaScriptInIsolatedWorld(999, [{code: 'thisShouldProduceAnError()'}], (result, error) => {
  221. callbackResult = result;
  222. callbackError = error;
  223. }).then(result => {throw new Error}, error => resolve({callbackResult, callbackError, error}))
  224. })`);
  225. expect(callbackResult).to.be.undefined();
  226. expect(callbackError).to.be.an('error');
  227. expect(error).to.be.an('error');
  228. });
  229. it('executeJavaScript(InIsolatedWorld) can be used without a callback', async () => {
  230. expect(await w.executeJavaScript('webFrame.executeJavaScript(\'1 + 1\')')).to.equal(2);
  231. expect(await w.executeJavaScript('webFrame.executeJavaScriptInIsolatedWorld(999, [{code: \'1 + 1\'}])')).to.equal(2);
  232. });
  233. });
  234. });
  235. });