api-web-frame-main-spec.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { expect } from 'chai';
  2. import * as http from 'http';
  3. import * as path from 'path';
  4. import * as url from 'url';
  5. import { BrowserWindow, WebFrameMain, webFrameMain, ipcMain } from 'electron/main';
  6. import { closeAllWindows } from './window-helpers';
  7. import { emittedOnce, emittedNTimes } from './events-helpers';
  8. import { AddressInfo } from 'net';
  9. describe('webFrameMain module', () => {
  10. const fixtures = path.resolve(__dirname, '..', 'spec-main', 'fixtures');
  11. const subframesPath = path.join(fixtures, 'sub-frames');
  12. const fileUrl = (filename: string) => url.pathToFileURL(path.join(subframesPath, filename)).href;
  13. afterEach(closeAllWindows);
  14. describe('WebFrame traversal APIs', () => {
  15. let w: BrowserWindow;
  16. let webFrame: WebFrameMain;
  17. beforeEach(async () => {
  18. w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
  19. await w.loadFile(path.join(subframesPath, 'frame-with-frame-container.html'));
  20. webFrame = w.webContents.mainFrame;
  21. });
  22. it('can access top frame', () => {
  23. expect(webFrame.top).to.equal(webFrame);
  24. });
  25. it('has no parent on top frame', () => {
  26. expect(webFrame.parent).to.be.null();
  27. });
  28. it('can access immediate frame descendents', () => {
  29. const { frames } = webFrame;
  30. expect(frames).to.have.lengthOf(1);
  31. const subframe = frames[0];
  32. expect(subframe).not.to.equal(webFrame);
  33. expect(subframe.parent).to.equal(webFrame);
  34. });
  35. it('can access deeply nested frames', () => {
  36. const subframe = webFrame.frames[0];
  37. expect(subframe).not.to.equal(webFrame);
  38. expect(subframe.parent).to.equal(webFrame);
  39. const nestedSubframe = subframe.frames[0];
  40. expect(nestedSubframe).not.to.equal(webFrame);
  41. expect(nestedSubframe).not.to.equal(subframe);
  42. expect(nestedSubframe.parent).to.equal(subframe);
  43. });
  44. it('can traverse all frames in root', () => {
  45. const urls = webFrame.framesInSubtree.map(frame => frame.url);
  46. expect(urls).to.deep.equal([
  47. fileUrl('frame-with-frame-container.html'),
  48. fileUrl('frame-with-frame.html'),
  49. fileUrl('frame.html')
  50. ]);
  51. });
  52. it('can traverse all frames in subtree', () => {
  53. const urls = webFrame.frames[0].framesInSubtree.map(frame => frame.url);
  54. expect(urls).to.deep.equal([
  55. fileUrl('frame-with-frame.html'),
  56. fileUrl('frame.html')
  57. ]);
  58. });
  59. describe('cross-origin', () => {
  60. type Server = { server: http.Server, url: string }
  61. /** Creates an HTTP server whose handler embeds the given iframe src. */
  62. const createServer = () => new Promise<Server>(resolve => {
  63. const server = http.createServer((req, res) => {
  64. const params = new URLSearchParams(url.parse(req.url || '').search || '');
  65. if (params.has('frameSrc')) {
  66. res.end(`<iframe src="${params.get('frameSrc')}"></iframe>`);
  67. } else {
  68. res.end('');
  69. }
  70. });
  71. server.listen(0, '127.0.0.1', () => {
  72. const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/`;
  73. resolve({ server, url });
  74. });
  75. });
  76. let serverA = null as unknown as Server;
  77. let serverB = null as unknown as Server;
  78. before(async () => {
  79. serverA = await createServer();
  80. serverB = await createServer();
  81. });
  82. after(() => {
  83. serverA.server.close();
  84. serverB.server.close();
  85. });
  86. it('can access cross-origin frames', async () => {
  87. await w.loadURL(`${serverA.url}?frameSrc=${serverB.url}`);
  88. webFrame = w.webContents.mainFrame;
  89. expect(webFrame.url.startsWith(serverA.url)).to.be.true();
  90. expect(webFrame.frames[0].url).to.equal(serverB.url);
  91. });
  92. });
  93. });
  94. describe('WebFrame.url', () => {
  95. it('should report correct address for each subframe', async () => {
  96. const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
  97. await w.loadFile(path.join(subframesPath, 'frame-with-frame-container.html'));
  98. const webFrame = w.webContents.mainFrame;
  99. expect(webFrame.url).to.equal(fileUrl('frame-with-frame-container.html'));
  100. expect(webFrame.frames[0].url).to.equal(fileUrl('frame-with-frame.html'));
  101. expect(webFrame.frames[0].frames[0].url).to.equal(fileUrl('frame.html'));
  102. });
  103. });
  104. describe('WebFrame IDs', () => {
  105. it('has properties for various identifiers', async () => {
  106. const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
  107. await w.loadFile(path.join(subframesPath, 'frame.html'));
  108. const webFrame = w.webContents.mainFrame;
  109. expect(webFrame).to.have.ownProperty('url').that.is.a('string');
  110. expect(webFrame).to.have.ownProperty('frameTreeNodeId').that.is.a('number');
  111. expect(webFrame).to.have.ownProperty('name').that.is.a('string');
  112. expect(webFrame).to.have.ownProperty('osProcessId').that.is.a('number');
  113. expect(webFrame).to.have.ownProperty('processId').that.is.a('number');
  114. expect(webFrame).to.have.ownProperty('routingId').that.is.a('number');
  115. });
  116. });
  117. describe('WebFrame.executeJavaScript', () => {
  118. it('can inject code into any subframe', async () => {
  119. const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
  120. await w.loadFile(path.join(subframesPath, 'frame-with-frame-container.html'));
  121. const webFrame = w.webContents.mainFrame;
  122. const getUrl = (frame: WebFrameMain) => frame.executeJavaScript('location.href');
  123. expect(await getUrl(webFrame)).to.equal(fileUrl('frame-with-frame-container.html'));
  124. expect(await getUrl(webFrame.frames[0])).to.equal(fileUrl('frame-with-frame.html'));
  125. expect(await getUrl(webFrame.frames[0].frames[0])).to.equal(fileUrl('frame.html'));
  126. });
  127. });
  128. describe('WebFrame.reload', () => {
  129. it('reloads a frame', async () => {
  130. const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
  131. await w.loadFile(path.join(subframesPath, 'frame.html'));
  132. const webFrame = w.webContents.mainFrame;
  133. await webFrame.executeJavaScript('window.TEMP = 1', false);
  134. expect(webFrame.reload()).to.be.true();
  135. await emittedOnce(w.webContents, 'dom-ready');
  136. expect(await webFrame.executeJavaScript('window.TEMP', false)).to.be.null();
  137. });
  138. });
  139. describe('WebFrame.send', () => {
  140. it('works', async () => {
  141. const w = new BrowserWindow({
  142. show: false,
  143. webPreferences: {
  144. preload: path.join(subframesPath, 'preload.js'),
  145. nodeIntegrationInSubFrames: true
  146. }
  147. });
  148. await w.loadURL('about:blank');
  149. const webFrame = w.webContents.mainFrame;
  150. const pongPromise = emittedOnce(ipcMain, 'preload-pong');
  151. webFrame.send('preload-ping');
  152. const [, routingId] = await pongPromise;
  153. expect(routingId).to.equal(webFrame.routingId);
  154. });
  155. });
  156. describe('disposed WebFrames', () => {
  157. let w: BrowserWindow;
  158. let webFrame: WebFrameMain;
  159. before(async () => {
  160. w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
  161. await w.loadFile(path.join(subframesPath, 'frame-with-frame-container.html'));
  162. webFrame = w.webContents.mainFrame;
  163. w.destroy();
  164. // Wait for WebContents, and thus RenderFrameHost, to be destroyed.
  165. await new Promise(resolve => setTimeout(resolve, 0));
  166. });
  167. it('throws upon accessing properties', () => {
  168. expect(() => webFrame.url).to.throw();
  169. });
  170. });
  171. describe('webFrameMain.fromId', () => {
  172. it('returns undefined for unknown IDs', () => {
  173. expect(webFrameMain.fromId(0, 0)).to.be.undefined();
  174. });
  175. it('can find each frame from navigation events', async () => {
  176. const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
  177. // frame-with-frame-container.html, frame-with-frame.html, frame.html
  178. const didFrameFinishLoad = emittedNTimes(w.webContents, 'did-frame-finish-load', 3);
  179. w.loadFile(path.join(subframesPath, 'frame-with-frame-container.html'));
  180. for (const [, isMainFrame, frameProcessId, frameRoutingId] of await didFrameFinishLoad) {
  181. const frame = webFrameMain.fromId(frameProcessId, frameRoutingId);
  182. expect(frame).not.to.be.null();
  183. expect(frame?.processId).to.be.equal(frameProcessId);
  184. expect(frame?.routingId).to.be.equal(frameRoutingId);
  185. expect(frame?.top === frame).to.be.equal(isMainFrame);
  186. }
  187. });
  188. });
  189. });