Skip to content

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

fix(addInitScript): tolerate trailing comments (#13275)

Pavel Feldmanb0103566c980
diff --git a/packages/playwright-core/src/client/clientHelper.ts b/packages/playwright-core/src/client/clientHelper.ts
index c4e3847d03..62959f5fd7 100644
--- a/packages/playwright-core/src/client/clientHelper.ts
+++ b/packages/playwright-core/src/client/clientHelper.ts
@@ -43,7 +43,7 @@ export async function evaluationScript(fun: Function | string | { path?: string,
if (fun.path !== undefined) {
let source = await fs.promises.readFile(fun.path, 'utf8');
if (addSourceUrl)
- source += '//# sourceURL=' + fun.path.replace(/\n/g, '');
+ source += '\n//# sourceURL=' + fun.path.replace(/\n/g, '');
return source;
}
throw new Error('Either path or content property must be present');
diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.ts
index 8044beb50e..4fd0bf55a6 100644
--- a/packages/playwright-core/src/server/webkit/wkPage.ts
+++ b/packages/playwright-core/src/server/webkit/wkPage.ts
@@ -777,7 +777,7 @@ export class WKPage implements PageDelegate {
scripts.push(this._bindingToScript(binding));
scripts.push(...this._browserContext.initScripts);
scripts.push(...this._page.initScripts);
- return scripts.join(';');
+ return scripts.join(';\n');
}
async _updateBootstrapScript(): Promise<void> {
diff --git a/tests/page/page-add-init-script.spec.ts b/tests/page/page-add-init-script.spec.ts
index 0477e95c61..f0a61e52c8 100644
--- a/tests/page/page-add-init-script.spec.ts
+++ b/tests/page/page-add-init-script.spec.ts
@@ -43,6 +43,13 @@ it('should throw without path and content', async ({ page }) => {
expect(error.message).toContain('Either path or content property must be present');
});
+it('should work with trailing comments', async ({ page, asset }) => {
+ await page.addInitScript({ content: '// comment' });
+ await page.addInitScript({ content: 'window.secret = 42;' });
+ await page.goto('data:text/html,<html></html>');
+ expect(await page.evaluate('secret')).toBe(42);
+});
+
it('should support multiple scripts', async ({ page, server }) => {
await page.addInitScript(function() {
window['script1'] = 1;