2020-12-29 06:50:12 +08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-03-26 07:05:50 +08:00
|
|
|
import { contextTest } from '../../config/browserTest';
|
2024-10-22 18:44:18 +08:00
|
|
|
import type { Locator, Page } from 'playwright-core';
|
2023-04-13 00:37:24 +08:00
|
|
|
import { step } from '../../config/baseTest';
|
2021-04-03 02:19:26 +08:00
|
|
|
import * as path from 'path';
|
2025-08-12 20:50:31 +08:00
|
|
|
import fs from 'fs';
|
2022-09-21 05:32:21 +08:00
|
|
|
import type { Source } from '../../../packages/recorder/src/recorderTypes';
|
2022-04-07 05:57:14 +08:00
|
|
|
import type { CommonFixtures, TestChildProcess } from '../../config/commonFixtures';
|
2023-01-04 08:26:21 +08:00
|
|
|
import { expect } from '@playwright/test';
|
2025-02-13 11:27:24 +08:00
|
|
|
import { nodePlatform } from '../../../packages/playwright-core/lib/server/utils/nodePlatform';
|
2021-10-11 22:52:17 +08:00
|
|
|
export { expect } from '@playwright/test';
|
2020-12-29 06:50:12 +08:00
|
|
|
|
2021-04-30 02:11:32 +08:00
|
|
|
type CLITestArgs = {
|
2021-04-06 00:18:56 +08:00
|
|
|
recorderPageGetter: () => Promise<Page>;
|
2021-06-05 09:43:54 +08:00
|
|
|
closeRecorder: () => Promise<void>;
|
2025-07-31 03:38:20 +08:00
|
|
|
openRecorder: (options?: { testIdAttributeName?: string, language?: string }) => Promise<{ recorder: Recorder, page: Page }>;
|
2025-08-12 20:50:31 +08:00
|
|
|
runCLI: (args: string[]) => CLIMock;
|
2020-12-29 06:50:12 +08:00
|
|
|
};
|
|
|
|
|
2022-08-16 01:44:46 +08:00
|
|
|
const codegenLang2Id: Map<string, string> = new Map([
|
2023-05-21 01:15:33 +08:00
|
|
|
['JSON', 'jsonl'],
|
2022-08-16 01:44:46 +08:00
|
|
|
['JavaScript', 'javascript'],
|
|
|
|
['Java', 'java'],
|
2024-02-21 03:08:53 +08:00
|
|
|
['Java JUnit', 'java-junit'],
|
2022-08-16 01:44:46 +08:00
|
|
|
['Python', 'python'],
|
|
|
|
['Python Async', 'python-async'],
|
2022-10-26 00:55:20 +08:00
|
|
|
['Pytest', 'python-pytest'],
|
2022-08-16 01:44:46 +08:00
|
|
|
['C#', 'csharp'],
|
2022-08-25 17:58:58 +08:00
|
|
|
['C# NUnit', 'csharp-nunit'],
|
|
|
|
['C# MSTest', 'csharp-mstest'],
|
2022-10-26 00:55:20 +08:00
|
|
|
['Playwright Test', 'playwright-test'],
|
2022-08-16 01:44:46 +08:00
|
|
|
]);
|
|
|
|
const codegenLangId2lang = new Map([...codegenLang2Id.entries()].map(([lang, langId]) => [langId, lang]));
|
|
|
|
|
2025-02-11 02:22:32 +08:00
|
|
|
const playwrightToAutomateInspector = require('../../../packages/playwright-core/lib/inProcessFactory').createInProcessPlaywright(nodePlatform);
|
2021-10-02 08:06:13 +08:00
|
|
|
|
2021-05-17 10:58:26 +08:00
|
|
|
export const test = contextTest.extend<CLITestArgs>({
|
2021-06-05 09:43:54 +08:00
|
|
|
recorderPageGetter: async ({ context, toImpl, mode }, run, testInfo) => {
|
2023-07-26 07:47:04 +08:00
|
|
|
testInfo.skip(mode.startsWith('service'));
|
2021-05-17 10:58:26 +08:00
|
|
|
await run(async () => {
|
2021-04-30 02:11:32 +08:00
|
|
|
while (!toImpl(context).recorderAppForTest)
|
|
|
|
await new Promise(f => setTimeout(f, 100));
|
2024-09-18 09:26:44 +08:00
|
|
|
const wsEndpoint = toImpl(context).recorderAppForTest.wsEndpointForTest;
|
2021-10-02 08:06:13 +08:00
|
|
|
const browser = await playwrightToAutomateInspector.chromium.connectOverCDP({ wsEndpoint });
|
2021-04-30 02:11:32 +08:00
|
|
|
const c = browser.contexts()[0];
|
|
|
|
return c.pages()[0] || await c.waitForEvent('page');
|
2021-05-17 10:58:26 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-06-05 09:43:54 +08:00
|
|
|
closeRecorder: async ({ context, toImpl }, run) => {
|
|
|
|
await run(async () => {
|
|
|
|
await toImpl(context).recorderAppForTest.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2025-01-16 09:37:33 +08:00
|
|
|
runCLI: async ({ childProcess, browserName, channel, headless, mode, launchOptions }, run, testInfo) => {
|
2025-06-04 22:42:44 +08:00
|
|
|
testInfo.slow();
|
2023-07-26 07:47:04 +08:00
|
|
|
testInfo.skip(mode.startsWith('service'));
|
2021-05-17 10:58:26 +08:00
|
|
|
|
2025-08-12 20:50:31 +08:00
|
|
|
let cli: CLIMock | undefined;
|
|
|
|
await run(cliArgs => {
|
|
|
|
const outputFile = testInfo.outputPath('codegen.output');
|
|
|
|
cli = new CLIMock(childProcess, {
|
|
|
|
outputFile,
|
2024-09-21 06:25:49 +08:00
|
|
|
browserName,
|
|
|
|
channel,
|
|
|
|
headless,
|
|
|
|
args: cliArgs,
|
|
|
|
executablePath: launchOptions.executablePath,
|
|
|
|
});
|
2025-08-12 20:50:31 +08:00
|
|
|
return cli;
|
2021-05-17 10:58:26 +08:00
|
|
|
});
|
2025-08-12 20:50:31 +08:00
|
|
|
await cli?.exit();
|
2021-04-30 02:11:32 +08:00
|
|
|
},
|
|
|
|
|
2025-07-09 04:38:00 +08:00
|
|
|
openRecorder: async ({ context, recorderPageGetter }, use) => {
|
2025-06-28 04:40:43 +08:00
|
|
|
await use(async options => {
|
2024-09-21 06:25:49 +08:00
|
|
|
await (context as any)._enableRecorder({
|
|
|
|
mode: 'recording',
|
|
|
|
...options
|
|
|
|
});
|
2024-09-20 01:31:44 +08:00
|
|
|
const page = await context.newPage();
|
|
|
|
return { page, recorder: new Recorder(page, await recorderPageGetter()) };
|
2021-05-17 10:58:26 +08:00
|
|
|
});
|
2021-04-30 02:11:32 +08:00
|
|
|
},
|
|
|
|
});
|
2020-12-29 06:50:12 +08:00
|
|
|
|
2024-09-26 09:18:36 +08:00
|
|
|
export class Recorder {
|
2020-12-29 06:50:12 +08:00
|
|
|
page: Page;
|
2021-05-24 06:09:46 +08:00
|
|
|
_highlightCallback: Function;
|
|
|
|
_highlightInstalled: boolean;
|
|
|
|
_actionReporterInstalled: boolean;
|
|
|
|
_actionPerformedCallback: Function;
|
2021-02-14 14:13:51 +08:00
|
|
|
recorderPage: Page;
|
2021-02-17 10:13:26 +08:00
|
|
|
private _sources = new Map<string, Source>();
|
2020-12-29 06:50:12 +08:00
|
|
|
|
2021-02-14 14:13:51 +08:00
|
|
|
constructor(page: Page, recorderPage: Page) {
|
2020-12-29 06:50:12 +08:00
|
|
|
this.page = page;
|
2021-02-14 14:13:51 +08:00
|
|
|
this.recorderPage = recorderPage;
|
2020-12-29 06:50:12 +08:00
|
|
|
this._highlightCallback = () => { };
|
|
|
|
this._highlightInstalled = false;
|
|
|
|
this._actionReporterInstalled = false;
|
|
|
|
this._actionPerformedCallback = () => { };
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:19:36 +08:00
|
|
|
async setContentAndWait(content: string, url: string = 'about:blank', frameCount: number = 1) {
|
|
|
|
await this.setPageContentAndWait(this.page, content, url, frameCount);
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 05:19:36 +08:00
|
|
|
async setPageContentAndWait(page: Page, content: string, url: string = 'about:blank', frameCount: number = 1) {
|
2020-12-29 06:50:12 +08:00
|
|
|
let callback;
|
|
|
|
const result = new Promise(f => callback = f);
|
2021-05-13 06:19:27 +08:00
|
|
|
let msgCount = 0;
|
|
|
|
const listener = msg => {
|
|
|
|
if (msg.text() === 'Recorder script ready for test') {
|
|
|
|
++msgCount;
|
|
|
|
if (msgCount === frameCount) {
|
|
|
|
page.off('console', listener);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
page.on('console', listener);
|
2023-08-10 22:51:38 +08:00
|
|
|
await page.goto(url);
|
2020-12-29 06:50:12 +08:00
|
|
|
await Promise.all([
|
|
|
|
result,
|
|
|
|
page.setContent(content)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-02-17 10:13:26 +08:00
|
|
|
async waitForOutput(file: string, text: string): Promise<Map<string, Source>> {
|
2025-06-28 04:40:43 +08:00
|
|
|
return await test.step('waitForOutput', async () => {
|
|
|
|
if (!codegenLang2Id.has(file))
|
|
|
|
throw new Error(`Unknown language: ${file}`);
|
|
|
|
await expect.poll(() => this.recorderPage.evaluate(languageId => {
|
|
|
|
const sources = ((window as any).playwrightSourcesEchoForTest || []) as Source[];
|
|
|
|
return sources.find(s => s.id === languageId)?.text || '';
|
|
|
|
}, codegenLang2Id.get(file)), { timeout: 0 }).toContain(text);
|
|
|
|
const sources: Source[] = await this.recorderPage.evaluate(() => (window as any).playwrightSourcesEchoForTest || []);
|
|
|
|
for (const source of sources) {
|
|
|
|
if (!codegenLangId2lang.has(source.id))
|
|
|
|
throw new Error(`Unknown language: ${source.id}`);
|
|
|
|
this._sources.set(codegenLangId2lang.get(source.id), source);
|
|
|
|
}
|
|
|
|
return this._sources;
|
|
|
|
}, { box: true });
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 10:13:26 +08:00
|
|
|
sources(): Map<string, Source> {
|
|
|
|
return this._sources;
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
|
|
|
|
2024-10-16 06:21:45 +08:00
|
|
|
async text(file: string): Promise<string> {
|
|
|
|
const sources: Source[] = await this.recorderPage.evaluate(() => (window as any).playwrightSourcesEchoForTest || []);
|
|
|
|
for (const source of sources) {
|
|
|
|
if (codegenLangId2lang.get(source.id) === file)
|
|
|
|
return source.text;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-12-29 06:50:12 +08:00
|
|
|
async waitForHighlight(action: () => Promise<void>): Promise<string> {
|
2025-06-28 04:40:43 +08:00
|
|
|
return await test.step('waitForHighlight', async () => {
|
|
|
|
await this.page.$$eval('x-pw-highlight', els => els.forEach(e => e.remove()));
|
|
|
|
await this.page.$$eval('x-pw-tooltip', els => els.forEach(e => e.remove()));
|
|
|
|
await action();
|
|
|
|
await this.page.locator('x-pw-highlight').waitFor();
|
|
|
|
await this.page.locator('x-pw-tooltip').waitFor();
|
|
|
|
await expect(this.page.locator('x-pw-tooltip')).not.toHaveText('');
|
|
|
|
await expect(this.page.locator('x-pw-tooltip')).not.toHaveText(`locator('body')`);
|
|
|
|
return this.page.locator('x-pw-tooltip').textContent();
|
|
|
|
}, { box: true });
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
|
|
|
|
2024-10-16 07:21:55 +08:00
|
|
|
async waitForHighlightNoTooltip(action: () => Promise<void>): Promise<string> {
|
|
|
|
await this.page.$$eval('x-pw-highlight', els => els.forEach(e => e.remove()));
|
|
|
|
await action();
|
|
|
|
await this.page.locator('x-pw-highlight').waitFor();
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-12-29 06:50:12 +08:00
|
|
|
async waitForActionPerformed(): Promise<{ hovered: string | null, active: string | null }> {
|
2021-05-13 06:19:27 +08:00
|
|
|
let callback;
|
|
|
|
const listener = async msg => {
|
|
|
|
const prefix = 'Action performed for test: ';
|
|
|
|
if (msg.text().startsWith(prefix)) {
|
|
|
|
this.page.off('console', listener);
|
|
|
|
const arg = JSON.parse(msg.text().substr(prefix.length));
|
|
|
|
callback(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.page.on('console', listener);
|
|
|
|
return new Promise(f => callback = f);
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
|
|
|
|
2024-10-16 07:21:55 +08:00
|
|
|
async hoverOverElement(selector: string, options?: { position?: { x: number, y: number }, omitTooltip?: boolean }): Promise<string> {
|
|
|
|
return (options?.omitTooltip ? this.waitForHighlightNoTooltip : this.waitForHighlight).call(this, async () => {
|
2022-11-15 07:16:25 +08:00
|
|
|
const box = await this.page.locator(selector).first().boundingBox();
|
|
|
|
const offset = options?.position || { x: box.width / 2, y: box.height / 2 };
|
|
|
|
await this.page.mouse.move(box.x + offset.x, box.y + offset.y);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-22 18:44:18 +08:00
|
|
|
async trustedMove(selector: string | Locator) {
|
|
|
|
const locator = typeof selector === 'string' ? this.page.locator(selector).first() : selector;
|
|
|
|
const box = await locator.boundingBox();
|
2022-11-15 07:16:25 +08:00
|
|
|
await this.page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
|
|
|
|
}
|
|
|
|
|
2024-07-18 02:45:48 +08:00
|
|
|
async trustedClick(options?: { button?: 'left' | 'right' | 'middle' }) {
|
|
|
|
await this.page.mouse.down(options);
|
|
|
|
await this.page.mouse.up(options);
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
|
|
|
|
2025-01-29 09:59:16 +08:00
|
|
|
async trustedPress(text: string) {
|
|
|
|
await this.page.keyboard.press(text);
|
|
|
|
}
|
|
|
|
|
2024-09-13 02:40:44 +08:00
|
|
|
async trustedDblclick() {
|
|
|
|
await this.page.mouse.down();
|
|
|
|
await this.page.mouse.up();
|
|
|
|
await this.page.mouse.down({ clickCount: 2 });
|
|
|
|
await this.page.mouse.up();
|
|
|
|
}
|
|
|
|
|
2020-12-29 06:50:12 +08:00
|
|
|
async focusElement(selector: string): Promise<string> {
|
|
|
|
return this.waitForHighlight(() => this.page.focus(selector));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 02:11:32 +08:00
|
|
|
class CLIMock {
|
2025-08-12 20:50:31 +08:00
|
|
|
private _process: TestChildProcess;
|
|
|
|
private _outputFile: string;
|
|
|
|
private _exitPromise: Promise<void> | undefined;
|
2020-12-29 06:50:12 +08:00
|
|
|
|
2025-08-12 20:50:31 +08:00
|
|
|
constructor(childProcess: CommonFixtures['childProcess'], options: { outputFile: string, browserName: string, channel: string | undefined, headless: boolean | undefined, args: string[], executablePath: string | undefined }) {
|
|
|
|
this._outputFile = options.outputFile;
|
2021-03-15 23:07:57 +08:00
|
|
|
const nodeArgs = [
|
2021-09-22 07:24:48 +08:00
|
|
|
'node',
|
2024-01-24 07:22:09 +08:00
|
|
|
path.join(__dirname, '..', '..', '..', 'packages', 'playwright-core', 'cli.js'),
|
2021-03-15 23:07:57 +08:00
|
|
|
'codegen',
|
2024-09-21 06:25:49 +08:00
|
|
|
...options.args,
|
|
|
|
`--browser=${options.browserName}`,
|
2025-08-12 20:50:31 +08:00
|
|
|
`--output=${this._outputFile}`,
|
2021-03-15 23:07:57 +08:00
|
|
|
];
|
2024-09-21 06:25:49 +08:00
|
|
|
if (options.channel)
|
|
|
|
nodeArgs.push(`--channel=${options.channel}`);
|
2025-08-12 20:50:31 +08:00
|
|
|
this._process = childProcess({
|
2021-09-22 07:24:48 +08:00
|
|
|
command: nodeArgs,
|
2020-12-29 06:50:12 +08:00
|
|
|
env: {
|
2022-08-09 06:13:38 +08:00
|
|
|
PWTEST_CLI_IS_UNDER_TEST: '1',
|
2024-09-21 06:25:49 +08:00
|
|
|
PWTEST_CLI_HEADLESS: options.headless ? '1' : undefined,
|
|
|
|
PWTEST_CLI_EXECUTABLE_PATH: options.executablePath,
|
2022-07-21 06:32:57 +08:00
|
|
|
DEBUG: (process.env.DEBUG ?? '') + ',pw:browser*',
|
2021-01-14 04:52:03 +08:00
|
|
|
},
|
2020-12-29 06:50:12 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:37:24 +08:00
|
|
|
@step
|
|
|
|
async waitFor(text: string): Promise<void> {
|
2025-08-27 18:19:03 +08:00
|
|
|
await expect.poll(() => this.text(), { timeout: 30000 }).toContain(text);
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:37:24 +08:00
|
|
|
@step
|
2025-08-12 20:50:31 +08:00
|
|
|
async exit() {
|
|
|
|
if (!this._exitPromise) {
|
|
|
|
this._process.write('exit\n');
|
|
|
|
this._exitPromise = this._process.cleanExit();
|
|
|
|
}
|
|
|
|
await this._exitPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigint() {
|
|
|
|
const result = this._process.kill('SIGINT');
|
|
|
|
this._exitPromise = this._process.exited.then(() => undefined); // Avoid double closing.
|
|
|
|
return result;
|
2020-12-29 06:50:12 +08:00
|
|
|
}
|
2022-08-09 06:13:38 +08:00
|
|
|
|
2025-08-12 20:50:31 +08:00
|
|
|
async text() {
|
|
|
|
try {
|
|
|
|
return await fs.promises.readFile(this._outputFile, 'utf-8');
|
|
|
|
} catch {
|
|
|
|
return '';
|
|
|
|
}
|
2022-08-09 06:13:38 +08:00
|
|
|
}
|
2023-08-10 22:51:38 +08:00
|
|
|
}
|