Skip to content

Commit

Permalink
[e2e tests] Introduce Faker - update checkout login test to use uniqu…
Browse files Browse the repository at this point in the history
…e and realistic test data (woocommerce#53385)
  • Loading branch information
adimoldovan authored Dec 4, 2024
1 parent d837888 commit 9239dec
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 338 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

E2E tests: update checkout-login test to use the existing customer instead of creating a new one
3 changes: 2 additions & 1 deletion plugins/woocommerce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@
"@babel/core": "7.12.9",
"@babel/preset-env": "7.12.7",
"@babel/register": "7.12.1",
"@faker-js/faker": "^9.3.0",
"@playwright/test": "^1.46.1",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/experimental-utils": "^5.62.0",
Expand All @@ -708,9 +709,9 @@
"@woocommerce/e2e-core-tests": "workspace:*",
"@woocommerce/e2e-environment": "workspace:*",
"@woocommerce/e2e-utils": "workspace:*",
"@woocommerce/e2e-utils-playwright": "workspace:*",
"@woocommerce/eslint-plugin": "workspace:*",
"@woocommerce/woocommerce-rest-api": "^1.0.1",
"@woocommerce/e2e-utils-playwright": "workspace:*",
"@wordpress/babel-plugin-import-jsx-pragma": "1.1.3",
"@wordpress/babel-preset-default": "3.0.2",
"@wordpress/e2e-test-utils-playwright": "wp-6.6",
Expand Down
183 changes: 87 additions & 96 deletions plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-login.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,128 +5,119 @@ import {
addAProductToCart,
getOrderIdFromUrl,
} from '@woocommerce/e2e-utils-playwright';
const { test, expect } = require( '@playwright/test' );
const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default;

const customer = {
username: 'customercheckoutlogin',
password: 'password',
email: `customercheckoutlogin${ new Date()
.getTime()
.toString() }@woocommercecoree2etestsuite.com`,
billing: {
first_name: 'Jane',
last_name: 'Smith',
address_1: '123 Anywhere St.',
address_2: 'Apartment 42',
city: 'New York',
state: 'NY',
postcode: '10010',
country: 'US',
phone: '(555) 777-7777',
},
};
const { test: baseTest, expect } = require( '../../fixtures/fixtures' );
const { getFakeCustomer, getFakeProduct } = require( '../../utils/data' );

test.describe(
'Shopper Checkout Login Account',
{ tag: [ '@payments', '@services', '@hpos' ] },
() => {
let productId, orderId, shippingZoneId, customerId;

test.beforeAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
// add product
await api
.post( 'products', {
name: 'Checkout Login Account',
type: 'simple',
regular_price: '19.99',
} )
.then( ( response ) => {
productId = response.data.id;
} );
const test = baseTest.extend( {
page: async ( { page, api }, use ) => {
// get the current value of woocommerce_enable_checkout_login_reminder
const response = await api.get(
'settings/account/woocommerce_enable_checkout_login_reminder'
);

// enable the setting if it is not already enabled
const initialValue = response.data.value;
if ( initialValue !== 'yes' ) {
await api.put(
'settings/account/woocommerce_enable_checkout_login_reminder',
{
value: 'yes',
}
);
// add a shipping zone and method
await api
.post( 'shipping/zones', {
name: 'Free Shipping New York',
} )
.then( ( response ) => {
shippingZoneId = response.data.id;
} );
await api.put( `shipping/zones/${ shippingZoneId }/locations`, [
{
code: 'US:NY',
type: 'state',
},
] );
await api.post( `shipping/zones/${ shippingZoneId }/methods`, {
method_id: 'free_shipping',
} );
// create customer and save its id
await api
.post( 'customers', customer )
.then( ( response ) => ( customerId = response.data.id ) );
// enable a payment method
}

// Check id COD payment is enabled and enable it if it is not
const codResponse = await api.get( 'payment_gateways/cod' );
const codEnabled = codResponse.enabled;

if ( ! codEnabled ) {
await api.put( 'payment_gateways/cod', {
enabled: true,
} );
} );
}

test.afterAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api.delete( `products/${ productId }`, {
force: true,
} );
if ( orderId ) {
await api.delete( `orders/${ orderId }`, { force: true } );
}
await use( page );

// revert the setting to its original value
if ( initialValue !== 'yes' ) {
await api.put(
'settings/account/woocommerce_enable_checkout_login_reminder',
{
value: 'no',
value: initialValue,
}
);
// delete the customer
await api.delete( `customers/${ customerId }`, {
force: true,
} );
// disable payment method
}

if ( ! codEnabled ) {
await api.put( 'payment_gateways/cod', {
enabled: false,
enabled: codEnabled,
} );
// delete shipping
await api.delete( `shipping/zones/${ shippingZoneId }`, {
force: true,
}
},
product: async ( { api }, use ) => {
let product;

await api.post( 'products', getFakeProduct() ).then( ( response ) => {
product = response.data;
} );

await use( product );

await api.delete( `products/${ product.id }`, { force: true } );
},
customer: async ( { api }, use ) => {
const customerData = getFakeCustomer();
let customer;

await api.post( 'customers', customerData ).then( ( response ) => {
customer = response.data;
customer.password = customerData.password;
} );

// add a shipping zone and method for the customer
let shippingZoneId;
await api
.post( 'shipping/zones', {
name: `Free Shipping ${ customerData.shipping.city }`,
} )
.then( ( response ) => {
shippingZoneId = response.data.id;
} );
await api.put( `shipping/zones/${ shippingZoneId }/locations`, [
{
code: `${ customerData.shipping.country }:${ customerData.shipping.state }`,
type: 'state',
},
] );
await api.post( `shipping/zones/${ shippingZoneId }/methods`, {
method_id: 'free_shipping',
} );

test.beforeEach( async ( { page, context } ) => {
// Shopping cart is very sensitive to cookies, so be explicit
await context.clearCookies();
await use( customer );

// all tests use the first product
await addAProductToCart( page, productId );
await api.delete( `customers/${ customer.id }`, { force: true } );
await api.delete( `shipping/zones/${ shippingZoneId }`, {
force: true,
} );
},
order: async ( { api }, use ) => {
const order = {};
await use( order );
await api.delete( `orders/${ order.id }`, { force: true } );
},
} );

test.describe(
'Shopper Checkout Login Account',
{ tag: [ '@payments', '@services', '@hpos' ] },
() => {
test( 'can login to an existing account during checkout', async ( {
page,
product,
customer,
order,
} ) => {
await addAProductToCart( page, product.id );
await page.goto( '/checkout/' );
await page.locator( 'text=Click here to login' ).click();

Expand Down Expand Up @@ -167,7 +158,7 @@ test.describe(
page.getByText( 'Your order has been received' )
).toBeVisible();

orderId = getOrderIdFromUrl( page );
order.id = getOrderIdFromUrl( page );

await expect( page.getByText( customer.email ) ).toBeVisible();

Expand Down
60 changes: 60 additions & 0 deletions plugins/woocommerce/tests/e2e-pw/utils/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { faker } = require( '@faker-js/faker' );

function getFakeUser( role ) {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const email = faker.internet.email( {
firstName,
lastName,
provider: 'example.fakerjs.dev',
} );

return {
email,
first_name: firstName,
last_name: lastName,
role,
username: faker.internet.username( { firstName, lastName } ),
password: faker.internet.password(),
billing: {
first_name: firstName,
last_name: lastName,
address_1: '969 Market',
address_2: '',
city: 'San Francisco',
state: 'CA',
postcode: '94103',
country: 'US',
email,
phone: '(555) 555-5555',
},
shipping: {
first_name: firstName,
last_name: lastName,
address_1: '969 Market',
address_2: '',
city: 'San Francisco',
state: 'CA',
postcode: '94103',
country: 'US',
},
};
}

function getFakeCustomer() {
return getFakeUser( 'customer' );
}

function getFakeProduct() {
return {
name: `${ faker.commerce.productName() }`,
description: faker.commerce.productDescription(),
regular_price: faker.commerce.price(),
type: 'simple',
};
}

module.exports = {
getFakeCustomer,
getFakeProduct,
};
Loading

0 comments on commit 9239dec

Please sign in to comment.