PluginProbe
Hello Plus / 1.7.0
Hello Plus v1.7.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / tests / playwright / parallelTest.ts

parallelTest.ts in Hello Plus 1.7.0, at tests/playwright/parallelTest.ts

53 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { request, test as baseTest } from '@playwright/test';
2 import fs from 'fs';
3 import path from 'path';
4 import { fetchNonce, login } from './wp-authentication';
5 import ApiRequests from './assets/api-requests';
6
7 export const parallelTest = baseTest.extend< NonNullable<unknown>, { workerStorageState: string, workerBaseURL: string, apiRequests: ApiRequests }>( {
8 // Use the same storage state for all tests in this worker.
9 baseURL: ( { workerBaseURL }, use ) => use( workerBaseURL ),
10 workerBaseURL: [ async ( {}, use ) => {
11 await use( process.env.BASE_URL || ( ( 1 === Number( process.env.TEST_PARALLEL_INDEX ) )
12 ? process.env.TEST_SERVER
13 : process.env.DEV_SERVER ),
14 );
15 }, { scope: 'worker' } ],
16
17 // Use the same storage state for all tests in this worker.
18 storageState: ( { workerStorageState }, use ) => use( workerStorageState ),
19
20 // Authenticate once per worker with a worker-scoped fixture.
21 workerStorageState: [ async ( { workerBaseURL }, use, testInfo ) => {
22 // Use parallelIndex as a unique identifier for each worker.
23 const id = testInfo.workerIndex;
24 const fileName = path.resolve( testInfo.project.outputDir, `.storageState-${ id }.json` );
25
26 if ( fs.existsSync( fileName ) ) {
27 // Reuse existing authentication state if any.
28 await use( fileName );
29 return;
30 }
31
32 // Send authentication request.
33 const context = await login( request, process.env.USERNAME || 'admin', process.env.PASSWORD || 'password', workerBaseURL );
34 await context.storageState( { path: fileName } );
35 await context.dispose();
36
37 await use( fileName );
38 }, { scope: 'worker' } ],
39
40 // Use the same storage state for all tests in this worker.
41 apiRequests: [ async ( { workerStorageState, workerBaseURL }, use ) => {
42 const context = await request.newContext( { storageState: workerStorageState } );
43 try {
44 const nonce = await fetchNonce( context, workerBaseURL );
45 const apiRequests = new ApiRequests( workerBaseURL, nonce );
46 await use( apiRequests );
47 } catch ( e ) {
48 throw new Error( `Failed to fetch Nonce. Base URL: ${ workerBaseURL }, Storage State: ${ workerStorageState } `, { cause: e } );
49 }
50 }, { scope: 'worker' } ],
51
52 } );
53