Add detailed logs for e2e setup/teardown (#34004)

This commit is contained in:
Lucas Bustamante 2022-07-21 00:34:24 +01:00 committed by GitHub
parent 7741464a32
commit f1ba1febea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 17 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Better e2e setup/teardown logging

View File

@ -1,44 +1,80 @@
const { chromium } = require( '@playwright/test' );
const { chromium, expect } = require( '@playwright/test' );
const fs = require( 'fs' );
module.exports = async ( config ) => {
const { stateDir } = config.projects[ 0 ].use;
const { baseURL } = config.projects[ 0 ].use;
console.log( `State Dir: ${ stateDir }` );
console.log( `Base URL: ${ baseURL }` );
// used throughout tests for authentication
process.env.ADMINSTATE = `${ stateDir }adminState.json`;
process.env.CUSTOMERSTATE = `${ stateDir }customerState.json`;
// Clear out the previous save states
fs.unlink( process.env.ADMINSTATE, function ( err ) {
if ( err ) {
// File doesn't exist yet, so will just create it.
try {
fs.unlinkSync( process.env.ADMINSTATE );
console.log( 'Admin state file deleted successfully.' );
} catch ( err ) {
if ( err.code === 'ENOENT' ) {
console.log( 'Admin state file does not exist.' );
} else {
// File exists. Delete it so it can be re-created.
console.log( 'Admin state file could not be deleted: ' + err );
}
} );
fs.unlink( process.env.CUSTOMERSTATE, function ( err ) {
if ( err ) {
// File doesn't exist yet, so will just create it.
}
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 {
// File exists. Delete it so it can be re-created.
console.log( 'Customer state file could not be deleted: ' + err );
}
} );
}
// Pre-requisites
let adminLoggedIn = false;
let customerLoggedIn = false;
let customerKeyConfigured = false;
const browser = await chromium.launch();
const adminPage = await browser.newPage();
const customerPage = await browser.newPage();
// Sign in as admin user and save state
const adminRetries = 5;
for ( let i = 0; i < adminRetries; i++ ) {
try {
console.log( 'Trying to log-in as admin...' );
await adminPage.goto( `${ baseURL }/wp-admin` );
await adminPage.fill( 'input[name="log"]', 'admin' );
await adminPage.fill( 'input[name="pwd"]', 'password' );
await adminPage.click( 'text=Log In' );
await adminPage.goto( `${ baseURL }/wp-admin` );
await expect( adminPage.locator( 'div.wrap > h1' ) ).toHaveText(
'Dashboard'
);
await adminPage
.context()
.storageState( { path: process.env.ADMINSTATE } );
} catch ( e ) {}
console.log( 'Logged-in as admin successfully.' );
adminLoggedIn = true;
break;
} catch ( e ) {
console.log(
`Admin log-in failed, Retrying... ${ i }/${ adminRetries }`
);
console.log( e );
}
}
if ( ! adminLoggedIn ) {
console.error(
'Cannot proceed e2e test, as admin login failed. Please check if the test site has been setup correctly.'
);
process.exit( 1 );
}
// While we're here, let's add a consumer token for API access
@ -46,6 +82,7 @@ module.exports = async ( config ) => {
const nRetries = 5;
for ( let i = 0; i < nRetries; i++ ) {
try {
console.log( 'Trying to add consumer token...' );
await adminPage.goto(
`${ baseURL }/wp-admin/admin.php?page=wc-settings&tab=advanced&section=keys&create-key=1`
);
@ -58,21 +95,64 @@ module.exports = async ( config ) => {
process.env.CONSUMER_SECRET = await adminPage.inputValue(
'#key_consumer_secret'
);
} catch ( e ) {}
console.log( 'Added consumer token successfully.' );
customerKeyConfigured = true;
break;
} catch ( e ) {
console.log(
`Failed to add consumer token. Retrying... ${ i }/${ nRetries }`
);
console.log( e );
}
}
if ( ! customerKeyConfigured ) {
console.error(
'Cannot proceed e2e test, as we could not set the customer key. Please check if the test site has been setup correctly.'
);
process.exit( 1 );
}
// Sign in as customer user and save state
const customerRetries = 5;
for ( let i = 0; i < customerRetries; i++ ) {
try {
const customerPage = await browser.newPage();
console.log( 'Trying to log-in as customer...' );
await customerPage.goto( `${ baseURL }/wp-admin` );
await customerPage.fill( 'input[name="log"]', 'customer' );
await customerPage.fill( 'input[name="pwd"]', 'password' );
await customerPage.click( 'text=Log In' );
await customerPage.goto( `${ baseURL }/my-account/` );
await expect(
customerPage.locator( 'h1.entry-title' )
).toContainText( 'My account' );
await expect(
customerPage.locator(
'div.woocommerce-MyAccount-content > p >> nth=0'
)
).toContainText( 'Jane Smith' );
await customerPage
.context()
.storageState( { path: process.env.CUSTOMERSTATE } );
await browser.close();
} catch ( e ) {}
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 browser.close();
};

View File

@ -6,10 +6,13 @@ module.exports = async ( config ) => {
const browser = await chromium.launch();
const adminPage = await browser.newPage();
let consumerTokenCleared = false;
// Clean up the consumer keys
const keysRetries = 5;
for ( let i = 0; i < keysRetries; i++ ) {
try {
console.log( 'Trying to clear consumer token... Try:' + i );
await adminPage.goto( `${ baseURL }/wp-admin` );
await adminPage.fill( 'input[name="log"]', 'admin' );
await adminPage.fill( 'input[name="pwd"]', 'password' );
@ -18,6 +21,16 @@ module.exports = async ( config ) => {
`${ baseURL }/wp-admin/admin.php?page=wc-settings&tab=advanced&section=keys`
);
await adminPage.dispatchEvent( 'a.submitdelete', 'click' );
} catch ( e ) {}
console.log( 'Cleared up consumer token successfully.' );
consumerTokenCleared = true;
break;
} catch ( e ) {
console.log( 'Failed to clear consumer token. Retrying...' );
}
}
if ( ! consumerTokenCleared ) {
console.error( 'Could not clear consumer token.' );
process.exit( 1 );
}
};