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

116 lines
3.0 KiB
TypeScript
Raw Normal View History

/* eslint-disable no-console */
/**
* External dependencies
*/
import { chromium, request } from '@playwright/test';
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright';
import {
BASE_URL,
adminFile,
wpCLI,
customerFile,
BLOCK_THEME_SLUG,
DB_EXPORT_FILE,
} from '@woocommerce/e2e-utils';
/**
* Internal dependencies
*/
import { customer, admin } from './test-data/data/data';
const prepareAttributes = async () => {
Playwright E2E tests: Multiple signed in roles (https://github.com/woocommerce/woocommerce-blocks/pull/10561) * Convert checkout place order E2E tests to Playwright * Add "gotoMyAccount" method * Create login/logout utility functions * Use the existing "customer" test data * Complete the place order test cases * Fix "My Account" page title * Fix rebase * Reset My account page title * Check for heading instead of the page title * Check for heading in login/logout functions * Fix all failing tests * Add guest/customer/admin roles * Update the auth setup * Register the auth setup within Playwright * Update testing cases * Add generated auth files to .gitignore * Tidy up comments Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Remove unnecessary comment Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update comments Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Remove unnecessary comment Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update comment for Guest case Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Remove confusing comment * Remove another unnecessary comment * Remove unnecessary Playwright project dependency * Tidy up the file structure and constants * Fix mixed up test descriptions * Remove commented code Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Remove unnecessary function from frontend-utils * Refactor testing cases * Rename testing file * Delete unused testing file * Ensure we're logged out before trying to log in as a user * Log out before each authentication setup step * Ensure tests requiring admin are logged in * Log in as admin during block theme setup * Fix Playwright strict mode violation * Run Multiple sign-in roles to the global-setup phase In this step of the Playwright's setup, we can add the multiple sign-in roles and keeping the admin logged by default. This fixes the issue of failing tests `logged out` error. * Remove unnecessary login as admin * Remove "auth.setup" dependency since the logic lives in the global setup instead * Remove unnecessary login as admin from test files The admin profile is set by default --------- Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2023-12-08 16:44:59 +00:00
const browser = await chromium.launch();
const context = await browser.newContext( {
baseURL: BASE_URL,
storageState: adminFile,
} );
const page = await context.newPage();
// Intercept the dialog event. This is needed because when the regenerate
// button is clicked, a dialog is shown.
page.on( 'dialog', ( dialog ) => {
void dialog.accept();
} );
await page.goto( '/wp-admin/admin.php?page=wc-status&tab=tools' );
// Attributes regeneration should be doable via a CLI command, e.g.:
// "wp wc tool run regenerate_product_attributes_lookup_table --user=1"
// It doesn't seem to be working correctly ATM so we need to do it via
// browser actions.
// See: https://github.com/woocommerce/woocommerce/issues/32831
await page
.getByRole( 'row', {
name: /Regenerate the product attributes lookup table/,
} )
.getByRole( 'button' )
.click();
await context.close();
await browser.close();
// Note that the two commands below are intentionally duplicated as we need
// to run the cron task twice as we need to process more than 1 batch of
// items.
const cronTask =
'action-scheduler run --hooks="woocommerce_run_product_attribute_lookup_regeneration_callback"';
await wpCLI( cronTask );
await wpCLI( cronTask );
};
async function globalSetup() {
console.log( 'Running global setup:' );
console.time( '└ Total time' );
let databaseImported = false;
try {
await wpCLI( `db import ${ DB_EXPORT_FILE }` );
console.log( '├ Database snapshot imported, running basic setup…' );
databaseImported = true;
} catch ( error ) {
if (
error instanceof Error &&
! error.message.includes( 'Import file missing' )
) {
// Throw if the error is not related to the import file missing.
throw error;
}
console.log( '├ Database snapshot not found, running full setup…' );
}
const requestContext = await request.newContext( {
baseURL: BASE_URL,
} );
console.log( '├ Pre-authenticating users…' );
await new RequestUtils( requestContext, {
user: customer,
storageStatePath: customerFile,
} ).setupRest();
const requestUtils = new RequestUtils( requestContext, {
user: admin,
storageStatePath: adminFile,
} );
await requestUtils.setupRest();
if ( ! databaseImported ) {
console.log( '├ Activating default theme…' );
await requestUtils.activateTheme( BLOCK_THEME_SLUG );
console.log( '├ Preparing product attributes…' );
await prepareAttributes();
}
console.log( '├ Exporting database snapshot…' );
await wpCLI( `db export ${ DB_EXPORT_FILE }` );
await requestContext.dispose();
console.timeEnd( '└ Total time' );
}
export default globalSetup;