Skip to content

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

fix(codegen): comment-out generated expects for library scripts (#28118)

Dmitry Gozman16aee8b5d090
diff --git a/packages/playwright-core/src/server/recorder/javascript.ts b/packages/playwright-core/src/server/recorder/javascript.ts
index 71e60d6f7b..a68d3212fa 100644
--- a/packages/playwright-core/src/server/recorder/javascript.ts
+++ b/packages/playwright-core/src/server/recorder/javascript.ts
@@ -126,12 +126,12 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator {
case 'select':
return `await ${subject}.${this._asLocator(action.selector)}.selectOption(${formatObject(action.options.length > 1 ? action.options : action.options[0])});`;
case 'assertText':
- return `await expect(${subject}.${this._asLocator(action.selector)}).${action.substring ? 'toContainText' : 'toHaveText'}(${quote(action.text)});`;
+ return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).${action.substring ? 'toContainText' : 'toHaveText'}(${quote(action.text)});`;
case 'assertChecked':
- return `await expect(${subject}.${this._asLocator(action.selector)})${action.checked ? '' : '.not'}.toBeChecked();`;
+ return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)})${action.checked ? '' : '.not'}.toBeChecked();`;
case 'assertValue': {
const assertion = action.value ? `toHaveValue(${quote(action.value)})` : `toBeEmpty()`;
- return `await expect(${subject}.${this._asLocator(action.selector)}).${assertion};`;
+ return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).${assertion};`;
}
}
}
@@ -169,7 +169,7 @@ ${useText ? '\ntest.use(' + useText + ');\n' : ''}
generateStandaloneHeader(options: LanguageGeneratorOptions): string {
const formatter = new JavaScriptFormatter();
formatter.add(`
- const { expect, ${options.browserName}${options.deviceName ? ', devices' : ''} } = require('@playwright/test');
+ const { ${options.browserName}${options.deviceName ? ', devices' : ''} } = require('playwright');
(async () => {
const browser = await ${options.browserName}.launch(${formatObjectOrVoid(options.launchOptions)});
diff --git a/tests/library/inspector/cli-codegen-javascript.spec.ts b/tests/library/inspector/cli-codegen-javascript.spec.ts
index 198721bb06..f2c34899d8 100644
--- a/tests/library/inspector/cli-codegen-javascript.spec.ts
+++ b/tests/library/inspector/cli-codegen-javascript.spec.ts
@@ -26,7 +26,7 @@ const launchOptions = (channel: string) => {
test('should print the correct imports and context options', async ({ browserName, channel, runCLI }) => {
const cli = runCLI(['--target=javascript', emptyHTML]);
- const expectedResult = `const { expect, ${browserName} } = require('@playwright/test');
+ const expectedResult = `const { ${browserName} } = require('playwright');
(async () => {
const browser = await ${browserName}.launch({
@@ -38,7 +38,7 @@ test('should print the correct imports and context options', async ({ browserNam
test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI }) => {
const cli = runCLI(['--color-scheme=light', '--target=javascript', emptyHTML]);
- const expectedResult = `const { expect, ${browserName} } = require('@playwright/test');
+ const expectedResult = `const { ${browserName} } = require('playwright');
(async () => {
const browser = await ${browserName}.launch({
@@ -55,7 +55,7 @@ test('should print the correct context options when using a device', async ({ br
test.skip(browserName !== 'chromium');
const cli = runCLI(['--device=Pixel 2', '--target=javascript', emptyHTML]);
- const expectedResult = `const { expect, chromium, devices } = require('@playwright/test');
+ const expectedResult = `const { chromium, devices } = require('playwright');
(async () => {
const browser = await chromium.launch({
@@ -71,7 +71,7 @@ test('should print the correct context options when using a device and additiona
test.skip(browserName !== 'webkit');
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=javascript', emptyHTML]);
- const expectedResult = `const { expect, webkit, devices } = require('@playwright/test');
+ const expectedResult = `const { webkit, devices } = require('playwright');
(async () => {
const browser = await webkit.launch({
@@ -91,7 +91,7 @@ test('should save the codegen output to a file if specified', async ({ browserNa
});
await cli.waitForCleanExit();
const content = fs.readFileSync(tmpFile);
- expect(content.toString()).toBe(`const { expect, ${browserName} } = require('@playwright/test');
+ expect(content.toString()).toBe(`const { ${browserName} } = require('playwright');
(async () => {
const browser = await ${browserName}.launch({
@@ -113,7 +113,7 @@ test('should print load/save storageState', async ({ browserName, channel, runCL
const saveFileName = testInfo.outputPath('save.json');
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=javascript', emptyHTML]);
- const expectedResult1 = `const { expect, ${browserName} } = require('@playwright/test');
+ const expectedResult1 = `const { ${browserName} } = require('playwright');
(async () => {
const browser = await ${browserName}.launch({