Skip to content

Commit

Permalink
Optimizing PlayWright tests for perf
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosEngine committed Dec 20, 2023
1 parent ce90bb3 commit 9f6c946
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
17 changes: 13 additions & 4 deletions DotnetPlayground.Web/e2e/TwoUsers.spec.js
Original file line number Diff line number Diff line change
@@ -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//////
Expand Down Expand Up @@ -131,24 +133,31 @@ 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);

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);
Expand Down
2 changes: 1 addition & 1 deletion DotnetPlayground.Web/e2e/TwoUsersFixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 21 additions & 12 deletions DotnetPlayground.Web/e2e/global-setup.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;

0 comments on commit 9f6c946

Please sign in to comment.