Skip to content

Commit

Permalink
fix: use selected project options for codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Feb 12, 2024
1 parent 112172a commit d2a8099
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/microsoft/playwright-vscode/issues"
},
"engines": {
"vscode": "^1.73.0"
"vscode": "^1.86.0"
},
"categories": [
"Testing"
Expand Down Expand Up @@ -119,7 +119,7 @@
"@types/glob": "^8.0.0",
"@types/node": "^18.11.9",
"@types/stack-utils": "^2.0.1",
"@types/vscode": "1.73.0",
"@types/vscode": "1.86.0",
"@types/which": "^2.0.1",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.44.0",
Expand Down
26 changes: 22 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type TestRunInfo = {
request: vscodeTypes.TestRunRequest;
};

const projectSymbol = Symbol('project');

export async function activate(context: vscodeTypes.ExtensionContext) {
// Do not await, quickly run the extension, schedule work.
new Extension(require('vscode')).activate(context);
Expand Down Expand Up @@ -178,18 +180,20 @@ export class Extension {
this._reusedBrowser.closeAllBrowsers();
}),
vscode.commands.registerCommand('pw.extension.command.recordNew', async () => {
if (!this._models.length) {
const selectedProject = await this._getDefaultTestProject();
if (!selectedProject) {
vscode.window.showWarningMessage(messageNoPlaywrightTestsFound);
return;
}
await this._reusedBrowser.record(this._models, true);
await this._reusedBrowser.record(selectedProject, true);
}),
vscode.commands.registerCommand('pw.extension.command.recordAtCursor', async () => {
if (!this._models.length) {
const selectedProject = await this._getDefaultTestProject();
if (!selectedProject) {
vscode.window.showWarningMessage(messageNoPlaywrightTestsFound);
return;
}
await this._reusedBrowser.record(this._models, false);
await this._reusedBrowser.record(selectedProject, false);
}),
vscode.workspace.onDidChangeTextDocument(() => {
if (this._completedSteps.size) {
Expand Down Expand Up @@ -336,6 +340,7 @@ export class Extension {
const projectTag = this._testTree.projectTag(project);
if (!runProfile) {
runProfile = this._testController.createRunProfile(`${projectPrefix}${folderName}${path.sep}${configName}`, this._vscode.TestRunProfileKind.Run, this._scheduleTestRunRequest.bind(this, configFile, project.name, false), false, projectTag);
(runProfile as any)[projectSymbol] = project;
this._runProfiles.set(keyPrefix + ':run', runProfile);
}
let debugProfile = this._runProfiles.get(keyPrefix + ':debug');
Expand All @@ -345,6 +350,19 @@ export class Extension {
}
}

private async _getDefaultTestProject(): Promise<TestProject | undefined> {
const defaultProjects = Array.from(this._runProfiles.values()).filter(project => project.isDefault).map(project => (project as any)[projectSymbol] as TestProject);
if (defaultProjects.length === 0)
return;
if (defaultProjects.length === 1)
return defaultProjects[0];
const selectedProject = await this._vscode.window.showQuickPick(defaultProjects.map(project => project.name), {
placeHolder: this._vscode.l10n.t('Select a project to run'),
canPickMany: false,
});
return selectedProject ? defaultProjects.find(project => project.name === selectedProject) : undefined;
}

private _scheduleTestRunRequest(configFile: string, projectName: string, isDebug: boolean, request: vscodeTypes.TestRunRequest) {
// Never run tests concurrently.
if (this._testRun)
Expand Down
9 changes: 7 additions & 2 deletions src/listTests.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

import type { TestError } from './reporter';

// This matches the structs in packages/playwright-test/src/runner/runner.ts.
// This matches the structs in packages/playwright/src/runner/runner.ts

export type ProjectConfigWithFiles = {
name: string;
testDir: string;
use: { testIdAttribute?: string };
use: {
testIdAttribute?: string;
browserName?: string;
contextOptions: Record<string, string>;
launchOptions: Record<string, string>;
};
files: string[];
};

Expand Down
1 change: 0 additions & 1 deletion src/playwrightTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export type TestConfig = {
configFile: string;
cli: string;
version: number;
testIdAttributeName?: string;
};

export interface TestListener {
Expand Down
19 changes: 13 additions & 6 deletions src/reusedBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ export class ReusedBrowser implements vscodeTypes.Disposable {
return !this._isRunningTests && !!this._pageCount;
}

async record(models: TestModel[], recordNew: boolean) {
if (!this._checkVersion(models[0].config))
async record(selectedProject: TestProject, recordNew: boolean) {
if (!this._checkVersion(selectedProject.model.config))
return;
if (!this.canRecord()) {
this._vscode.window.showWarningMessage(
Expand All @@ -321,7 +321,7 @@ export class ReusedBrowser implements vscodeTypes.Disposable {
location: this._vscode.ProgressLocation.Notification,
title: 'Playwright codegen',
cancellable: true
}, async (progress, token) => this._doRecord(progress, models[0], recordNew, token));
}, async (progress, token) => this._doRecord(progress, selectedProject, recordNew, token));
}

highlight(selector: string) {
Expand Down Expand Up @@ -355,7 +355,8 @@ export class ReusedBrowser implements vscodeTypes.Disposable {
return true;
}

private async _doRecord(progress: vscodeTypes.Progress<{ message?: string; increment?: number }>, model: TestModel, recordNew: boolean, token: vscodeTypes.CancellationToken) {
private async _doRecord(progress: vscodeTypes.Progress<{ message?: string; increment?: number }>, selectedProject: TestProject, recordNew: boolean, token: vscodeTypes.CancellationToken) {
const model = selectedProject.model;
const startBackend = this._startBackendIfNeeded(model.config);
let editor: vscodeTypes.TextEditor | undefined;
if (recordNew)
Expand All @@ -374,7 +375,13 @@ export class ReusedBrowser implements vscodeTypes.Disposable {
}

try {
await this._backend?.setMode({ mode: 'recording', testIdAttributeName: model.config.testIdAttributeName });
await this._backend?.setMode({
mode: 'recording',
testIdAttributeName: selectedProject.testIdAttribute,
browserName: selectedProject.browserName,
launchOptions: selectedProject.launchOptions,
contextOptions: selectedProject.contextOptions,
});
} catch (e) {
showExceptionAsUserError(this._vscode, model, e as Error);
await this._reset(true);
Expand Down Expand Up @@ -515,7 +522,7 @@ export class Backend extends EventEmitter {
await this._send('navigate', params);
}

async setMode(params: { mode: 'none' | 'inspecting' | 'recording', testIdAttributeName?: string }) {
async setMode(params: { mode: 'none' | 'inspecting' | 'recording', testIdAttributeName?: string, browserName?: string, launchOptions?: Record<string, any>, contextOptions?: Record<string, any> }) {
await this._send('setRecorderMode', params);
}

Expand Down
10 changes: 9 additions & 1 deletion src/testModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export type TestProject = {
model: TestModel;
isFirst: boolean;
files: Map<string, TestFile>;

testIdAttribute?: string;
browserName?: string;
contextOptions: Record<string, string>;
launchOptions: Record<string, string>;
};

export class TestModel {
Expand Down Expand Up @@ -96,7 +101,6 @@ export class TestModel {
for (const file of project.files)
files.push(...await resolveSourceMap(file, this._fileToSources, this._sourceToFile));
project.files = files;
this.config.testIdAttributeName = project.use?.testIdAttribute;
}

const projectsToKeep = new Set<string>();
Expand All @@ -123,6 +127,10 @@ export class TestModel {
...projectReport,
isFirst,
files: new Map(),
browserName: projectReport.use.browserName,
testIdAttribute: projectReport.use.testIdAttribute,
launchOptions: projectReport.use.launchOptions,
contextOptions: projectReport.use.contextOptions,
};
this.projects.set(project.name, project);
return project;
Expand Down

0 comments on commit d2a8099

Please sign in to comment.