Add detailed logs for e2e setup/teardown (#34004)
This commit is contained in:
parent
7741464a32
commit
f1ba1febea
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: dev
|
||||||
|
|
||||||
|
Better e2e setup/teardown logging
|
|
@ -1,44 +1,80 @@
|
||||||
const { chromium } = require( '@playwright/test' );
|
const { chromium, expect } = require( '@playwright/test' );
|
||||||
const fs = require( 'fs' );
|
const fs = require( 'fs' );
|
||||||
|
|
||||||
module.exports = async ( config ) => {
|
module.exports = async ( config ) => {
|
||||||
const { stateDir } = config.projects[ 0 ].use;
|
const { stateDir } = config.projects[ 0 ].use;
|
||||||
const { baseURL } = 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
|
// used throughout tests for authentication
|
||||||
process.env.ADMINSTATE = `${ stateDir }adminState.json`;
|
process.env.ADMINSTATE = `${ stateDir }adminState.json`;
|
||||||
process.env.CUSTOMERSTATE = `${ stateDir }customerState.json`;
|
process.env.CUSTOMERSTATE = `${ stateDir }customerState.json`;
|
||||||
|
|
||||||
// Clear out the previous save states
|
// Clear out the previous save states
|
||||||
fs.unlink( process.env.ADMINSTATE, function ( err ) {
|
try {
|
||||||
if ( err ) {
|
fs.unlinkSync( process.env.ADMINSTATE );
|
||||||
// File doesn't exist yet, so will just create it.
|
console.log( 'Admin state file deleted successfully.' );
|
||||||
|
} catch ( err ) {
|
||||||
|
if ( err.code === 'ENOENT' ) {
|
||||||
|
console.log( 'Admin state file does not exist.' );
|
||||||
} else {
|
} 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 ) {
|
try {
|
||||||
if ( err ) {
|
fs.unlinkSync( process.env.CUSTOMERSTATE );
|
||||||
// File doesn't exist yet, so will just create it.
|
console.log( 'Customer state file deleted successfully.' );
|
||||||
|
} catch ( err ) {
|
||||||
|
if ( err.code === 'ENOENT' ) {
|
||||||
|
console.log( 'Customer state file does not exist.' );
|
||||||
} else {
|
} 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 browser = await chromium.launch();
|
||||||
const adminPage = await browser.newPage();
|
const adminPage = await browser.newPage();
|
||||||
|
const customerPage = await browser.newPage();
|
||||||
|
|
||||||
// Sign in as admin user and save state
|
// Sign in as admin user and save state
|
||||||
const adminRetries = 5;
|
const adminRetries = 5;
|
||||||
for ( let i = 0; i < adminRetries; i++ ) {
|
for ( let i = 0; i < adminRetries; i++ ) {
|
||||||
try {
|
try {
|
||||||
|
console.log( 'Trying to log-in as admin...' );
|
||||||
await adminPage.goto( `${ baseURL }/wp-admin` );
|
await adminPage.goto( `${ baseURL }/wp-admin` );
|
||||||
await adminPage.fill( 'input[name="log"]', 'admin' );
|
await adminPage.fill( 'input[name="log"]', 'admin' );
|
||||||
await adminPage.fill( 'input[name="pwd"]', 'password' );
|
await adminPage.fill( 'input[name="pwd"]', 'password' );
|
||||||
await adminPage.click( 'text=Log In' );
|
await adminPage.click( 'text=Log In' );
|
||||||
|
await adminPage.goto( `${ baseURL }/wp-admin` );
|
||||||
|
await expect( adminPage.locator( 'div.wrap > h1' ) ).toHaveText(
|
||||||
|
'Dashboard'
|
||||||
|
);
|
||||||
await adminPage
|
await adminPage
|
||||||
.context()
|
.context()
|
||||||
.storageState( { path: process.env.ADMINSTATE } );
|
.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
|
// While we're here, let's add a consumer token for API access
|
||||||
|
@ -46,6 +82,7 @@ module.exports = async ( config ) => {
|
||||||
const nRetries = 5;
|
const nRetries = 5;
|
||||||
for ( let i = 0; i < nRetries; i++ ) {
|
for ( let i = 0; i < nRetries; i++ ) {
|
||||||
try {
|
try {
|
||||||
|
console.log( 'Trying to add consumer token...' );
|
||||||
await adminPage.goto(
|
await adminPage.goto(
|
||||||
`${ baseURL }/wp-admin/admin.php?page=wc-settings&tab=advanced§ion=keys&create-key=1`
|
`${ baseURL }/wp-admin/admin.php?page=wc-settings&tab=advanced§ion=keys&create-key=1`
|
||||||
);
|
);
|
||||||
|
@ -58,21 +95,64 @@ module.exports = async ( config ) => {
|
||||||
process.env.CONSUMER_SECRET = await adminPage.inputValue(
|
process.env.CONSUMER_SECRET = await adminPage.inputValue(
|
||||||
'#key_consumer_secret'
|
'#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
|
// Sign in as customer user and save state
|
||||||
const customerRetries = 5;
|
const customerRetries = 5;
|
||||||
for ( let i = 0; i < customerRetries; i++ ) {
|
for ( let i = 0; i < customerRetries; i++ ) {
|
||||||
try {
|
try {
|
||||||
const customerPage = await browser.newPage();
|
console.log( 'Trying to log-in as customer...' );
|
||||||
await customerPage.goto( `${ baseURL }/wp-admin` );
|
await customerPage.goto( `${ baseURL }/wp-admin` );
|
||||||
await customerPage.fill( 'input[name="log"]', 'customer' );
|
await customerPage.fill( 'input[name="log"]', 'customer' );
|
||||||
await customerPage.fill( 'input[name="pwd"]', 'password' );
|
await customerPage.fill( 'input[name="pwd"]', 'password' );
|
||||||
await customerPage.click( 'text=Log In' );
|
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
|
await customerPage
|
||||||
.context()
|
.context()
|
||||||
.storageState( { path: process.env.CUSTOMERSTATE } );
|
.storageState( { path: process.env.CUSTOMERSTATE } );
|
||||||
await browser.close();
|
console.log( 'Logged-in as customer successfully.' );
|
||||||
} catch ( e ) {}
|
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();
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,10 +6,13 @@ module.exports = async ( config ) => {
|
||||||
const browser = await chromium.launch();
|
const browser = await chromium.launch();
|
||||||
const adminPage = await browser.newPage();
|
const adminPage = await browser.newPage();
|
||||||
|
|
||||||
|
let consumerTokenCleared = false;
|
||||||
|
|
||||||
// Clean up the consumer keys
|
// Clean up the consumer keys
|
||||||
const keysRetries = 5;
|
const keysRetries = 5;
|
||||||
for ( let i = 0; i < keysRetries; i++ ) {
|
for ( let i = 0; i < keysRetries; i++ ) {
|
||||||
try {
|
try {
|
||||||
|
console.log( 'Trying to clear consumer token... Try:' + i );
|
||||||
await adminPage.goto( `${ baseURL }/wp-admin` );
|
await adminPage.goto( `${ baseURL }/wp-admin` );
|
||||||
await adminPage.fill( 'input[name="log"]', 'admin' );
|
await adminPage.fill( 'input[name="log"]', 'admin' );
|
||||||
await adminPage.fill( 'input[name="pwd"]', 'password' );
|
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§ion=keys`
|
`${ baseURL }/wp-admin/admin.php?page=wc-settings&tab=advanced§ion=keys`
|
||||||
);
|
);
|
||||||
await adminPage.dispatchEvent( 'a.submitdelete', 'click' );
|
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 );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue