2020-03-20 20:28:31 +00:00
|
|
|
const { expect } = require('chai');
|
|
|
|
const { webFrame } = require('electron');
|
2018-07-10 19:43:39 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('webFrame module', function () {
|
2019-02-08 21:38:31 +00:00
|
|
|
it('top is self for top frame', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(webFrame.top.context).to.equal(webFrame.context);
|
|
|
|
});
|
2019-02-08 21:38:31 +00:00
|
|
|
|
|
|
|
it('opener is null for top frame', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(webFrame.opener).to.be.null();
|
|
|
|
});
|
2019-02-08 21:38:31 +00:00
|
|
|
|
|
|
|
it('firstChild is null for top frame', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(webFrame.firstChild).to.be.null();
|
|
|
|
});
|
2019-02-08 21:38:31 +00:00
|
|
|
|
|
|
|
it('getFrameForSelector() does not crash when not found', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(webFrame.getFrameForSelector('unexist-selector')).to.be.null();
|
|
|
|
});
|
2019-02-08 21:38:31 +00:00
|
|
|
|
|
|
|
it('findFrameByName() does not crash when not found', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(webFrame.findFrameByName('unexist-name')).to.be.null();
|
|
|
|
});
|
2019-02-08 21:38:31 +00:00
|
|
|
|
|
|
|
it('findFrameByRoutingId() does not crash when not found', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(webFrame.findFrameByRoutingId(-1)).to.be.null();
|
|
|
|
});
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
describe('executeJavaScript', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let childFrameElement, childFrame;
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
before(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
childFrameElement = document.createElement('iframe');
|
|
|
|
document.body.appendChild(childFrameElement);
|
|
|
|
childFrame = webFrame.firstChild;
|
|
|
|
});
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
after(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
childFrameElement.remove();
|
|
|
|
});
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
it('executeJavaScript() yields results via a promise and a sync callback', done => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let callbackResult, callbackError;
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
childFrame
|
|
|
|
.executeJavaScript('1 + 1', (result, error) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callbackResult = result;
|
|
|
|
callbackError = error;
|
2020-03-03 00:11:40 +00:00
|
|
|
})
|
|
|
|
.then(
|
|
|
|
promiseResult => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(promiseResult).to.equal(2);
|
|
|
|
done();
|
2020-03-03 00:11:40 +00:00
|
|
|
},
|
|
|
|
promiseError => {
|
2020-03-20 20:28:31 +00:00
|
|
|
done(promiseError);
|
2020-03-03 00:11:40 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
2020-03-03 00:11:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(callbackResult).to.equal(2);
|
|
|
|
expect(callbackError).to.be.undefined();
|
|
|
|
});
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
it('executeJavaScriptInIsolatedWorld() yields results via a promise and a sync callback', done => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let callbackResult, callbackError;
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
childFrame
|
|
|
|
.executeJavaScriptInIsolatedWorld(999, [{ code: '1 + 1' }], (result, error) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callbackResult = result;
|
|
|
|
callbackError = error;
|
2020-03-03 00:11:40 +00:00
|
|
|
})
|
|
|
|
.then(
|
|
|
|
promiseResult => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(promiseResult).to.equal(2);
|
|
|
|
done();
|
2020-03-03 00:11:40 +00:00
|
|
|
},
|
|
|
|
promiseError => {
|
2020-03-20 20:28:31 +00:00
|
|
|
done(promiseError);
|
2020-03-03 00:11:40 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
2020-03-03 00:11:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(callbackResult).to.equal(2);
|
|
|
|
expect(callbackError).to.be.undefined();
|
|
|
|
});
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
it('executeJavaScript() yields errors via a promise and a sync callback', done => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let callbackResult, callbackError;
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
childFrame
|
|
|
|
.executeJavaScript('thisShouldProduceAnError()', (result, error) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callbackResult = result;
|
|
|
|
callbackError = error;
|
2020-03-03 00:11:40 +00:00
|
|
|
})
|
|
|
|
.then(
|
|
|
|
promiseResult => {
|
2020-03-20 20:28:31 +00:00
|
|
|
done(new Error('error is expected'));
|
2020-03-03 00:11:40 +00:00
|
|
|
},
|
|
|
|
promiseError => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(promiseError).to.be.an('error');
|
|
|
|
done();
|
2020-03-03 00:11:40 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
2020-03-03 00:11:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(callbackResult).to.be.undefined();
|
|
|
|
expect(callbackError).to.be.an('error');
|
|
|
|
});
|
2020-03-03 00:11:40 +00:00
|
|
|
|
|
|
|
// executeJavaScriptInIsolatedWorld is failing to detect exec errors and is neither
|
|
|
|
// rejecting nor passing the error to the callback. This predates the reintroduction
|
|
|
|
// of the callback so will not be fixed as part of the callback PR
|
|
|
|
// if/when this is fixed the test can be uncommented.
|
|
|
|
//
|
|
|
|
// it('executeJavaScriptInIsolatedWorld() yields errors via a promise and a sync callback', done => {
|
|
|
|
// let callbackResult, callbackError
|
|
|
|
//
|
|
|
|
// childFrame
|
|
|
|
// .executeJavaScriptInIsolatedWorld(999, [{ code: 'thisShouldProduceAnError()' }], (result, error) => {
|
|
|
|
// callbackResult = result
|
|
|
|
// callbackError = error
|
|
|
|
// })
|
|
|
|
// .then(
|
|
|
|
// promiseResult => {
|
|
|
|
// done(new Error('error is expected'))
|
|
|
|
// },
|
|
|
|
// promiseError => {
|
|
|
|
// expect(promiseError).to.be.an('error')
|
|
|
|
// done()
|
|
|
|
// }
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// expect(callbackResult).to.be.undefined()
|
|
|
|
// expect(callbackError).to.be.an('error')
|
|
|
|
// })
|
|
|
|
|
|
|
|
it('executeJavaScript(InIsolatedWorld) can be used without a callback', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(await webFrame.executeJavaScript('1 + 1')).to.equal(2);
|
|
|
|
expect(await webFrame.executeJavaScriptInIsolatedWorld(999, [{ code: '1 + 1' }])).to.equal(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|