Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(page): support multiple bootstrap scripts (#59)

Yury Semikhatskyef847b19413e
diff --git a/src/webkit/Page.ts b/src/webkit/Page.ts
index 6adecb64e9..5c87b76f2b 100644
--- a/src/webkit/Page.ts
+++ b/src/webkit/Page.ts
@@ -48,6 +48,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;
@@ -346,7 +347,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 });
}
diff --git a/test/evaluation.spec.js b/test/evaluation.spec.js
index aa10248abd..f4c2b563ad 100644
--- a/test/evaluation.spec.js
+++ b/test/evaluation.spec.js
@@ -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(){