fix: promise support with webFrameMain.executeJavaScript (#35292)
* fix: promise support with webFrameMain.executeJavaScript * chore: reject when result is an error
This commit is contained in:
parent
8e4a168a13
commit
43182bf030
2 changed files with 58 additions and 12 deletions
|
@ -163,6 +163,44 @@ describe('webFrameMain module', () => {
|
|||
expect(await getUrl(webFrame.frames[0])).to.equal(fileUrl('frame-with-frame.html'));
|
||||
expect(await getUrl(webFrame.frames[0].frames[0])).to.equal(fileUrl('frame.html'));
|
||||
});
|
||||
|
||||
it('can resolve promise', async () => {
|
||||
const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
|
||||
await w.loadFile(path.join(subframesPath, 'frame.html'));
|
||||
const webFrame = w.webContents.mainFrame;
|
||||
const p = () => webFrame.executeJavaScript('new Promise(resolve => setTimeout(resolve(42), 2000));');
|
||||
const result = await p();
|
||||
expect(result).to.equal(42);
|
||||
});
|
||||
|
||||
it('can reject with error', async () => {
|
||||
const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
|
||||
await w.loadFile(path.join(subframesPath, 'frame.html'));
|
||||
const webFrame = w.webContents.mainFrame;
|
||||
const p = () => webFrame.executeJavaScript('new Promise((r,e) => setTimeout(e("error!"), 500));');
|
||||
await expect(p()).to.be.eventually.rejectedWith('error!');
|
||||
const errorTypes = new Set([
|
||||
Error,
|
||||
ReferenceError,
|
||||
EvalError,
|
||||
RangeError,
|
||||
SyntaxError,
|
||||
TypeError,
|
||||
URIError
|
||||
]);
|
||||
for (const error of errorTypes) {
|
||||
await expect(webFrame.executeJavaScript(`Promise.reject(new ${error.name}("Wamp-wamp"))`))
|
||||
.to.eventually.be.rejectedWith(/Error/);
|
||||
}
|
||||
});
|
||||
|
||||
it('can reject when script execution fails', async () => {
|
||||
const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
|
||||
await w.loadFile(path.join(subframesPath, 'frame.html'));
|
||||
const webFrame = w.webContents.mainFrame;
|
||||
const p = () => webFrame.executeJavaScript('console.log(test)');
|
||||
await expect(p()).to.be.eventually.rejectedWith(/ReferenceError/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('WebFrame.reload', () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue