fix(page): support multiple bootstrap scripts

This commit is contained in:
Yury Semikhatsky 2019-11-22 14:08:09 -08:00
parent ac01262c88
commit 7d6e5c124c
2 changed files with 16 additions and 1 deletions

View file

@ -47,6 +47,7 @@ export class Page extends EventEmitter {
private _mouse: Mouse;
private _timeoutSettings: TimeoutSettings;
private _frameManager: FrameManager;
private _bootstrapScripts: string[] = [];
_javascriptEnabled = true;
private _viewport: Viewport | null = null;
private _screenshotTaskQueue: TaskQueue;
@ -345,7 +346,10 @@ export class Page extends EventEmitter {
}
async evaluateOnNewDocument(pageFunction: Function | string, ...args: Array<any>) {
const source = helper.evaluationString(pageFunction, ...args);
const script = helper.evaluationString(pageFunction, ...args);
this._bootstrapScripts.push(script);
const source = this._bootstrapScripts.join(';');
// TODO(yurys): support process swap on navigation.
await this._session.send('Page.setBootstrapScript', { source });
}

View file

@ -264,6 +264,17 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
await page.goto(server.PREFIX + '/tamperable.html');
expect(await page.evaluate(() => window.result)).toBe(123);
});
fit('should support multiple scripts', async({page, server}) => {
await page.evaluateOnNewDocument(function(){
window.script1 = 1;
});
await page.evaluateOnNewDocument(function(){
window.script2 = 2;
});
await page.goto(server.PREFIX + '/tamperable.html');
expect(await page.evaluate(() => window.script1)).toBe(1);
expect(await page.evaluate(() => window.script2)).toBe(2);
});
it('should work with CSP', async({page, server}) => {
server.setCSP('/empty.html', 'script-src ' + server.PREFIX);
await page.evaluateOnNewDocument(function(){