Skip to content

Commit b697b43

Browse files
committed
Adds Playwright, first tests and linting.
This runs in your local database and is noisy! I'd rather run it with a separate database but that might be overkill, I don't know.
1 parent 973f2a9 commit b697b43

File tree

8 files changed

+2286
-26
lines changed

8 files changed

+2286
-26
lines changed

.eslintrc.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
"camelcase": "off",
1010
"no-alert": "off",
1111
"vars-on-top": "warn"
12-
}
12+
},
13+
"ignorePatterns": [
14+
"e2e/*",
15+
"playwright.config.js"
16+
]
1317
}

.eslintrc.playwright.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"env": {
3+
"browser": true
4+
},
5+
"extends": [
6+
"plugin:@wordpress/eslint-plugin/esnext"
7+
]
8+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ alerts/js/*.min.js
2121

2222
# Generated test data
2323
tests/data/tmp/*
24+
/test-results/
25+
/playwright-report/
26+
/blob-report/
27+
/playwright/.cache/

.vscode/extensions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"recommendations": [
33
"EditorConfig.EditorConfig",
44
"felixfbecker.php-debug",
5-
"ikappas.phpcs"
5+
"ms-playwright.playwright"
66
]
77
}

e2e/editor-new-post.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { test, expect } from '@wordpress/e2e-test-utils-playwright';
2+
import { v4 as uuidv4 } from 'uuid';
3+
4+
test.describe( 'Editor: saving a new post', () => {
5+
let page, postTitle, postId;
6+
7+
test.beforeAll( async ( { browser } ) => {
8+
page = await browser.newPage();
9+
10+
const uuid = uuidv4();
11+
postTitle = `Test Post ${ uuid }`; // Sometimes this runs more than once within a microsecond so it's a UUID.
12+
13+
// eslint-disable-next-line no-console
14+
console.log( `New post ${ postTitle }` );
15+
16+
// Even though we're using WP's npm package, it's more straightforward to do it this way, at least for me in this environment.
17+
await page.goto( 'http://stream.wpenv.net/wp-login.php?redirect_to=http://stream.wpenv.net/wp-admin/post-new.php%2F&reauth=1' );
18+
await page.getByLabel( 'Username or Email Address' ).fill( 'admin' );
19+
await page.getByLabel( 'Password', { exact: true } ).fill( 'password' );
20+
await page.getByRole( 'button', { name: 'Log In' } ).click();
21+
await page.getByLabel( 'Add title' ).click();
22+
await page.getByLabel( 'Add title' ).fill( postTitle );
23+
await page.getByLabel( 'Add title' ).press( 'Tab' );
24+
await page.getByLabel( 'Empty block; start writing or' ).fill( 'I\'m a test post' );
25+
await page.getByRole( 'button', { name: 'Publish', exact: true } ).click();
26+
27+
postId = await page.locator( 'input#post_ID' ).inputValue();
28+
29+
// eslint-disable-next-line no-console
30+
console.log( `Post ID: ${ postId }` );
31+
32+
// We need to wait for both of the editor responses! The post saves to the posts table then the metadata saves to postmeta.
33+
await Promise.all( [
34+
page.waitForResponse( ( resp ) => resp.url().includes( 'meta-box-loader' ) && resp.status() === 302 ),
35+
page.waitForResponse( ( resp ) => resp.url().includes( `wp-json/wp/v2/posts/${ postId }` ) && resp.status() === 200 ),
36+
page.getByLabel( 'Editor publish' ).getByRole( 'button', { name: 'Publish', exact: true } ).click(),
37+
] );
38+
39+
// They are too much in the posts table so I'm deleting them.
40+
await page.goto( 'http://stream.wpenv.net/wp-admin/edit.php?post_type=post' );
41+
const listTable = page.getByRole( 'table', { name: 'Table ordered by' } );
42+
await expect( listTable ).toBeVisible();
43+
44+
// Move post to trash.
45+
await listTable.getByRole( 'link', { name: `“${ postTitle }” (Edit)` } ).hover();
46+
await listTable.getByRole( 'link', { name: `Move “${ postTitle }” to the Trash` } ).click();
47+
48+
// Ok, we're all set up, let's go to our page.
49+
await page.goto( 'http://stream.wpenv.net/wp-admin/admin.php?page=wp_stream' );
50+
} );
51+
52+
// Do we have a published row?
53+
test( 'has published row', async () => {
54+
// Expects Stream log to have "Test Post" post published visible.
55+
await expect( page.getByText( `"${ postTitle }" post published` ) ).toBeVisible();
56+
} );
57+
58+
// We should not have an updated row. This times out which makes it fail.
59+
test( 'does not have updated row', async () => {
60+
// Expects Stream log to have "Test Post" post published visible.
61+
await expect( page.getByText( `"${ postTitle }" post updated` ) ).not.toBeVisible();
62+
} );
63+
} );

0 commit comments

Comments
 (0)