Skip to content

Commit fb58db7

Browse files
Merge pull request #1988 from WordPress/add/setup-e2e-tests
Add E2E tests setup for Auto Sizes
2 parents 7139d2c + 645ced5 commit fb58db7

File tree

7 files changed

+173
-4
lines changed

7 files changed

+173
-4
lines changed

.github/workflows/e2e-test.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: End-to-End Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- trunk
7+
- 'release/**'
8+
- 'add/setup-e2e-tests'
9+
paths:
10+
- '.github/workflows/e2e-test.yml'
11+
- 'plugins/auto-sizes/**'
12+
- '**/package.json'
13+
- 'package-lock.json'
14+
- 'composer.json'
15+
- 'composer.lock'
16+
pull_request:
17+
paths:
18+
- '.github/workflows/e2e-test.yml'
19+
- 'plugins/auto-sizes/**'
20+
- '**/package.json'
21+
- 'package-lock.json'
22+
- 'composer.json'
23+
- 'composer.lock'
24+
types:
25+
- opened
26+
- reopened
27+
- synchronize
28+
29+
jobs:
30+
e2e-test:
31+
name: E2E Tests
32+
runs-on: ubuntu-latest
33+
timeout-minutes: 20
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v4
37+
38+
- name: Setup Node.js (.nvmrc)
39+
uses: actions/setup-node@v4
40+
with:
41+
node-version-file: '.nvmrc'
42+
cache: npm
43+
44+
- name: Install npm dependencies
45+
run: npm ci
46+
47+
- name: Build assets
48+
run: npm run build
49+
50+
- name: Install Playwright dependencies
51+
run: npx playwright install chromium --with-deps
52+
53+
- name: Install WordPress
54+
run: npm run wp-env start
55+
56+
- name: Run tests
57+
env:
58+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
59+
run: npm run test-e2e

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
############
44

55
plugins/*/tests/**/actual.html
6+
artifacts
7+
test-results
68

79
############
810
## IDEs

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
},
1313
"devDependencies": {
1414
"@octokit/rest": "^21.1.1",
15+
"@playwright/test": "^1.51.1",
16+
"@wordpress/e2e-test-utils-playwright": "^1.21.0",
1517
"@wordpress/env": "^10.22.0",
1618
"@wordpress/prettier-config": "^4.20.0",
1719
"@wordpress/scripts": "^30.15.0",
@@ -52,6 +54,9 @@
5254
"tsc": "tsc",
5355
"format-php": "composer format:all",
5456
"phpstan": "composer phpstan",
57+
"test-e2e": "wp-scripts test-playwright --config tools/e2e/playwright.config.ts",
58+
"test-e2e:debug": "wp-scripts test-playwright --config tools/e2e/playwright.config.ts --ui",
59+
"test-e2e:auto-sizes": "wp-scripts test-playwright --config tools/e2e/playwright.config.ts --project=auto-sizes",
5560
"lint-php": "composer lint:all",
5661
"test-php": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:plugins",
5762
"test-php-watch": "./bin/test-php-watch.sh",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* External dependencies
3+
*/
4+
const path = require( 'path' );
5+
6+
/**
7+
* WordPress dependencies
8+
*/
9+
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
10+
11+
test.describe( 'check accurate sizes', () => {
12+
test.beforeAll( async ( { requestUtils } ) => {
13+
await requestUtils.activateTheme( 'twentytwentyfour' );
14+
await requestUtils.deactivatePlugin( 'enhanced-responsive-images' );
15+
await requestUtils.deactivatePlugin( 'modern-image-formats' );
16+
await requestUtils.deleteAllMedia();
17+
} );
18+
19+
test.afterEach( async ( { requestUtils } ) => {
20+
await requestUtils.deleteAllMedia();
21+
} );
22+
23+
test( 'should get smaller version of image', async ( {
24+
admin,
25+
editor,
26+
requestUtils,
27+
page,
28+
} ) => {
29+
const filename = 'leaves.jpg';
30+
const filepath = path.join(
31+
__dirname + '/../../data/images/',
32+
filename
33+
);
34+
35+
await admin.createNewPost();
36+
const media = await requestUtils.uploadMedia( filepath );
37+
38+
await editor.insertBlock( {
39+
name: 'core/group',
40+
attributes: {
41+
align: 'wide',
42+
},
43+
innerBlocks: [
44+
{
45+
name: 'core/image',
46+
attributes: {
47+
alt: filename,
48+
id: media.id,
49+
url: media.source_url,
50+
},
51+
},
52+
],
53+
} );
54+
55+
const postId = await editor.publishPost();
56+
57+
// Navigate to the post and wait for the image to load.
58+
await page.goto( `/?p=${ postId }` );
59+
const imageElement = await page.getByAltText( filename );
60+
const imageSizes = await imageElement.getAttribute( 'sizes' );
61+
62+
// Activate the plugin.
63+
await requestUtils.activatePlugin( 'enhanced-responsive-images' );
64+
65+
// Reload the page and wait for the image to load.
66+
await page.goto( `/?p=${ postId }` );
67+
const updatedImageElement = await page.getByAltText( filename );
68+
69+
const updatedImageSizes =
70+
await updatedImageElement.getAttribute( 'sizes' );
71+
72+
expect( imageSizes ).not.toStrictEqual( updatedImageSizes );
73+
74+
expect( updatedImageSizes ).toStrictEqual(
75+
'(max-width: 620px) 100vw, 620px'
76+
);
77+
78+
const currentSrc = await updatedImageElement.evaluate( ( img ) =>
79+
img instanceof HTMLImageElement ? img.currentSrc : null
80+
);
81+
await expect( currentSrc ).toContain( 'leaves-768x512.jpg' );
82+
} );
83+
} );

tools/e2e/playwright.config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { defineConfig } from '@playwright/test';
5+
6+
/**
7+
* WordPress dependencies
8+
*/
9+
import baseConfig from '@wordpress/scripts/config/playwright.config.js';
10+
11+
const config = defineConfig( {
12+
...baseConfig,
13+
projects: [
14+
{
15+
name: 'auto-sizes',
16+
testDir: '../../plugins/auto-sizes/tests/e2e/specs',
17+
},
18+
],
19+
} );
20+
21+
export default config;

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"include": [
1212
"plugins/**/*.js",
1313
"plugins/**/*.ts",
14+
"tools/**/*.ts",
1415
],
1516
"exclude": [
1617
"plugins/*/build/*"

0 commit comments

Comments
 (0)