File size: 1,757 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/* eslint-disable require-jsdoc */
import { Browser, chromium } from 'playwright';
import envVariables from '../env-variables';
import { TestAccount } from '../lib/test-account';
import pwConfig from './playwright-config';
export default async (): Promise< void > => {
const { AUTHENTICATE_ACCOUNTS } = envVariables;
// If PWDEBUG mode is enabled (stepping through each step)
// don't execute the cookie refresh.
if ( process.env.PWDEBUG ) {
return;
}
// If the list of accounts for which to pre-authenticate and save cookies
// for is empty, then don't run.
if ( AUTHENTICATE_ACCOUNTS.length === 0 ) {
return;
}
const browser = await chromium.launch( {
...pwConfig.launchOptions,
headless: true,
} );
await Promise.all(
AUTHENTICATE_ACCOUNTS.map( async ( accountName ) => {
const testAccount = new TestAccount( accountName );
if ( await testAccount.hasFreshAuthCookies() ) {
return;
}
await loginAndSaveCookiesWithRetry( testAccount, browser );
} )
);
await browser.close();
};
async function loginAndSaveCookiesWithRetry( testAccount: TestAccount, browser: Browser ) {
const MAX_ATTEMPTS = 2;
let numberOfAttempts = 0;
let error: Error | undefined;
while ( numberOfAttempts < MAX_ATTEMPTS ) {
try {
return await loginAndSaveCookies( testAccount, browser );
} catch ( err ) {
numberOfAttempts++;
error = err as Error;
}
}
throw error;
}
async function loginAndSaveCookies( testAccount: TestAccount, browser: Browser ) {
const page = await browser.newPage( pwConfig.contextOptions );
page.setDefaultTimeout( envVariables.TIMEOUT );
try {
await testAccount.logInViaLoginPage( page );
await testAccount.saveAuthCookies( page.context() );
} finally {
await page.close();
}
}
|