Skip to content
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

Add infrastructure for writing Playwright E2E tests #38570

Merged
merged 6 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,39 @@ module.exports = {
},
},
{
files: [ 'packages/jest*/**/*.js' ],
files: [ 'packages/jest*/**/*.js', '**/test/**/*.js' ],
excludedFiles: [ 'test/e2e/**/*.js' ],
extends: [ 'plugin:@wordpress/eslint-plugin/test-unit' ],
},
{
files: [ 'packages/e2e-test*/**/*.js' ],
files: [
'packages/e2e-tests/**/*.js',
'packages/e2e-test-utils/**/*.js',
],
extends: [ 'plugin:@wordpress/eslint-plugin/test-e2e' ],
},
{
files: [
'test/e2e/**/*.[tj]s',
'packages/e2e-test-utils-playwright/**/*.[tj]s',
],
extends: [ 'plugin:@wordpress/eslint-plugin/test-e2e-playwright' ],
rules: {
'jest/expect-expect': 'off',
'@wordpress/no-global-active-element': 'off',
'@wordpress/no-global-get-selection': 'off',
'no-restricted-syntax': [
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
'error',
{
selector: 'CallExpression[callee.name="$"]',
message:
'`$` is discouraged, please use `locator` instead',
},
{
selector: 'CallExpression[callee.name="$$"]',
message:
'`$$` is discouraged, please use `locator` instead',
},
],
},
},
{
Expand Down
60 changes: 46 additions & 14 deletions .github/report-flaky-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const metaData = {

for ( const flakyTest of flakyTests ) {
const {
runner: testRunner = 'jest-circus',
title: testTitle,
path: testPath,
results: testResults,
Expand Down Expand Up @@ -92,7 +93,11 @@ const metaData = {
body.indexOf( TEST_RESULTS_LIST.close )
) +
[
renderTestErrorMessage( { testPath, testResults } ),
renderTestErrorMessage( {
testRunner,
testPath,
testResults,
} ),
TEST_RESULTS_LIST.close,
].join( '\n' );

Expand Down Expand Up @@ -121,6 +126,7 @@ const metaData = {
title: issueTitle,
body: renderIssueBody( {
meta,
testRunner,
testTitle,
testPath,
testResults,
Expand Down Expand Up @@ -187,10 +193,16 @@ function getIssueTitle( testTitle ) {
return `[Flaky Test] ${ testTitle }`;
}

function renderIssueBody( { meta, testTitle, testPath, testResults } ) {
function renderIssueBody( {
meta,
testRunner,
testTitle,
testPath,
testResults,
} ) {
return (
renderIssueDescription( { meta, testTitle, testPath } ) +
renderTestResults( { testPath, testResults } )
renderTestResults( { testRunner, testPath, testResults } )
);
}

Expand All @@ -211,14 +223,41 @@ ${ testTitle }
`;
}

function renderTestResults( { testPath, testResults } ) {
function renderTestResults( { testRunner, testPath, testResults } ) {
return `${ TEST_RESULTS_LIST.open }
${ renderTestErrorMessage( { testPath, testResults } ) }
${ renderTestErrorMessage( { testRunner, testPath, testResults } ) }
${ TEST_RESULTS_LIST.close }
`;
}

function renderTestErrorMessage( { testPath, testResults } ) {
function renderTestResults( { testRunner, testResults, testPath } ) {
switch ( testRunner ) {
case '@playwright/test': {
// Could do a slightly better formatting than this.
return stripAnsi(
testResults.map( ( result ) => result.error.stack ).join( '\n' )
);
}
case 'jest-circus':
default: {
return stripAnsi(
formatResultsErrors(
testResults,
{
rootDir: path.join(
process.cwd(),
'packages/e2e-tests'
),
},
{},
testPath
)
);
}
}
}

function renderTestErrorMessage( { testRunner, testPath, testResults } ) {
const date = new Date().toISOString();
// It will look something like this without formatting:
// ▶ [2021-08-31T16:15:19.875Z] Test passed after 2 failed attempts on trunk
Expand All @@ -231,14 +270,7 @@ function renderTestErrorMessage( { testPath, testResults } ) {
</summary>

\`\`\`
${ stripAnsi(
formatResultsErrors(
testResults,
{ rootDir: path.join( process.cwd(), 'packages/e2e-tests' ) },
{},
testPath
)
) }
${ renderTestResults( { testRunner, testPath, testResults } ) }
\`\`\`
</details>${ TEST_RESULT.close }`;
}
Expand Down
68 changes: 68 additions & 0 deletions .github/workflows/end2end-test-playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: End-to-End Tests Playwright

on:
pull_request:
push:
branches:
- trunk
- 'release/**'
- 'wp/**'

# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
e2e:
name: E2E Tests
runs-on: ubuntu-latest
if: ${{ github.repository == 'WordPress/gutenberg' || github.event_name == 'pull_request' }}
strategy:
fail-fast: false
matrix:
node: ['14']
Copy link
Member Author

Choose a reason for hiding this comment

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

We could add matrix here in the future when test suites grow and take advantage of the sharding feature.


steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # v2.3.4

- name: Use desired version of NodeJS
uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f # v2.2.2
with:
node-version: ${{ matrix.node }}
cache: npm

- name: Npm install and build
run: |
npm ci
npm run build

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

- name: Install WordPress and start the server
run: |
npm run wp-env start

- name: Run the tests
run: |
npm run test-e2e:playwright

- name: Archive debug artifacts (screenshots, traces)
uses: actions/upload-artifact@e448a9b857ee2131e752b06002bf0e093c65e571 # v2.2.2
if: always()
with:
name: failures-artifacts
path: artifacts
if-no-files-found: ignore

- name: Archive flaky tests report
uses: actions/upload-artifact@e448a9b857ee2131e752b06002bf0e093c65e571 # v2.2.2
if: always()
with:
name: flaky-tests-report
path: flaky-tests
if-no-files-found: ignore
2 changes: 2 additions & 0 deletions .github/workflows/flaky-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Report Flaky Tests

on:
workflow_run:
# We should also add 'End-to-End Tests Playwright' here but that
# wil run this workflow whenever either one of them completes.
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be a neat exercise to migrate some of our flakiest tests to Playwright and run the flaky report on both, to see if there's any difference 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a great idea! We can migrate those flaky tests first.

workflows: ['End-to-End Tests']
types:
- completed
Expand Down
1 change: 1 addition & 0 deletions bin/packages/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ if ( files.length ) {
`**/benchmark/**`,
`**/{__mocks__,__tests__,test}/**`,
`**/{storybook,stories}/**`,
`**/e2e-test-utils-playwright/**`,
],
onlyFiles: true,
}
Expand Down
2 changes: 1 addition & 1 deletion bin/packages/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function isSourceFile( filename ) {
return (
/\/src\/.+\.(js|json|scss|ts|tsx)$/.test( relativePath ) &&
! [
/\/(benchmark|__mocks__|__tests__|test|storybook|stories)\/.+/,
/\/(benchmark|__mocks__|__tests__|test|storybook|stories|e2e-test-utils-playwright)\/.+/,
/.\.(spec|test)\.js$/,
].some( ( regex ) => regex.test( relativePath ) )
);
Expand Down
4 changes: 3 additions & 1 deletion docs/contributors/code/testing-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ There is an ongoing effort to add integration tests to the native mobile project

## End-to-end Testing

End-to-end tests use [Puppeteer](https://github.com/puppeteer/puppeteer) as a headless Chromium driver, and are otherwise still run by a [Jest](https://jestjs.io/) test runner.
End-to-end tests currently use [Puppeteer](https://github.com/puppeteer/puppeteer) as a headless Chromium driver to run the tests in `packages/e2e-tests`, and are otherwise still run by a [Jest](https://jestjs.io/) test runner.

> There's a ongoing [project](https://github.com/WordPress/gutenberg/issues/38851) to migrate them from Puppeteer to Playwright. See the [README](https://github.com/WordPress/gutenberg/tree/HEAD/test/e2e/README.md) of the new E2E tests for the updated guideline and best practices.

### Using wp-env

Expand Down
6 changes: 0 additions & 6 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1505,12 +1505,6 @@
"markdown_source": "../packages/e2e-test-utils/README.md",
"parent": "packages"
},
{
"title": "@wordpress/e2e-tests",
"slug": "packages-e2e-tests",
"markdown_source": "../packages/e2e-tests/README.md",
"parent": "packages"
},
{
"title": "@wordpress/edit-post",
"slug": "packages-edit-post",
Expand Down
Loading