fix(addInitScript): make it work on new pages without navigations (#5675)

This commit is contained in:
Dmitry Gozman 2021-03-02 16:03:48 -08:00 committed by GitHub
parent 2cdb6b49cd
commit ff243f1af5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 5 deletions

View file

@ -421,7 +421,9 @@ class FrameSession {
worldName: UTILITY_WORLD_NAME, worldName: UTILITY_WORLD_NAME,
}); });
for (const binding of this._crPage._browserContext._pageBindings.values()) for (const binding of this._crPage._browserContext._pageBindings.values())
frame._evaluateExpression(binding.source, false, {}, binding.world).catch(e => {}); frame._evaluateExpression(binding.source, false, undefined, binding.world).catch(e => {});
for (const source of this._crPage._browserContext._evaluateOnNewDocumentSources)
frame._evaluateExpression(source, false, undefined, 'main').catch(e => {});
} }
const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':'; const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':';
if (isInitialEmptyPage) { if (isInitialEmptyPage) {

View file

@ -176,9 +176,9 @@ export class WKPage implements PageDelegate {
promises.push(session.send('Page.overrideUserAgent', { value: contextOptions.userAgent })); promises.push(session.send('Page.overrideUserAgent', { value: contextOptions.userAgent }));
if (this._page._state.mediaType || this._page._state.colorScheme) if (this._page._state.mediaType || this._page._state.colorScheme)
promises.push(WKPage._setEmulateMedia(session, this._page._state.mediaType, this._page._state.colorScheme)); promises.push(WKPage._setEmulateMedia(session, this._page._state.mediaType, this._page._state.colorScheme));
promises.push(session.send('Page.setBootstrapScript', { source: this._calculateBootstrapScript() })); const bootstrapScript = this._calculateBootstrapScript();
for (const binding of this._browserContext._pageBindings.values()) promises.push(session.send('Page.setBootstrapScript', { source: bootstrapScript }));
promises.push(this._evaluateBindingScript(binding)); this._page.frames().map(frame => frame._evaluateExpression(bootstrapScript, false, undefined, 'main').catch(e => {}));
if (contextOptions.bypassCSP) if (contextOptions.bypassCSP)
promises.push(session.send('Page.setBypassCSP', { enabled: true })); promises.push(session.send('Page.setBypassCSP', { enabled: true }));
if (this._page._state.viewportSize) { if (this._page._state.viewportSize) {

View file

@ -66,7 +66,8 @@ it('should be callable from-inside addInitScript', async ({context, server}) =>
await page.addInitScript('window["woof"]("page")'); await page.addInitScript('window["woof"]("page")');
args = []; args = [];
await page.reload(); await page.reload();
expect(args).toEqual(['context', 'page']); expect(args).toContain('context');
expect(args).toContain('page');
}); });
it('exposeBindingHandle should work', async ({context}) => { it('exposeBindingHandle should work', async ({context}) => {

View file

@ -54,6 +54,39 @@ it('should work with browser context scripts', async ({ browser, server }) => {
await context.close(); await context.close();
}); });
it('should work without navigation, after all bindings', async ({ browser }) => {
const context = await browser.newContext();
let callback;
const promise = new Promise(f => callback = f);
await context.exposeFunction('woof', function(arg) {
callback(arg);
});
await context.addInitScript(() => {
window['woof']('hey');
window['temp'] = 123;
});
const page = await context.newPage();
expect(await page.evaluate(() => window['temp'])).toBe(123);
expect(await promise).toBe('hey');
await context.close();
});
it('should work without navigation in popup', async ({ browser }) => {
const context = await browser.newContext();
await context.addInitScript(() => window['temp'] = 123);
const page = await context.newPage();
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(() => window['win'] = window.open()),
]);
expect(await popup.evaluate(() => window['temp'])).toBe(123);
await context.close();
});
it('should work with browser context scripts with a path', async ({ browser, server }) => { it('should work with browser context scripts with a path', async ({ browser, server }) => {
const context = await browser.newContext(); const context = await browser.newContext();
await context.addInitScript({ path: path.join(__dirname, 'assets/injectedfile.js') }); await context.addInitScript({ path: path.join(__dirname, 'assets/injectedfile.js') });