[e2e tests] Add tests for product bulks edit (#44039)

This commit is contained in:
Adrian Moldovan 2024-01-26 10:53:05 +02:00 committed by GitHub
parent 067cdef753
commit 4bcb62dc9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 179 additions and 54 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
E2E tests: add tests for products bulk edit

View File

@ -4,7 +4,7 @@ const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default;
baseTest.describe( 'Products > Delete Product', () => {
baseTest.use( { storageState: process.env.ADMINSTATE } );
const apiFixture = baseTest.extend( {
const test = baseTest.extend( {
api: async ( { baseURL }, use ) => {
const api = new wcApi( {
url: baseURL,
@ -23,11 +23,9 @@ baseTest.describe( 'Products > Delete Product', () => {
await use( api );
},
} );
const test = apiFixture.extend( {
product: async ( { api }, use ) => {
const product = {
let product = {
id: 0,
name: `Product ${ Date.now() }`,
type: 'simple',
@ -35,7 +33,7 @@ baseTest.describe( 'Products > Delete Product', () => {
};
await api.post( 'products', product ).then( ( response ) => {
product.id = response.data.id;
product = response.data;
} );
await use( product );

View File

@ -1,64 +1,187 @@
const { test, expect } = require( '@playwright/test' );
const { test: baseTest, expect } = require( '@playwright/test' );
const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default;
test.describe( 'Products > Edit Product', () => {
test.use( { storageState: process.env.ADMINSTATE } );
baseTest.describe( 'Products > Edit Product', () => {
baseTest.use( { storageState: process.env.ADMINSTATE } );
let productId;
test.beforeAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api
.post( 'products', {
name: 'Product to edit',
type: 'simple',
regular_price: '12.99',
} )
.then( ( response ) => {
productId = response.data.id;
const test = baseTest.extend( {
api: async ( { baseURL }, use ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await use( api );
},
products: async ( { api }, use ) => {
const products = [];
for ( let i = 0; i < 2; i++ ) {
await api
.post( 'products', {
id: 0,
name: `Product ${ i }_${ Date.now() }`,
type: 'simple',
regular_price: `${ 12.99 + i }`,
manage_stock: true,
stock_quantity: 10 + i,
stock_status: 'instock',
} )
.then( ( response ) => {
products.push( response.data );
} );
}
await use( products );
// Cleanup
for ( const product of products ) {
await api.delete( `products/${ product.id }`, { force: true } );
}
},
} );
test.afterAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
test( 'can edit a product and save the changes', async ( {
page,
products,
} ) => {
await page.goto(
`wp-admin/post.php?post=${ products[ 0 ].id }&action=edit`
);
const newProduct = {
name: `Product ${ Date.now() }`,
description: `This product is pretty awesome ${ Date.now() }`,
regularPrice: '100.05',
salePrice: '99.05',
};
await test.step( 'edit the product name', async () => {
await page.getByLabel( 'Product name' ).fill( newProduct.name );
} );
await api.delete( `products/${ productId }`, {
force: true,
await test.step( 'edit the product description', async () => {
await page.locator( '#content-html' ).click(); // text mode to work around iframe
await page
.locator( '.wp-editor-area' )
.first()
.fill( newProduct.description );
} );
await test.step( 'edit the product price', async () => {
await page
.getByLabel( 'Regular price ($)' )
.fill( newProduct.regularPrice );
await page
.getByLabel( 'Sale price ($)' )
.fill( newProduct.salePrice );
} );
await test.step( 'publish the updated product', async () => {
await page.getByRole( 'button', { name: 'Update' } ).click();
} );
await test.step( 'verify the changes', async () => {
await expect( page.getByLabel( 'Product name' ) ).toHaveValue(
newProduct.name
);
await expect(
page.locator( '.wp-editor-area' ).first()
).toContainText( newProduct.description );
await expect( page.getByLabel( 'Regular price ($)' ) ).toHaveValue(
newProduct.regularPrice
);
await expect( page.getByLabel( 'Sale price ($)' ) ).toHaveValue(
newProduct.salePrice
);
} );
} );
test( 'can edit a product and save the changes', async ( { page } ) => {
await page.goto( `wp-admin/post.php?post=${ productId }&action=edit` );
test( 'can bulk edit products', async ( { page, products } ) => {
await page.goto( `wp-admin/edit.php?post_type=product` );
// make some edits
await page.locator( '#title' ).fill( 'Awesome product' );
await page.locator( '#content-html' ).click(); // text mode to work around iframe
await page
.locator( '.wp-editor-area >> nth=0' )
.fill( 'This product is pretty awesome' );
await page.locator( '#_regular_price' ).fill( '100.05' );
const regularPriceIncrease = 10;
const salePriceDecrease = 10;
const stockQtyIncrease = 10;
// publish the edits
await page.locator( '#publish' ).click();
await test.step( 'select and bulk edit the products', async () => {
for ( const product of products ) {
await page.getByLabel( `Select ${ product.name }` ).click();
}
// verify the changes saved
await expect( page.locator( '#title' ) ).toHaveValue(
'Awesome product'
);
await expect(
page.locator( '.wp-editor-area >> nth=0' )
).toContainText( 'This product is pretty awesome' );
await expect( page.locator( '#_regular_price' ) ).toHaveValue(
'100.05'
);
await page
.locator( '#bulk-action-selector-top' )
.selectOption( 'Edit' );
await page.locator( '#doaction' ).click();
await expect(
await page.locator( '#bulk-titles-list li' ).count()
).toEqual( products.length );
} );
await test.step( 'update the regular price', async () => {
await page
.locator( 'select[name="change_regular_price"]' )
.selectOption(
'Increase existing price by (fixed amount or %):'
);
await page
.getByPlaceholder( 'Enter price ($)' )
.fill( `${ regularPriceIncrease }%` );
} );
await test.step( 'update the sale price', async () => {
await page
.locator( 'select[name="change_sale_price"]' )
.selectOption(
'Set to regular price decreased by (fixed amount or %):'
);
await page
.getByPlaceholder( 'Enter sale price ($)' )
.fill( `${ salePriceDecrease }%` );
} );
await test.step( 'update the stock quantity', async () => {
await page
.locator( 'select[name="change_stock"]' )
.selectOption( 'Increase existing stock by:' );
await page
.getByPlaceholder( 'Stock qty' )
.fill( `${ stockQtyIncrease }` );
} );
await test.step( 'save the updates', async () => {
await page.getByRole( 'button', { name: 'Update' } ).click();
} );
await test.step( 'verify the changes', async () => {
for ( const product of products ) {
await page.goto( `product/${ product.slug }` );
const expectedRegularPrice = (
product.regular_price *
( 1 + regularPriceIncrease / 100 )
).toFixed( 2 );
const expectedSalePrice = (
expectedRegularPrice *
( 1 - salePriceDecrease / 100 )
).toFixed( 2 );
const expectedStockQty =
product.stock_quantity + stockQtyIncrease;
await expect
.soft( page.locator( 'p' ).locator( 'del' ) )
.toHaveText( `$${ expectedRegularPrice }` );
await expect
.soft( page.locator( 'p' ).getByRole( 'insertion' ) )
.toHaveText( `$${ expectedSalePrice }` );
await expect
.soft( page.getByText( `${ expectedStockQty } in stock` ) )
.toBeVisible();
}
} );
} );
} );