woocommerce-paypal-payments/tests/qa/utils/test.ts
2025-02-06 12:02:17 +01:00

218 lines
5.7 KiB
TypeScript

/**
* External dependencies
*/
import fs from 'fs';
import { APIRequestContext, Page } from '@playwright/test';
import {
test as base,
expect,
WooCommerceApi,
} from '@inpsyde/playwright-utils/build';
/**
* Internal dependencies
*/
import { PayPalAPI } from './paypal-api';
import { PayPalUI } from './frontend/paypal-ui';
import { Utils } from './utils';
// PCP tabs
import {
PcpOnboarding,
PcpOverview,
PcpPaymentMethods,
PcpSettings,
PcpStyling,
PcpPayLaterMessaging,
WooCommerceOrderEdit,
WooCommerceSubscriptionEdit,
} from './admin';
// WooCommerce front end
import {
Shop,
Product,
Cart,
Checkout,
ClassicCart,
ClassicCheckout,
PayForOrder,
OrderReceived,
CustomerAccount,
CustomerPaymentMethods,
CustomerSubscriptions,
ClassicPayForOrder,
} from './frontend';
type BaseExtend = {
ppapi: PayPalAPI;
ppui: PayPalUI;
visitorPage: Page;
visitorRequest: APIRequestContext;
visitorWooCommerceApi: WooCommerceApi;
// PCP tabs
pcpOnboarding: PcpOnboarding;
pcpOverview: PcpOverview;
pcpPaymentMethods: PcpPaymentMethods;
pcpSettings: PcpSettings;
pcpStyling: PcpStyling;
pcpPayLaterMessaging: PcpPayLaterMessaging;
// WooCommerce dashboard
wooCommerceOrderEdit: WooCommerceOrderEdit;
wooCommerceSubscriptionEdit: WooCommerceSubscriptionEdit;
// WooCommerce Guest front end
shop: Shop;
product: Product;
cart: Cart;
checkout: Checkout;
classicCart: ClassicCart;
classicCheckout: ClassicCheckout;
classicPayForOrder: ClassicPayForOrder;
payForOrder: PayForOrder;
orderReceived: OrderReceived;
customerAccount: CustomerAccount;
customerPaymentMethods: CustomerPaymentMethods;
customerSubscriptions: CustomerSubscriptions;
// Utils & preconditions
utils: Utils;
};
const test = base.extend< BaseExtend >( {
ppapi: async ( { request }, use ) => {
await use( new PayPalAPI( { request } ) );
},
visitorPage: async ( { browser }, use, testInfo ) => {
// check if visitor is specified in test otherwise use guest
const storageStateName =
testInfo.annotations?.find( ( el ) => el.type === 'visitor' )
?.description || 'guest';
const storageStatePath = `${ process.env.STORAGE_STATE_PATH }/${ storageStateName }.json`;
// apply current visitor's storage state to the context
const context = await browser.newContext( {
storageState: fs.existsSync( storageStatePath )
? storageStatePath
: undefined,
} );
const page = await context.newPage();
await use( page );
await page.close();
await context.close();
},
visitorRequest: async ( { visitorPage }, use ) => {
const request = visitorPage.request;
await use( request );
},
visitorWooCommerceApi: async ( { visitorRequest }, use ) => {
await use( new WooCommerceApi( { request: visitorRequest } ) );
},
ppui: async ( { visitorPage, ppapi }, use ) => {
await use( new PayPalUI( { page: visitorPage, ppapi } ) );
},
// PCP settings
pcpOnboarding: async ( { page }, use ) => {
await use( new PcpOnboarding( { page } ) );
},
pcpOverview: async ( { page }, use ) => {
await use( new PcpOverview( { page } ) );
},
pcpPaymentMethods: async ( { page }, use ) => {
await use( new PcpPaymentMethods( { page } ) );
},
pcpSettings: async ( { page }, use ) => {
await use( new PcpSettings( { page } ) );
},
pcpStyling: async ( { page }, use ) => {
await use( new PcpStyling( { page } ) );
},
pcpPayLaterMessaging: async ( { page }, use ) => {
await use( new PcpPayLaterMessaging( { page } ) );
},
// WooCommerce dashboard
wooCommerceOrderEdit: async ( { page }, use ) => {
await use( new WooCommerceOrderEdit( { page } ) );
},
wooCommerceSubscriptionEdit: async ( { page }, use ) => {
await use( new WooCommerceSubscriptionEdit( { page } ) );
},
// WooCommerce front end
shop: async ( { visitorPage, ppui }, use ) => {
await use( new Shop( { page: visitorPage, ppui } ) );
},
product: async ( { visitorPage, ppui }, use ) => {
await use( new Product( { page: visitorPage, ppui } ) );
},
cart: async ( { visitorPage, ppui }, use ) => {
await use( new Cart( { page: visitorPage, ppui } ) );
},
checkout: async ( { visitorPage, ppui }, use ) => {
await use( new Checkout( { page: visitorPage, ppui } ) );
},
classicCart: async ( { visitorPage, ppui }, use ) => {
await use( new ClassicCart( { page: visitorPage, ppui } ) );
},
classicCheckout: async ( { visitorPage, ppui }, use ) => {
await use( new ClassicCheckout( { page: visitorPage, ppui } ) );
},
classicPayForOrder: async ( { visitorPage, ppui }, use ) => {
await use( new ClassicPayForOrder( { page: visitorPage, ppui } ) );
},
payForOrder: async ( { visitorPage, ppui }, use ) => {
await use( new PayForOrder( { page: visitorPage, ppui } ) );
},
orderReceived: async ( { visitorPage, ppui }, use ) => {
await use( new OrderReceived( { page: visitorPage, ppui } ) );
},
customerAccount: async ( { visitorPage, ppui }, use ) => {
await use( new CustomerAccount( { page: visitorPage, ppui } ) );
},
customerPaymentMethods: async ( { visitorPage, ppui }, use ) => {
await use( new CustomerPaymentMethods( { page: visitorPage, ppui } ) );
},
customerSubscriptions: async ( { visitorPage, ppui }, use ) => {
await use( new CustomerSubscriptions( { page: visitorPage, ppui } ) );
},
// Utils & preconditions
utils: async (
{
plugins,
wooCommerceUtils,
requestUtils,
wooCommerceApi,
pcpOnboarding,
payForOrder,
checkout,
classicCheckout,
orderReceived,
customerAccount,
customerPaymentMethods,
visitorWooCommerceApi,
},
use
) => {
await use(
new Utils( {
plugins,
wooCommerceUtils,
requestUtils,
wooCommerceApi,
pcpOnboarding,
payForOrder,
checkout,
classicCheckout,
orderReceived,
customerAccount,
customerPaymentMethods,
visitorWooCommerceApi,
} )
);
},
} );
export { test, expect };