Skip to content

Add E2E tests setup for Auto Sizes #1988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: feature/1511-incorporate-layout-constraints-from-ancestors
Choose a base branch
from
59 changes: 59 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: End-to-End Tests

on:
push:
branches:
- trunk
- 'release/**'
- 'add/setup-e2e-tests'
paths:
- '.github/workflows/e2e-test.yml'
- 'plugins/auto-sizes/**'
- '**/package.json'
- 'package-lock.json'
- 'composer.json'
- 'composer.lock'
pull_request:
paths:
- '.github/workflows/e2e-test.yml'
- 'plugins/auto-sizes/**'
- '**/package.json'
- 'package-lock.json'
- 'composer.json'
- 'composer.lock'
types:
- opened
- reopened
- synchronize

jobs:
e2e-test:
name: E2E Tests
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js (.nvmrc)
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: npm

- name: Install npm dependencies
run: npm ci

- name: Build assets
run: npm run build

- name: Install Playwright dependencies
run: npx playwright install chromium --with-deps

- name: Install WordPress
run: npm run wp-env start

- name: Run tests
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
run: npm run test-e2e
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
############

plugins/*/tests/**/actual.html
artifacts
test-results

############
## IDEs
Expand Down
6 changes: 2 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
},
"devDependencies": {
"@octokit/rest": "^21.1.1",
"@playwright/test": "^1.51.1",
"@wordpress/e2e-test-utils-playwright": "^1.21.0",
"@wordpress/env": "^10.22.0",
"@wordpress/prettier-config": "^4.20.0",
"@wordpress/scripts": "^30.15.0",
Expand Down Expand Up @@ -51,6 +53,8 @@
"tsc": "tsc",
"format-php": "composer format:all",
"phpstan": "composer phpstan",
"test-e2e": "wp-scripts test-playwright --config plugins/auto-sizes/tests/e2e/playwright.config.ts",
"test-e2e:debug": "wp-scripts test-playwright --config plugins/auto-sizes/tests/e2e/playwright.config.ts --ui",
Comment on lines +56 to +57
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we ever want to have e2e tests in other plugins too? It would be weird to copy these config files everywhere, so I think it would be better to move them to tools/ or so. But then again see my other comments about their purpose.

"lint-php": "composer lint:all",
"test-php": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:plugins",
"test-php-watch": "./bin/test-php-watch.sh",
Expand Down
39 changes: 39 additions & 0 deletions plugins/auto-sizes/tests/e2e/config/global-setup.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wp-scripts already has a good global-setup config. Why do we need a custom one here?

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* External dependencies
*/
import { request } from '@playwright/test';
import type { FullConfig } from '@playwright/test';

/**
* WordPress dependencies
*/
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright';

async function globalSetup( config: FullConfig ) {
const { storageState, baseURL } = config.projects[ 0 ].use;
const storageStatePath =
typeof storageState === 'string' ? storageState : undefined;

const requestContext = await request.newContext( {
baseURL,
} );

const requestUtils = new RequestUtils( requestContext, {
storageStatePath,
} );

// Authenticate and save the storageState to disk.
await requestUtils.setupRest();

// Reset the test environment before running the tests.
await Promise.all( [
requestUtils.activateTheme( 'twentytwentyfour' ),
requestUtils.deleteAllPosts(),
requestUtils.deleteAllBlocks(),
requestUtils.resetPreferences(),
] );

await requestContext.dispose();
}

export default globalSetup;
51 changes: 51 additions & 0 deletions plugins/auto-sizes/tests/e2e/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* External dependencies
*/
import * as os from 'os';
import { fileURLToPath } from 'url';
import { defineConfig, devices } from '@playwright/test';

/**
* WordPress dependencies
*/
import baseConfig from '@wordpress/scripts/config/playwright.config.js';

const config = defineConfig( {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a custom config over what's provided by wp-scripts? IMO the default one should suffice for our needs.

...baseConfig,
workers: 1,
globalSetup: fileURLToPath(
new URL( './config/global-setup.ts', 'file:' + __filename ).href
),
projects: [
{
name: 'chromium',
use: { ...devices[ 'Desktop Chrome' ] },
grepInvert: /-chromium/,
},
{
name: 'webkit',
use: {
...devices[ 'Desktop Safari' ],
/**
* Headless webkit won't receive dataTransfer with custom types in the
* drop event on Linux. The solution is to use `xvfb-run` to run the tests.
* ```sh
* xvfb-run npm run test:e2e
* ```
* See `.github/workflows/end2end-test-playwright.yml` for advanced usages.
*/
headless: os.type() !== 'Linux',
},
grep: /@webkit/,
grepInvert: /-webkit/,
},
{
name: 'firefox',
use: { ...devices[ 'Desktop Firefox' ] },
grep: /@firefox/,
grepInvert: /-firefox/,
},
],
} );

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* External dependencies
*/
const path = require( 'path' );

/**
* WordPress dependencies
*/
const { test } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'check accurate sizes', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyfour' );
await requestUtils.deactivatePlugin( 'enhanced-responsive-images' );
await requestUtils.deleteAllMedia();
} );

test.afterEach( async ( { requestUtils } ) => {
await requestUtils.deleteAllMedia();
} );

test( 'should get smaller version of image', async ( {
admin,
editor,
requestUtils,
page,
} ) => {
const filename = 'leaves.jpg';
const filepath = path.join(
__dirname + '/../../data/images/',
filename
);

await admin.createNewPost();
const media = await requestUtils.uploadMedia( filepath );

await editor.insertBlock( {
name: 'core/group',
attributes: {
align: 'wide',
},
innerBlocks: [
{
name: 'core/image',
attributes: {
alt: filename,
id: media.id,
url: media.source_url,
},
},
],
} );

const postId = await editor.publishPost();

// Navigate to the post and wait for the image to load.
await page.goto( `/?p=${ postId }` );
const imageElement = await page.waitForSelector(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's typically better to use locators, for example getByAltText, or alternatively CSS locators. But not waitFor*.

`img.wp-image-${ media.id }`
);
const imageSizes = await imageElement.getAttribute( 'sizes' );

// Activate the plugin.
await requestUtils.activatePlugin( 'enhanced-responsive-images' );

// Reload the page and wait for the image to load.
await page.goto( `/?p=${ postId }` );
const updatedImageElement = await page.waitForSelector(
`img.wp-image-${ media.id }`
);

const updatedImageSizes =
await updatedImageElement.getAttribute( 'sizes' );

if ( imageSizes === updatedImageSizes ) {
throw new Error(
'Image sizes did not update after activating the plugin.'
);
}

if ( '(max-width: 620px) 100vw, 620px' !== updatedImageSizes ) {
throw new Error(
`Unexpected image sizes: ${ updatedImageSizes }. Expected: (max-width: 620px) 100vw, 620px`
);
}

const currentSrc = await updatedImageElement.evaluate( ( img ) =>
img instanceof HTMLImageElement ? img.currentSrc : null
);
currentSrc.endsWith( 'leaves-768x512.jpg' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't do anything, this isn't an assertion that fails the test.

Ideally we use something like await expect(locator).toHaveAttribute('src', /leaves-768x512\.jpeg$/ );

} );
} );
Loading