Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit b190d23

Browse files
authored
Add e2e tests for attributes count (#9500)
* Add e2e tests for attributes count * Add test with pricing filter and turn on debug to prevent cache * Prevent tests from passing if test page is not loaded * Use WP wrapper to call WC CLI * Refactor to use more of PW methods * Use existing active filters block post for testing * Move prepareAttributes function to global setup
1 parent ca1d989 commit b190d23

File tree

3 files changed

+128
-15
lines changed

3 files changed

+128
-15
lines changed

tests/e2e-pw/bin/test-env-setup.sh

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ wp-env run tests-cli "wp site empty --yes"
1616
# If no attributes exist, otherwiese create them
1717
###################################################################################################
1818

19-
attributes=$(wp-env run tests-cli "wc product_attribute list --format=json --user=1")
19+
attributes=$(wp-env run tests-cli "wp wc product_attribute list --format=json --user=1")
2020

2121
if [ -z "$attributes" ] || [ "$attributes" == "[]" ]; then
22-
pa_color=$(wp-env run tests-cli "wc product_attribute create \
22+
pa_color=$(wp-env run tests-cli "wp wc product_attribute create \
2323
--name=Color \
2424
--slug=pa_color \
2525
--user=1 \
2626
--porcelain \
2727
")
2828

29-
pa_size=$(wp-env run tests-cli "wc product_attribute create \
29+
pa_size=$(wp-env run tests-cli "wp wc product_attribute create \
3030
--name=Size \
3131
--slug=pa_size \
3232
--user=1 \
@@ -35,9 +35,8 @@ if [ -z "$attributes" ] || [ "$attributes" == "[]" ]; then
3535
fi
3636

3737
###################################################################################################
38-
# Import sample products and egenerate product lookup tables
38+
# Import sample products and regenerate product lookup tables
3939
###################################################################################################
40-
4140
wp-env run tests-cli "wp import wp-content/plugins/woocommerce/sample-data/sample_products.xml --authors=skip"
4241
wp-env run tests-cli "wp wc tool run regenerate_product_lookup_tables --user=1"
4342

@@ -216,14 +215,6 @@ wp-env run tests-cli "wp post create \
216215
--post_content=\"$post_content\"
217216
"
218217

219-
post_content=$(cat "${script_dir}/product-category.txt" | sed 's/"/\\"/g')
220-
wp-env run tests-cli "wp post create \
221-
--post_status=publish \
222-
--post_author=1 \
223-
--post_title='Products by Category block' \
224-
--post_content=\"$post_content\"
225-
"
226-
227218
post_content=$(cat "${script_dir}/product-categories.txt" | sed 's/"/\\"/g')
228219
wp-env run tests-cli "wp post create \
229220
--post_status=publish \

tests/e2e-pw/global-setup.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
import { FullConfig, chromium, request } from '@playwright/test';
66
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright';
77
import fs from 'fs';
8+
import { cli } from '@woocommerce/e2e-utils';
89

910
/**
1011
* Internal dependencies
1112
*/
12-
import { customer } from './test-data/data/data';
13+
import { customer, admin } from './test-data/data/data';
1314

1415
const loginAsCustomer = async ( config: FullConfig ) => {
1516
const { stateDir, baseURL, userAgent } = config.projects[ 0 ].use;
1617

1718
// used throughout tests for authentication
18-
process.env.ADMINSTATE = `${ stateDir }adminState.json`;
1919
process.env.CUSTOMERSTATE = `${ stateDir }customerState.json`;
2020

2121
try {
@@ -75,6 +75,52 @@ const loginAsCustomer = async ( config: FullConfig ) => {
7575
await browser.close();
7676
};
7777

78+
const prepareAttributes = async ( config: FullConfig ) => {
79+
const { baseURL, userAgent } = config.projects[ 0 ].use;
80+
81+
// Specify user agent when running against an external test site to avoid getting HTTP 406 NOT ACCEPTABLE errors.
82+
const contextOptions = { baseURL, userAgent };
83+
84+
// Create browser, browserContext, and page for customer and admin users
85+
const browser = await chromium.launch();
86+
const context = await browser.newContext( contextOptions );
87+
const page = await context.newPage();
88+
89+
await page.goto( `/wp-admin` );
90+
await page.fill( 'input[name="log"]', admin.username );
91+
await page.fill( 'input[name="pwd"]', admin.password );
92+
await page.click( 'text=Log In' );
93+
94+
/*
95+
* Intercept the dialog event.
96+
* This is needed because when the regenerate
97+
* button is clicked, a dialog is shown.
98+
*/
99+
page.on( 'dialog', async ( dialog ) => {
100+
await dialog.accept();
101+
} );
102+
103+
await page.goto( '/wp-admin/admin.php?page=wc-status&tab=tools' );
104+
105+
await page.click( '.regenerate_product_attributes_lookup_table input' );
106+
107+
await context.close();
108+
await browser.close();
109+
110+
/*
111+
* Note that the two commands below are intentionally
112+
* duplicated as we need to run the cron task twice as
113+
* we need to process more than 1 batch of items.
114+
*/
115+
await cli(
116+
`npm run wp-env run tests-cli wp action-scheduler run --hooks="woocommerce_run_product_attribute_lookup_regeneration_callback"`
117+
);
118+
119+
await cli(
120+
`npm run wp-env run tests-cli wp action-scheduler run --hooks="woocommerce_run_product_attribute_lookup_regeneration_callback"`
121+
);
122+
};
123+
78124
async function globalSetup( config: FullConfig ) {
79125
const { storageState, baseURL } = config.projects[ 0 ].use;
80126
const storageStatePath =
@@ -91,6 +137,7 @@ async function globalSetup( config: FullConfig ) {
91137
await requestUtils.setupRest();
92138
await requestContext.dispose();
93139

140+
await prepareAttributes( config );
94141
await loginAsCustomer( config );
95142
}
96143

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { test, expect } from '@woocommerce/e2e-playwright-utils';
5+
6+
test.describe( 'Filter by Attributes Block - with All products Block', () => {
7+
test( 'should show correct attrs count (color=blue|query_type_color=or)', async ( {
8+
page,
9+
} ) => {
10+
await page.goto(
11+
'/active-filters-block/?filter_color=blue&query_type_color=or'
12+
);
13+
await page.waitForLoadState( 'networkidle' );
14+
15+
// Check if the page has loaded successfully.
16+
await expect( page.getByText( 'Active Filters block' ) ).toBeVisible();
17+
18+
const expectedValues = [ '4', '0', '2', '2', '0' ];
19+
20+
await expect(
21+
page
22+
.locator( 'ul.wc-block-attribute-filter-list' )
23+
.first()
24+
.locator(
25+
'> li:not([class^="is-loading"]) .wc-filter-element-label-list-count > span:not([class^="screen-reader"])'
26+
)
27+
).toHaveText( expectedValues );
28+
} );
29+
30+
test( 'should show correct attrs count (color=blue,gray|query_type_color=or)', async ( {
31+
page,
32+
} ) => {
33+
await page.goto(
34+
'/active-filters-block/?filter_color=blue,gray&query_type_color=or'
35+
);
36+
await page.waitForLoadState( 'networkidle' );
37+
38+
// Check if the page has loaded successfully.
39+
await expect( page.getByText( 'Active Filters block' ) ).toBeVisible();
40+
41+
const expectedValues = [ '4', '3', '2', '2', '0' ];
42+
43+
await expect(
44+
page
45+
.locator( 'ul.wc-block-attribute-filter-list' )
46+
.first()
47+
.locator(
48+
'> li:not([class^="is-loading"]) .wc-filter-element-label-list-count > span:not([class^="screen-reader"])'
49+
)
50+
).toHaveText( expectedValues );
51+
} );
52+
53+
test( 'should show correct attrs count (color=blue|query_type_color=or|min_price=15|max_price=40)', async ( {
54+
page,
55+
} ) => {
56+
await page.goto(
57+
'/active-filters-block/?filter_color=blue&query_type_color=or&min_price=15&max_price=40'
58+
);
59+
await page.waitForLoadState( 'networkidle' );
60+
61+
// Check if the page has loaded successfully.
62+
await expect( page.getByText( 'Active Filters block' ) ).toBeVisible();
63+
64+
const expectedValues = [ '2', '0', '1', '1', '0' ];
65+
66+
await expect(
67+
page
68+
.locator( 'ul.wc-block-attribute-filter-list' )
69+
.first()
70+
.locator(
71+
'> li:not([class^="is-loading"]) .wc-filter-element-label-list-count > span:not([class^="screen-reader"])'
72+
)
73+
).toHaveText( expectedValues );
74+
} );
75+
} );

0 commit comments

Comments
 (0)