api-web-frame-spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const { expect } = require('chai');
  2. const { webFrame } = require('electron');
  3. describe('webFrame module', function () {
  4. it('top is self for top frame', () => {
  5. expect(webFrame.top.context).to.equal(webFrame.context);
  6. });
  7. it('opener is null for top frame', () => {
  8. expect(webFrame.opener).to.be.null();
  9. });
  10. it('firstChild is null for top frame', () => {
  11. expect(webFrame.firstChild).to.be.null();
  12. });
  13. it('getFrameForSelector() does not crash when not found', () => {
  14. expect(webFrame.getFrameForSelector('unexist-selector')).to.be.null();
  15. });
  16. it('findFrameByName() does not crash when not found', () => {
  17. expect(webFrame.findFrameByName('unexist-name')).to.be.null();
  18. });
  19. it('findFrameByRoutingId() does not crash when not found', () => {
  20. expect(webFrame.findFrameByRoutingId(-1)).to.be.null();
  21. });
  22. describe('executeJavaScript', () => {
  23. let childFrameElement, childFrame;
  24. before(() => {
  25. childFrameElement = document.createElement('iframe');
  26. document.body.appendChild(childFrameElement);
  27. childFrame = webFrame.firstChild;
  28. });
  29. after(() => {
  30. childFrameElement.remove();
  31. });
  32. it('executeJavaScript() yields results via a promise and a sync callback', async () => {
  33. let callbackResult, callbackError;
  34. const executeJavaScript = childFrame
  35. .executeJavaScript('1 + 1', (result, error) => {
  36. callbackResult = result;
  37. callbackError = error;
  38. });
  39. expect(callbackResult).to.equal(2);
  40. expect(callbackError).to.be.undefined();
  41. const promiseResult = await executeJavaScript;
  42. expect(promiseResult).to.equal(2);
  43. });
  44. it('executeJavaScriptInIsolatedWorld() yields results via a promise and a sync callback', async () => {
  45. let callbackResult, callbackError;
  46. const executeJavaScriptInIsolatedWorld = childFrame
  47. .executeJavaScriptInIsolatedWorld(999, [{ code: '1 + 1' }], (result, error) => {
  48. callbackResult = result;
  49. callbackError = error;
  50. });
  51. expect(callbackResult).to.equal(2);
  52. expect(callbackError).to.be.undefined();
  53. const promiseResult = await executeJavaScriptInIsolatedWorld;
  54. expect(promiseResult).to.equal(2);
  55. });
  56. it('executeJavaScript() yields errors via a promise and a sync callback', async () => {
  57. let callbackResult, callbackError;
  58. const executeJavaScript = childFrame
  59. .executeJavaScript('thisShouldProduceAnError()', (result, error) => {
  60. callbackResult = result;
  61. callbackError = error;
  62. });
  63. expect(callbackResult).to.be.undefined();
  64. expect(callbackError).to.be.an('error');
  65. await expect(executeJavaScript).to.eventually.be.rejected('error is expected');
  66. });
  67. // executeJavaScriptInIsolatedWorld is failing to detect exec errors and is neither
  68. // rejecting nor passing the error to the callback. This predates the reintroduction
  69. // of the callback so will not be fixed as part of the callback PR
  70. // if/when this is fixed the test can be uncommented.
  71. //
  72. // it('executeJavaScriptInIsolatedWorld() yields errors via a promise and a sync callback', done => {
  73. // let callbackResult, callbackError
  74. //
  75. // const executeJavaScriptInIsolatedWorld = childFrame
  76. // .executeJavaScriptInIsolatedWorld(999, [{ code: 'thisShouldProduceAnError()' }], (result, error) => {
  77. // callbackResult = result
  78. // callbackError = error
  79. // });
  80. //
  81. // expect(callbackResult).to.be.undefined()
  82. // expect(callbackError).to.be.an('error')
  83. //
  84. // expect(executeJavaScriptInIsolatedWorld).to.eventually.be.rejected('error is expected');
  85. // })
  86. it('executeJavaScript(InIsolatedWorld) can be used without a callback', async () => {
  87. expect(await webFrame.executeJavaScript('1 + 1')).to.equal(2);
  88. expect(await webFrame.executeJavaScriptInIsolatedWorld(999, [{ code: '1 + 1' }])).to.equal(2);
  89. });
  90. });
  91. });