From 9f6c946ecdb6d81f3ae60362b15947abc33d05fb Mon Sep 17 00:00:00 2001 From: ChaosEngine Date: Wed, 20 Dec 2023 21:53:50 +0100 Subject: [PATCH] Optimizing PlayWright tests for perf --- DotnetPlayground.Web/e2e/TwoUsers.spec.js | 17 +++++++--- DotnetPlayground.Web/e2e/TwoUsersFixtures.js | 2 +- DotnetPlayground.Web/e2e/global-setup.js | 33 +++++++++++++------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/DotnetPlayground.Web/e2e/TwoUsers.spec.js b/DotnetPlayground.Web/e2e/TwoUsers.spec.js index 4db7fef2..cdafb6b4 100644 --- a/DotnetPlayground.Web/e2e/TwoUsers.spec.js +++ b/DotnetPlayground.Web/e2e/TwoUsers.spec.js @@ -1,6 +1,8 @@ /* eslint-disable no-unused-vars */ // Import test with our new fixtures. -import { test, expect } from './TwoUsersFixtures'; +// import { test, expect } from './TwoUsersFixtures'; +import { test, expect } from '@playwright/test'; +import { FixtureUsers, PlaywrightUser } from './TwoUsersFixtures'; //////handy helper functions - START////// @@ -131,10 +133,16 @@ async function chatPlayerToPlayer(fromPlayer, toPlayer, message) { //////handy helper functions - END////// +//init +let Playwright1, Playwright2; +test.beforeAll(async ({ browser }) => { + Playwright1 = await PlaywrightUser.create(browser, FixtureUsers[0].userName); + Playwright2 = await PlaywrightUser.create(browser, FixtureUsers[1].userName); +}); //////Tests////// -test('Playwright1 and Playwright2 - no games created', async ({ Playwright1, Playwright2 }) => { +test('Playwright1 and Playwright2 - no games created', async () => { // ... interact with Playwright1 and/or Playwright2 ... await testLoggedInAndNoGameAlert(Playwright1.page, Playwright1.userName); @@ -142,13 +150,14 @@ test('Playwright1 and Playwright2 - no games created', async ({ Playwright1, Pla await testLoggedInAndNoGameAlert(Playwright2.page, Playwright2.userName); }); -test('Playwright1 and Playwright2 - GamesList', async ({ Playwright1, Playwright2 }) => { +test('Playwright1 and Playwright2 - GamesList', async () => { // ... interact with Playwright1 and/or Playwright2 ... await testLoggedInGamesList(Playwright1.page); }); -test('P1 create game, P2 joins, P2 wins', async ({ Playwright1: p1, Playwright2: p2 }) => { +test('P1 create game, P2 joins, P2 wins', async () => { + const p1 = Playwright1, p2 = Playwright2; // ... interact with Playwright1 and/or Playwright2 ... //create new game as p1 await createGameFromHome(p1); diff --git a/DotnetPlayground.Web/e2e/TwoUsersFixtures.js b/DotnetPlayground.Web/e2e/TwoUsersFixtures.js index c65f4320..6e422c3b 100644 --- a/DotnetPlayground.Web/e2e/TwoUsersFixtures.js +++ b/DotnetPlayground.Web/e2e/TwoUsersFixtures.js @@ -9,7 +9,7 @@ export const FixtureUsers = [ // Page Object Model for the "PlaywrightUserX" page. // Here you can add locators and helper methods specific to the admin page. -class PlaywrightUser { +export class PlaywrightUser { // Page signed in as "PlaywrightX". constructor(page, userName) { diff --git a/DotnetPlayground.Web/e2e/global-setup.js b/DotnetPlayground.Web/e2e/global-setup.js index 533792bc..e9961f6f 100644 --- a/DotnetPlayground.Web/e2e/global-setup.js +++ b/DotnetPlayground.Web/e2e/global-setup.js @@ -1,5 +1,6 @@ // global-setup.js -import { chromium, firefox, webkit/*, FullConfig */ } from '@playwright/test'; +import fs from 'fs'; +import { chromium, firefox, webkit/* , FullConfig */ } from '@playwright/test'; import { FixtureUsers } from './TwoUsersFixtures'; async function signInUser(browser, loginURL, storageStatePath, user) { @@ -19,21 +20,29 @@ async function signInUser(browser, loginURL, storageStatePath, user) { async function globalSetup(config) { let browser = undefined; - if (!browser && chromium) - browser = await chromium.launch(); - if (!browser && firefox) - browser = await firefox.launch(); - if (!browser && webkit) - browser = await webkit.launch(); - - const use = config.projects.find(p => p.name === browser._name).use; + const use = config.projects.at(0).use; const loginURL = use.baseURL + 'Identity/Account/Login'; - for (const fu of FixtureUsers) { - await signInUser(browser, loginURL, use.storageState, fu); + for (const user of FixtureUsers) { + const storageFile = `${use.storageState}${user.userName}-storageState.json`; + if (!fs.existsSync(storageFile)) { + + // const stats = fs.statSync(storageFile); + // const date = new Date(stats.mtime); + + if (!browser && chromium) + browser = await chromium.launch(); + if (!browser && firefox) + browser = await firefox.launch(); + if (!browser && webkit) + browser = await webkit.launch(); + + await signInUser(browser, loginURL, use.storageState, user); + } } - await browser.close(); + if (browser) + await browser.close(); } export default globalSetup; \ No newline at end of file