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

This commit is contained in:
Pavel Feldman 2022-04-03 17:47:12 -08:00 committed by GitHub
parent 66a95c6897
commit b0103566c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 2 deletions

View File

@ -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');

View File

@ -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> {

View File

@ -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;