chore(test): remove try/finally pattern from fixtures (#3409)

This commit is contained in:
Joel Einbinder 2020-08-12 17:51:07 -07:00 committed by GitHub
parent 23f5ed89b0
commit ec24516e66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 95 deletions

View File

@ -107,21 +107,15 @@ registerWorkerFixture('playwright', async({parallelIndex}, test) => {
connection.onmessage = message => transport.send(JSON.stringify(message)); connection.onmessage = message => transport.send(JSON.stringify(message));
transport.onmessage = message => connection.dispatch(JSON.parse(message)); transport.onmessage = message => connection.dispatch(JSON.parse(message));
const playwrightObject = await connection.waitForObjectWithKnownName('Playwright'); const playwrightObject = await connection.waitForObjectWithKnownName('Playwright');
try { await test(playwrightObject);
await test(playwrightObject); spawnedProcess.removeListener('exit', onExit);
} finally { spawnedProcess.stdin.destroy();
spawnedProcess.removeListener('exit', onExit); spawnedProcess.stdout.destroy();
spawnedProcess.stdin.destroy(); spawnedProcess.stderr.destroy();
spawnedProcess.stdout.destroy(); await teardownCoverage();
spawnedProcess.stderr.destroy();
await teardownCoverage();
}
} else { } else {
try { await test(require('../index'))
await test(require('../index')) await teardownCoverage();
} finally {
await teardownCoverage();
}
} }
async function teardownCoverage() { async function teardownCoverage() {
@ -144,24 +138,18 @@ registerWorkerFixture('browserType', async ({playwright}, test) => {
registerWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, test) => { registerWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, test) => {
const browser = await browserType.launch(defaultBrowserOptions); const browser = await browserType.launch(defaultBrowserOptions);
try { await test(browser);
await test(browser); if (browser.contexts().length !== 0) {
if (browser.contexts().length !== 0) { console.warn(`\nWARNING: test did not close all created contexts! ${new Error().stack}\n`);
console.warn(`\nWARNING: test did not close all created contexts! ${new Error().stack}\n`); await Promise.all(browser.contexts().map(context => context.close())).catch(e => void 0);
await Promise.all(browser.contexts().map(context => context.close()));
}
} finally {
await browser.close();
} }
await browser.close();
}); });
registerFixture('context', async ({browser}, test) => { registerFixture('context', async ({browser}, test) => {
const context = await browser.newContext(); const context = await browser.newContext();
try { await test(context);
await test(context); await context.close();
} finally {
await context.close();
}
}); });
registerFixture('page', async ({context}, test) => { registerFixture('page', async ({context}, test) => {

View File

@ -30,20 +30,14 @@ registerFixture('sppBrowser', async ({browserType, defaultBrowserOptions}, test)
...defaultBrowserOptions, ...defaultBrowserOptions,
args: (defaultBrowserOptions.args || []).concat(['--site-per-process']) args: (defaultBrowserOptions.args || []).concat(['--site-per-process'])
}); });
try { await test(browser);
await test(browser); await browser.close();
} finally {
await browser.close();
}
}); });
registerFixture('sppContext', async ({sppBrowser}, test) => { registerFixture('sppContext', async ({sppBrowser}, test) => {
const context = await sppBrowser.newContext(); const context = await sppBrowser.newContext();
try { await test(context);
await test(context); await context.close();
} finally {
await context.close();
}
}); });
registerFixture('sppPage', async ({sppContext}, test) => { registerFixture('sppPage', async ({sppContext}, test) => {

View File

@ -32,11 +32,8 @@ declare global {
} }
registerFixture('userDataDir', async ({}, test) => { registerFixture('userDataDir', async ({}, test) => {
const userDataDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright_dev_profile-')); const userDataDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright_dev_profile-'));
try { await test(userDataDir);
await test(userDataDir); removeFolderAsync(userDataDir).catch(e => {});
} finally {
removeFolderAsync(userDataDir).catch(e => {});
}
}); });
registerFixture('launchPersistent', async ({userDataDir, defaultBrowserOptions, browserType}, test) => { registerFixture('launchPersistent', async ({userDataDir, defaultBrowserOptions, browserType}, test) => {
@ -48,12 +45,9 @@ registerFixture('launchPersistent', async ({userDataDir, defaultBrowserOptions,
const page = context.pages()[0]; const page = context.pages()[0];
return {context, page}; return {context, page};
} }
try { await test(launchPersistent);
await test(launchPersistent); if (context)
} finally { await context.close();
if (context)
await context.close();
}
}); });
it('context.cookies() should work', async ({server, launchPersistent}) => { it('context.cookies() should work', async ({server, launchPersistent}) => {

View File

@ -30,11 +30,8 @@ declare global {
} }
registerFixture('persistentDirectory', async ({}, test) => { registerFixture('persistentDirectory', async ({}, test) => {
const persistentDirectory = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-')); const persistentDirectory = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
try { await test(persistentDirectory);
await test(persistentDirectory); await removeFolderAsync(persistentDirectory);
} finally {
await removeFolderAsync(persistentDirectory);
}
}); });
beforeEach(async ({server}) => { beforeEach(async ({server}) => {

View File

@ -30,11 +30,8 @@ declare global {
} }
registerFixture('downloadsPath', async ({}, test) => { registerFixture('downloadsPath', async ({}, test) => {
const downloadsPath = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-')); const downloadsPath = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
try { await test(downloadsPath);
await test(downloadsPath); await removeFolderAsync(downloadsPath);
} finally {
await removeFolderAsync(downloadsPath);
}
}); });
registerFixture('downloadsBrowser', async ({server, browserType, defaultBrowserOptions, downloadsPath}, test) => { registerFixture('downloadsBrowser', async ({server, browserType, defaultBrowserOptions, downloadsPath}, test) => {
@ -47,11 +44,8 @@ registerFixture('downloadsBrowser', async ({server, browserType, defaultBrowserO
...defaultBrowserOptions, ...defaultBrowserOptions,
downloadsPath: downloadsPath, downloadsPath: downloadsPath,
}); });
try { await test(browser);
await test(browser); await browser.close();
} finally {
await browser.close();
}
}); });
registerFixture('persistentDownloadsContext', async ({server, browserType, defaultBrowserOptions, downloadsPath}, test) => { registerFixture('persistentDownloadsContext', async ({server, browserType, defaultBrowserOptions, downloadsPath}, test) => {
@ -71,12 +65,9 @@ registerFixture('persistentDownloadsContext', async ({server, browserType, defau
); );
const page = context.pages()[0]; const page = context.pages()[0];
page.setContent(`<a href="${server.PREFIX}/download">download</a>`); page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
try { await test(context);
await test(context); await context.close();
} finally { await removeFolderAsync(userDataDir);
await context.close();
await removeFolderAsync(userDataDir);
}
}); });
it('should keep downloadsPath folder', async({downloadsBrowser, downloadsPath, server}) => { it('should keep downloadsPath folder', async({downloadsBrowser, downloadsPath, server}) => {

View File

@ -20,18 +20,12 @@ registerFixture('application', async ({playwright}, test) => {
const application = await playwright.electron.launch(electronPath, { const application = await playwright.electron.launch(electronPath, {
args: [path.join(__dirname, 'testApp.js')], args: [path.join(__dirname, 'testApp.js')],
}); });
try { await test(application);
await test(application); await application.close();
} finally {
await application.close();
}
}); });
registerFixture('window', async ({application}, test) => { registerFixture('window', async ({application}, test) => {
const page = await application.newBrowserWindow({ width: 800, height: 600 }); const page = await application.newBrowserWindow({ width: 800, height: 600 });
try { await test(page);
await test(page); await page.close();
} finally {
await page.close();
}
}); });

View File

@ -33,11 +33,8 @@ declare global {
registerFixture('persistentDirectory', async ({}, test) => { registerFixture('persistentDirectory', async ({}, test) => {
const persistentDirectory = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-')); const persistentDirectory = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
try { await test(persistentDirectory);
await test(persistentDirectory); await removeFolderAsync(persistentDirectory);
} finally {
await removeFolderAsync(persistentDirectory);
}
}); });
registerFixture('videoPlayer', async ({playwright, context}, test) => { registerFixture('videoPlayer', async ({playwright, context}, test) => {
@ -48,17 +45,13 @@ registerFixture('videoPlayer', async ({playwright, context}, test) => {
context = await firefox.newContext(); context = await firefox.newContext();
} }
let page; const page = await context.newPage();
try { const player = new VideoPlayer(page);
page = await context.newPage(); await test(player);
const player = new VideoPlayer(page); if (firefox)
await test(player); await firefox.close();
} finally { else
if (firefox) await page.close();
await firefox.close();
else
await page.close();
}
}); });
function almostRed(r, g, b, alpha) { function almostRed(r, g, b, alpha) {