woocommerce/plugins/woocommerce-blocks/tests/e2e-pw/global-setup.ts

98 lines
2.8 KiB
TypeScript
Raw Normal View History

/* eslint-disable no-console */
/**
* External dependencies
*/
import { FullConfig, chromium, request } from '@playwright/test';
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright';
import fs from 'fs';
/**
* Internal dependencies
*/
import { customer } from './test-data/data/data';
const loginAsCustomer = async ( config: FullConfig ) => {
const { stateDir, baseURL, userAgent } = config.projects[ 0 ].use;
// used throughout tests for authentication
process.env.ADMINSTATE = `${ stateDir }adminState.json`;
process.env.CUSTOMERSTATE = `${ stateDir }customerState.json`;
try {
fs.unlinkSync( process.env.CUSTOMERSTATE );
console.log( 'Customer state file deleted successfully.' );
} catch ( err ) {
if ( err.code === 'ENOENT' ) {
console.log( 'Customer state file does not exist.' );
} else {
console.log( 'Customer state file could not be deleted: ' + err );
}
}
let customerLoggedIn = false;
// Specify user agent when running against an external test site to avoid getting HTTP 406 NOT ACCEPTABLE errors.
const contextOptions = { baseURL, userAgent };
// Create browser, browserContext, and page for customer and admin users
const browser = await chromium.launch();
const customerContext = await browser.newContext( contextOptions );
const customerPage = await customerContext.newPage();
// Sign in as customer user and save state
const customerRetries = 5;
for ( let i = 0; i < customerRetries; i++ ) {
try {
await customerPage.goto( `/wp-admin` );
await customerPage.fill( 'input[name="log"]', customer.username );
await customerPage.fill( 'input[name="pwd"]', customer.password );
await customerPage.click( 'text=Log In' );
await customerPage.goto( `/my-account` );
await customerPage
.context()
.storageState( { path: process.env.CUSTOMERSTATE } );
console.log( 'Logged-in as customer successfully.' );
customerLoggedIn = true;
break;
} catch ( e ) {
console.log(
`Customer log-in failed. Retrying... ${ i }/${ customerRetries }`
);
console.log( e );
}
}
if ( ! customerLoggedIn ) {
console.error(
'Cannot proceed e2e test, as customer login failed. Please check if the test site has been setup correctly.'
);
process.exit( 1 );
}
await customerContext.close();
await browser.close();
};
async function globalSetup( config: FullConfig ) {
const { storageState, baseURL } = config.projects[ 0 ].use;
const storageStatePath =
typeof storageState === 'string' ? storageState : '';
const requestContext = await request.newContext( {
baseURL: baseURL ?? '',
} );
const requestUtils = new RequestUtils( requestContext, {
storageStatePath,
} );
await requestUtils.setupRest();
await requestContext.dispose();
await loginAsCustomer( config );
}
export default globalSetup;