Skip to content

Commit

Permalink
Convert Shopper Multi Currency Widget spec to Playwright (#10204)
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaeldcom authored Jan 23, 2025
1 parent 65c0450 commit 936b878
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 207 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Convert shopper-multi-currency-widget spec from Puppeteer to Playwright
4 changes: 2 additions & 2 deletions tests/e2e-pw/specs/shopper/multi-currency-checkout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
activateMulticurrency,
addCurrency,
deactivateMulticurrency,
removeCurrency,
restoreCurrencies,
} from '../../utils/merchant';
import { emptyCart, placeOrderWithCurrency } from '../../utils/shopper';
import * as navigation from '../../utils/shopper-navigation';
Expand All @@ -33,7 +33,7 @@ test.describe( 'Multi-currency checkout', () => {
} );

test.afterAll( async () => {
await removeCurrency( merchantPage, 'EUR' );
await restoreCurrencies( merchantPage );
await emptyCart( shopperPage );

if ( ! wasMulticurrencyEnabled ) {
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e-pw/specs/shopper/shopper-bnpls-checkout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ const bnplProviders = [ 'Affirm', 'Afterpay' ];
test.describe( 'BNPL checkout', () => {
let merchantPage: Page;
let shopperPage: Page;
let wasMulticurrencyEnabled: boolean;

test.beforeAll( async ( { browser } ) => {
shopperPage = ( await getShopper( browser ) ).shopperPage;
merchantPage = ( await getMerchant( browser ) ).merchantPage;
wasMulticurrencyEnabled = await merchant.isMulticurrencyEnabled(
merchantPage
);

await merchant.enablePaymentMethods( merchantPage, bnplProviders );
if ( wasMulticurrencyEnabled ) {
await merchant.deactivateMulticurrency( merchantPage );
}
} );

test.afterAll( async () => {
await merchant.disablePaymentMethods( merchantPage, bnplProviders );
if ( wasMulticurrencyEnabled ) {
await merchant.activateMulticurrency( merchantPage );
}
} );

for ( const ctpEnabled of cardTestingProtectionStates ) {
Expand Down
126 changes: 126 additions & 0 deletions tests/e2e-pw/specs/shopper/shopper-multi-currency-widget.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* External dependencies
*/
import { test, expect, Page } from '@playwright/test';
/**
* Internal dependencies
*/
import * as merchant from '../../utils/merchant';
import * as shopper from '../../utils/shopper';
import * as navigation from '../../utils/shopper-navigation';
import { goToOrder } from '../../utils/merchant-navigation';
import RestAPI from '../../utils/rest-api';
import { getMerchant, getShopper } from '../../utils/helpers';

test.describe( 'Shopper Multi-Currency widget', () => {
let merchantPage: Page;
let shopperPage: Page;
let wasMulticurrencyEnabled: boolean;

test.beforeAll( async ( { browser } ) => {
shopperPage = ( await getShopper( browser ) ).shopperPage;
merchantPage = ( await getMerchant( browser ) ).merchantPage;
wasMulticurrencyEnabled = await merchant.activateMulticurrency(
merchantPage
);
await merchant.restoreCurrencies( merchantPage );
} );

test.afterAll( async () => {
if ( ! wasMulticurrencyEnabled ) {
await merchant.deactivateMulticurrency( merchantPage );
}
} );

test( 'should display currency switcher widget if multi-currency is enabled', async () => {
await merchant.addMulticurrencyWidget( merchantPage );

await navigation.goToShop( shopperPage );
await expect(
shopperPage.locator( '.widget select[name=currency]' )
).toBeVisible();
} );

test( 'should not display currency switcher widget if multi-currency is disabled', async () => {
await merchant.deactivateMulticurrency( merchantPage );

await navigation.goToShop( shopperPage );
await expect(
shopperPage.locator( '.widget select[name=currency]' )
).not.toBeVisible();

await merchant.activateMulticurrency( merchantPage );
} );

test.describe( 'Should allow shopper to switch currency', () => {
test.afterEach( async () => {
await shopperPage.selectOption(
'.widget select[name=currency]',
'EUR'
);
await expect( shopperPage ).toHaveURL( /.*currency=EUR/ );

// Change it back to USD for the other tests.
await navigation.goToShopWithCurrency( shopperPage, 'USD' );
} );

test( 'at the product page', async () => {
await navigation.goToProductPageBySlug( shopperPage, 'beanie' );
} );

test( 'at the cart page', async () => {
await navigation.goToCart( shopperPage );
} );

test( 'at the checkout page', async () => {
await navigation.goToCheckout( shopperPage );
} );
} );

test.describe( 'Should not affect prices', () => {
let orderId: string;

test.afterEach( async () => {
await shopperPage.selectOption(
'.widget select[name=currency]',
'EUR'
);

await expect(
shopperPage.getByText( '$18.00 USD' ).first()
).toBeVisible();

await navigation.goToShopWithCurrency( shopperPage, 'USD' );
} );

test( 'at the order received page', async () => {
orderId = await shopper.placeOrderWithCurrency(
shopperPage,
'USD'
);
} );

test( 'at My account > Orders', async () => {
await navigation.goToOrders( shopperPage );
await expect(
shopperPage.getByLabel( `View order number ${ orderId }` )
).toBeVisible();
} );
} );

test( 'should not display currency switcher on pay for order page', async ( {}, {
project,
} ) => {
const restApi = new RestAPI( project.use.baseURL );
const orderId = await restApi.createOrder();

await goToOrder( merchantPage, orderId );
await merchantPage
.getByRole( 'link', { name: 'Customer payment page' } )
.click();

await expect(
merchantPage.locator( '.widget select[name=currency]' )
).not.toBeVisible();
} );
} );
4 changes: 3 additions & 1 deletion tests/e2e-pw/utils/merchant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ export const deactivateMulticurrency = async ( page: Page ) => {
export const addMulticurrencyWidget = async ( page: Page ) => {
await navigation.goToWidgets( page );
// Wait for all widgets to load. This is important to prevent flakiness.
await page.locator( '.components-spinner' ).first().waitFor();
await expect( page.locator( '.components-spinner' ) ).toHaveCount( 0 );

if ( await page.getByRole( 'button', { name: 'Close' } ).isVisible() ) {
await page.getByRole( 'button', { name: 'Close' } ).click();
}

const isWidgetAdded = await page
.getByRole( 'heading', { name: 'Currency Switcher Widget' } )
.locator( 'iframe[srcdoc*=currency]' )
.first()
.isVisible();

if ( ! isWidgetAdded ) {
Expand Down
16 changes: 16 additions & 0 deletions tests/e2e-pw/utils/rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HTTPClientFactory } from '@woocommerce/api';
import { config } from '../config/default';

const userEndpoint = '/wp/v2/users';
const ordersEndpoint = '/wc/v3/orders';

class RestAPI {
private baseUrl: string;
Expand Down Expand Up @@ -63,6 +64,21 @@ class RestAPI {
}
}
}

async createOrder(): Promise< string > {
const client = this.getAdminClient();

const order = await client.post( ordersEndpoint, {
line_items: [
{
product_id: 16,
quantity: 1,
},
],
} );

return `${ order.data.id }`;
}
}

export default RestAPI;
Loading

0 comments on commit 936b878

Please sign in to comment.