Skip to content

Commit

Permalink
feat: add toggle for always recreating global setup (#524)
Browse files Browse the repository at this point in the history
* feat: add toggle for always recreating global setup

* link issue

* move and rename

* hide manual commands

* move up
  • Loading branch information
Skn0tt authored Sep 2, 2024
1 parent 1d855b9 commit 6835805
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 5 deletions.
1 change: 1 addition & 0 deletions l10n/bundle.l10n.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Select Playwright Config": "Playwright-Konfiguration auswählen",
"Show browser mode does not work in remote vscode": "Der Browser kann nicht mit Remote VSCode angezeigt werden",
"Show browser": "Browser anzeigen",
"Run global setup on each run": "Globales Setup jedesmal ausführen",
"Show trace viewer": "Trace Viewer anzeigen",
"Start dev server": "Entwicklungsserver starten",
"Stop dev server": "Entwicklungsserver stoppen",
Expand Down
1 change: 1 addition & 0 deletions l10n/bundle.l10n.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Select Playwright Config": "Sélectionner la configuration Playwright",
"Show browser mode does not work in the Web mode": "Le mode affichage de navigateur ne peut être utilisé en mode Web",
"Show browser": "Afficher le navigateur",
"Run global setup on each run": "Exécuter la configuration globale chaque course",
"Show trace viewer": "Afficher le Trace Viewer",
"Start dev server": "Démarrer le serveur de développement",
"Stop dev server": "Arrêter le serveur de développement",
Expand Down
1 change: 1 addition & 0 deletions l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Select Playwright Config": "Select Playwright Config",
"Show browser mode does not work in remote vscode": "Show browser mode does not work in remote vscode",
"Show browser": "Show browser",
"Run global setup on each run": "Run global setup on each run",
"Show trace viewer": "Show trace viewer",
"Start dev server": "Start dev server",
"Stop dev server": "Stop dev server",
Expand Down
1 change: 1 addition & 0 deletions l10n/bundle.l10n.zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Select Playwright Config": "选择 Playwright 配置",
"Show browser mode does not work in remote vscode": "显示浏览器模式在远程vscode中不起作用",
"Show browser": "显示浏览器",
"Run global setup on each run": "每次运行全局设置",
"Show trace viewer": "显示 Trace Viewer",
"Start dev server": "启动 dev 服务器",
"Stop dev server": "停止 dev 服务器",
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
"command": "pw.extension.toggle.showTrace",
"title": "%contributes.command.pw.extension.toggle.showTrace%"
},
{
"category": "Test",
"command": "pw.extension.toggle.runGlobalSetupOnEachRun",
"title": "%contributes.command.pw.extension.toggle.runGlobalSetupOnEachRun%"
},
{
"category": "Test",
"command": "pw.extension.command.runGlobalSetup",
Expand Down Expand Up @@ -113,6 +118,11 @@
"type": "boolean",
"default": false,
"description": "%configuration.playwright.showTrace%"
},
"playwright.runGlobalSetupOnEachRun": {
"type": "boolean",
"default": false,
"description": "%configuration.playwright.runGlobalSetupOnEachRun%"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions package.nls.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"contributes.command.pw.extension.command.toggleModels": "Playwright Konfigurationen umschalten",
"contributes.command.pw.extension.toggle.reuseBrowser": "Wiederverwendung des Playwright Browsers umschalten",
"contributes.command.pw.extension.toggle.showTrace": "Playwright Trace Viewer umschalten",
"contributes.command.pw.extension.toggle.runGlobalSetupOnEachRun": "Wiederverwendung des globalen Setups umschalten",
"configuration.playwright.env": "Umgebungsvariablen, die an Playwright Test übergeben werden.",
"configuration.playwright.reuseBrowser": "Browser zwischen Tests anzeigen & wiederverwenden.",
"configuration.playwright.showTrace": "Trace Viewer anzeigen.",
"configuration.playwright.runGlobalSetupOnEachRun": "Globales Setup nicht wiederverwenden.",
"views.test.pw.extension.settingsView": "Playwright"
}
2 changes: 2 additions & 0 deletions package.nls.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"contributes.command.pw.extension.command.toggleModels": "Sélectionner les configurations Playwright",
"contributes.command.pw.extension.toggle.reuseBrowser": "Activer/désactiver la réutilisation du navigateur",
"contributes.command.pw.extension.toggle.showTrace": "Activer/désactiver Playwright Trace Viewer",
"contributes.command.pw.extension.toggle.runGlobalSetupOnEachRun": "Activer/désactiver la réutilisation de la setup globale",
"configuration.playwright.env": "Variables d'environnement à transmettre à Playwright Test.",
"configuration.playwright.reuseBrowser": "Afficher et réutiliser le navigateur entre les tests.",
"configuration.playwright.showTrace": "Afficher les traces avec Trace Viewer.",
"configuration.playwright.runGlobalSetupOnEachRun": "Ne pas réutiliser la setup globale.",
"views.test.pw.extension.settingsView": "Playwright"
}
2 changes: 2 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"contributes.command.pw.extension.command.toggleModels": "Toggle Playwright configs",
"contributes.command.pw.extension.toggle.reuseBrowser": "Toggle Playwright browser reuse",
"contributes.command.pw.extension.toggle.showTrace": "Toggle Playwright Trace Viewer",
"contributes.command.pw.extension.toggle.runGlobalSetupOnEachRun": "Toggle Playwright global setup reuse",
"configuration.playwright.env": "Environment variables to pass to Playwright Test.",
"configuration.playwright.reuseBrowser": "Show & reuse browser between tests.",
"configuration.playwright.showTrace": "Show Trace Viewer.",
"configuration.playwright.runGlobalSetupOnEachRun": "Run global setup on each run.",
"views.test.pw.extension.settingsView": "Playwright"
}
2 changes: 2 additions & 0 deletions package.nls.zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"contributes.command.pw.extension.command.toggleModels": "选择 Playwright 配置",
"contributes.command.pw.extension.toggle.reuseBrowser": "是否复用 Playwright 浏览器",
"contributes.command.pw.extension.toggle.showTrace": "是否显示 Playwright 浏览器的跟踪",
"contributes.command.pw.extension.toggle.runGlobalSetupOnEachRun": "切换 Playwright 全局设置的重用",
"configuration.playwright.env": "Playwright Test 运行环境变量",
"configuration.playwright.reuseBrowser": "在测试用例间显示并复用 Playwright 浏览器",
"configuration.playwright.showTrace": "显示 Playwright 浏览器的跟踪",
"configuration.playwright.runGlobalSetupOnEachRun": "不要重复使用全局设置",
"views.test.pw.extension.settingsView": "Playwright"
}
2 changes: 2 additions & 0 deletions src/settingsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class SettingsModel extends DisposableBase {
private _onChange: vscodeTypes.EventEmitter<void>;
showBrowser: Setting<boolean>;
showTrace: Setting<boolean>;
runGlobalSetupOnEachRun: Setting<boolean>;
embeddedTraceViewer: Setting<boolean>;
private _isUnderTest: boolean;

Expand All @@ -56,6 +57,7 @@ export class SettingsModel extends DisposableBase {

this.showBrowser = this._createSetting('reuseBrowser');
this.showTrace = this._createSetting('showTrace');
this.runGlobalSetupOnEachRun = this._createSetting('runGlobalSetupOnEachRun');
this.embeddedTraceViewer = this._createHiddenSetting('embeddedTraceViewer', false);

this.showBrowser.onChange(enabled => {
Expand Down
11 changes: 11 additions & 0 deletions src/settingsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class SettingsView extends DisposableBase implements vscodeTypes.WebviewV

this._disposables.push(this._settingsModel.onChange(() => {
this._updateSettings();
this._updateActions();
}));

this._disposables.push(webviewView.onDidChangeVisibility(() => {
Expand Down Expand Up @@ -144,13 +145,15 @@ export class SettingsView extends DisposableBase implements vscodeTypes.WebviewV
text: this._vscode.l10n.t('Run global setup'),
location: 'rareActions',
disabled: !this._models.selectedModel() || !this._models.selectedModel()!.canRunGlobalHooks('setup'),
hidden: this._settingsModel.runGlobalSetupOnEachRun.get(),
},
{
command: 'pw.extension.command.runGlobalTeardown',
svg: `<div class="action-indent"></div>`,
text: this._vscode.l10n.t('Run global teardown'),
location: 'rareActions',
disabled: !this._models.selectedModel() || !this._models.selectedModel()!.canRunGlobalHooks('teardown'),
hidden: this._settingsModel.runGlobalSetupOnEachRun.get(),
},
{
command: 'pw.extension.command.startDevServer',
Expand Down Expand Up @@ -283,6 +286,14 @@ function htmlForWebview(vscode: vscodeTypes.VSCode, extensionUri: vscodeTypes.Ur
<div class="section-header">${vscode.l10n.t('TOOLS')}</div>
<div id="actions" class="list"></div>
<div class="section-header">${vscode.l10n.t('SETUP')}</div>
<div class="list">
<div>
<label>
<input type="checkbox" setting="runGlobalSetupOnEachRun"></input>
${vscode.l10n.t('Run global setup on each run')}
</label>
</div>
</div>
<div id="rareActions" class="list"></div>
</body>
<script nonce="${nonce}">
Expand Down
14 changes: 11 additions & 3 deletions src/testModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,16 @@ export class TestModel extends DisposableBase {
}

canRunGlobalHooks(type: 'setup' | 'teardown') {
if (type === 'setup')
return !this._useLegacyCLIDriver && !this._ranGlobalSetup;
if (this._useLegacyCLIDriver)
return false;

if (type === 'setup') {
if (this._embedder.settingsModel.runGlobalSetupOnEachRun.get())
return true;

return !this._ranGlobalSetup;
}

return this._ranGlobalSetup;
}

Expand All @@ -466,7 +474,7 @@ export class TestModel extends DisposableBase {
if (!this.canRunGlobalHooks(type))
return 'passed';
if (type === 'setup') {
if (this._ranGlobalSetup)
if (!this.canRunGlobalHooks('setup'))
return 'passed';
const status = await this._playwrightTest.runGlobalHooks('setup', testListener);
if (status === 'passed')
Expand Down
1 change: 1 addition & 0 deletions tests/mock/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ export class VSCode {
'playwright.reuseBrowser': false,
'playwright.showTrace': false,
'playwright.embeddedTraceViewer': false,
'playwright.runGlobalSetupOnEachRun': false,
'workbench.colorTheme': 'Dark Modern',
};
this.workspace.getConfiguration = (scope: string) => {
Expand Down
57 changes: 57 additions & 0 deletions tests/run-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,3 +1246,60 @@ test('should force workers=1 when reusing the browser', async ({ activate, showB
const testRun = await testController.run();
expect(testRun.renderLog({ output: true })).toContain('Running 3 tests using 1 worker');
});

test.describe('runGlobalSetupOnEachRun', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/32121' } }, () => {
test.skip(({ overridePlaywrightVersion }) => !!overridePlaywrightVersion, 'old world doesnt have globalSetup');

function expectOrdering(log: string, items: string[]) {
items.forEach(item => expect(log).toContain(item));

const indices = items.map(item => log.indexOf(item));
expect(indices, `log contains entries ${JSON.stringify(items)} in order`).toEqual(indices.sort());
}

const files = {
'playwright.config.js': `module.exports = {
testDir: 'tests',
globalSetup: './globalSetup.js',
globalTeardown: './globalTeardown.js',
}`,
'globalSetup.js': `
export default () => console.log('RUNNING SETUP');
`,
'globalTeardown.js': `
export default () => console.log('RUNNING TEARDOWN');
`,
'tests/test.spec.ts': `
import { test } from '@playwright/test';
test('one', () => {
console.log('RUNNING TEST');
});
`,
};

test('if disabled, should run global setup only once and teardown never automatically', async ({ activate }) => {
const { testController } = await activate(files);

const firstRun = (await testController.run()).renderOutput();
expectOrdering(firstRun, ['RUNNING SETUP', 'RUNNING TEST']);
expect(firstRun).not.toContain('RUNNING TEARDOWN');

const secondRun = (await testController.run()).renderOutput();
expect(secondRun).toContain('RUNNING TEST');
expect(secondRun).not.toContain('RUNNING SETUP');
expect(secondRun).not.toContain('RUNNING TEARDOWN');
});

test('should always run global setup and teardown if runGlobalSetupOnEachRun is enabled', async ({ activate }) => {
const { testController } = await activate(files, { runGlobalSetupOnEachRun: true });

const firstRun = (await testController.run()).renderOutput();
expectOrdering(firstRun, ['RUNNING SETUP', 'RUNNING TEST']);
expect(firstRun).not.toContain('RUNNING TEARDOWN');

const secondRun = (await testController.run()).renderOutput();
expectOrdering(secondRun, ['RUNNING TEARDOWN', 'RUNNING SETUP', 'RUNNING TEST']);
});
});


6 changes: 4 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Latch = {

type TestFixtures = {
vscode: VSCode,
activate: (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][], env?: Record<string, any> }) => Promise<ActivateResult>;
activate: (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][], env?: Record<string, any>, runGlobalSetupOnEachRun?: boolean }) => Promise<ActivateResult>;
createLatch: () => Latch;
};

Expand Down Expand Up @@ -132,7 +132,7 @@ export const test = baseTest.extend<TestFixtures, WorkerOptions>({

activate: async ({ vscode, showBrowser, overridePlaywrightVersion, showTrace }, use, testInfo) => {
const instances: VSCode[] = [];
await use(async (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][], env?: Record<string, any> }) => {
await use(async (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][], env?: Record<string, any>, runGlobalSetupOnEachRun?: boolean }) => {
if (options?.workspaceFolders) {
for (const wf of options?.workspaceFolders)
await vscode.addWorkspaceFolder(wf[0], wf[1]);
Expand All @@ -143,6 +143,8 @@ export const test = baseTest.extend<TestFixtures, WorkerOptions>({
const configuration = vscode.workspace.getConfiguration('playwright');
if (options?.env)
configuration.update('env', options.env);
if (options?.runGlobalSetupOnEachRun)
configuration.update('runGlobalSetupOnEachRun', true);
if (showBrowser)
configuration.update('reuseBrowser', true);
if (showTrace) {
Expand Down

0 comments on commit 6835805

Please sign in to comment.