fix(addInitScript): require non-undefined arg to trigger commonjs module (#32282)

This commit is contained in:
Dmitry Gozman 2024-08-23 02:48:56 -07:00 committed by GitHub
parent 0b9c036505
commit 3a75f23ea1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 13 additions and 19 deletions

View File

@ -439,9 +439,9 @@ const mockPath = { path: path.resolve(__dirname, '../mocks/mockRandom.js') };
// Passing 42 as an argument to the default export function. // Passing 42 as an argument to the default export function.
await context.addInitScript({ path: mockPath }, 42); await context.addInitScript({ path: mockPath }, 42);
// Make sure to pass undefined even if you do not need to pass an argument. // Make sure to pass something even if you do not need to pass an argument.
// This instructs Playwright to treat the file as a commonjs module. // This instructs Playwright to treat the file as a commonjs module.
await context.addInitScript({ path: mockPath }, undefined); await context.addInitScript({ path: mockPath }, '');
``` ```
### param: BrowserContext.addInitScript.script ### param: BrowserContext.addInitScript.script

View File

@ -643,9 +643,9 @@ const mockPath = { path: path.resolve(__dirname, '../mocks/mockRandom.js') };
// Passing 42 as an argument to the default export function. // Passing 42 as an argument to the default export function.
await page.addInitScript({ path: mockPath }, 42); await page.addInitScript({ path: mockPath }, 42);
// Make sure to pass undefined even if you do not need to pass an argument. // Make sure to pass something even if you do not need to pass an argument.
// This instructs Playwright to treat the file as a commonjs module. // This instructs Playwright to treat the file as a commonjs module.
await page.addInitScript({ path: mockPath }, undefined); await page.addInitScript({ path: mockPath }, '');
``` ```
### param: Page.addInitScript.script ### param: Page.addInitScript.script

View File

@ -308,7 +308,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
} }
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any): Promise<void> { async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any): Promise<void> {
const source = await evaluationScript(script, arg, arguments.length > 1); const source = await evaluationScript(script, arg);
await this._channel.addInitScript({ source }); await this._channel.addInitScript({ source });
} }

View File

@ -28,7 +28,7 @@ export function envObjectToArray(env: types.Env): { name: string, value: string
return result; return result;
} }
export async function evaluationScript(fun: Function | string | { path?: string, content?: string }, arg: any, hasArg: boolean, addSourceUrl: boolean = true): Promise<string> { export async function evaluationScript(fun: Function | string | { path?: string, content?: string }, arg: any, addSourceUrl: boolean = true): Promise<string> {
if (typeof fun === 'function') { if (typeof fun === 'function') {
const source = fun.toString(); const source = fun.toString();
const argString = Object.is(arg, undefined) ? 'undefined' : JSON.stringify(arg); const argString = Object.is(arg, undefined) ? 'undefined' : JSON.stringify(arg);
@ -46,7 +46,7 @@ export async function evaluationScript(fun: Function | string | { path?: string,
} }
if (fun.path !== undefined) { if (fun.path !== undefined) {
let source = await fs.promises.readFile(fun.path, 'utf8'); let source = await fs.promises.readFile(fun.path, 'utf8');
if (hasArg) { if (arg !== undefined) {
// Assume a CJS module that has a function default export. // Assume a CJS module that has a function default export.
source = `(() => { source = `(() => {
var exports = {}; var module = { exports }; var exports = {}; var module = { exports };

View File

@ -492,7 +492,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
} }
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) { async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) {
const source = await evaluationScript(script, arg, arguments.length > 1); const source = await evaluationScript(script, arg);
await this._channel.addInitScript({ source }); await this._channel.addInitScript({ source });
} }

View File

@ -26,7 +26,7 @@ export class Selectors implements api.Selectors {
private _registrations: channels.SelectorsRegisterParams[] = []; private _registrations: channels.SelectorsRegisterParams[] = [];
async register(name: string, script: string | (() => SelectorEngine) | { path?: string, content?: string }, options: { contentScript?: boolean } = {}): Promise<void> { async register(name: string, script: string | (() => SelectorEngine) | { path?: string, content?: string }, options: { contentScript?: boolean } = {}): Promise<void> {
const source = await evaluationScript(script, undefined, false, false); const source = await evaluationScript(script, undefined, false);
const params = { ...options, name, source }; const params = { ...options, name, source };
for (const channel of this._channels) for (const channel of this._channels)
await channel._channel.register(params); await channel._channel.register(params);

View File

@ -312,9 +312,9 @@ export interface Page {
* // Passing 42 as an argument to the default export function. * // Passing 42 as an argument to the default export function.
* await page.addInitScript({ path: mockPath }, 42); * await page.addInitScript({ path: mockPath }, 42);
* *
* // Make sure to pass undefined even if you do not need to pass an argument. * // Make sure to pass something even if you do not need to pass an argument.
* // This instructs Playwright to treat the file as a commonjs module. * // This instructs Playwright to treat the file as a commonjs module.
* await page.addInitScript({ path: mockPath }, undefined); * await page.addInitScript({ path: mockPath }, '');
* ``` * ```
* *
* @param script Script to be evaluated in the page. * @param script Script to be evaluated in the page.
@ -7723,9 +7723,9 @@ export interface BrowserContext {
* // Passing 42 as an argument to the default export function. * // Passing 42 as an argument to the default export function.
* await context.addInitScript({ path: mockPath }, 42); * await context.addInitScript({ path: mockPath }, 42);
* *
* // Make sure to pass undefined even if you do not need to pass an argument. * // Make sure to pass something even if you do not need to pass an argument.
* // This instructs Playwright to treat the file as a commonjs module. * // This instructs Playwright to treat the file as a commonjs module.
* await context.addInitScript({ path: mockPath }, undefined); * await context.addInitScript({ path: mockPath }, '');
* ``` * ```
* *
* @param script Script to be evaluated in all pages in the browser context. * @param script Script to be evaluated in all pages in the browser context.

View File

@ -37,12 +37,6 @@ it('should assume CJS module with a path and arg', async ({ page, server, asset
expect(await page.evaluate(() => window['injected'])).toBe(17); expect(await page.evaluate(() => window['injected'])).toBe(17);
}); });
it('should assume CJS module with a path and undefined arg', async ({ page, server, asset }) => {
await page.addInitScript({ path: asset('injectedmodule.js') }, undefined);
await page.goto(server.EMPTY_PAGE);
expect(await page.evaluate(() => window['injected'])).toBe(42);
});
it('should work with content @smoke', async ({ page, server }) => { it('should work with content @smoke', async ({ page, server }) => {
await page.addInitScript({ content: 'window["injected"] = 123' }); await page.addInitScript({ content: 'window["injected"] = 123' });
await page.goto(server.PREFIX + '/tamperable.html'); await page.goto(server.PREFIX + '/tamperable.html');