Skip to content

Commit

Permalink
Playwright Utils: Introduce the 'editor.saveDraft' helper (#55308)
Browse files Browse the repository at this point in the history
* Playwright Utils: Introduce the 'editor.saveDraft' helper
* Update e2e tests where possible
  • Loading branch information
Mamaduka authored Oct 13, 2023
1 parent ef21f20 commit 24add08
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 73 deletions.
3 changes: 3 additions & 0 deletions packages/e2e-test-utils-playwright/src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { insertBlock } from './insert-block';
import { openDocumentSettingsSidebar } from './open-document-settings-sidebar';
import { openPreviewPage } from './preview';
import { publishPost } from './publish-post';
import { saveDraft } from './save-draft';
import { selectBlocks } from './select-blocks';
import { setContent } from './set-content';
import { showBlockToolbar } from './show-block-toolbar';
Expand Down Expand Up @@ -66,6 +67,8 @@ export class Editor {
openPreviewPage: typeof openPreviewPage = openPreviewPage.bind( this );
/** @borrows publishPost as this.publishPost */
publishPost: typeof publishPost = publishPost.bind( this );
/** @borrows saveDraft as this.saveDraft */
saveDraft: typeof saveDraft = saveDraft.bind( this );
/** @borrows saveSiteEditorEntities as this.saveSiteEditorEntities */
saveSiteEditorEntities: typeof saveSiteEditorEntities =
saveSiteEditorEntities.bind( this );
Expand Down
20 changes: 20 additions & 0 deletions packages/e2e-test-utils-playwright/src/editor/save-draft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Internal dependencies
*/
import type { Editor } from './index';

/**
* Saves the post as a draft, resolving once the request is complete (once a notice
* is displayed).
*/
export async function saveDraft( this: Editor ) {
await this.page
.getByRole( 'region', { name: 'Editor top bar' } )
.getByRole( 'button', { name: 'Save draft' } )
.click();

await this.page
.getByRole( 'button', { name: 'Dismiss this notice' } )
.filter( { hasText: 'Draft saved' } )
.waitFor();
}
7 changes: 1 addition & 6 deletions test/e2e/specs/editor/blocks/classic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ test.describe( 'Classic', () => {
// Move focus away.
await pageUtils.pressKeys( 'shift+Tab' );

await page.click( 'role=button[name="Save draft"i]' );

await expect(
page.locator( 'role=button[name="Saved"i]' )
).toBeDisabled();

await editor.saveDraft();
await page.reload();
await page.unroute( '**' );

Expand Down
7 changes: 1 addition & 6 deletions test/e2e/specs/editor/blocks/post-title.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ test.describe( 'Post Title block', () => {
.fill( 'Just tweaking the post title' );

// Save the post draft and reload.
await page.getByRole( 'button', { name: 'Save draft' } ).click();
await expect(
page
.getByRole( 'button', { name: 'Dismiss this notice' } )
.filter( { hasText: 'Draft saved' } )
).toBeVisible();
await editor.saveDraft();
await page.reload();

const titleBlock = editor.canvas.getByRole( 'document', {
Expand Down
5 changes: 1 addition & 4 deletions test/e2e/specs/editor/plugins/nonce.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ test.describe( 'Nonce', () => {
}
} );

await page.click( 'role=button[name=/Save draft/i]' );
// Saving draft should still succeed after retrying.
await expect(
page.locator( 'role=button[name="Dismiss this notice"i]' )
).toContainText( /Draft saved/i );
await editor.saveDraft();

// We expect a 403 status only once.
expect( saveDraftResponses ).toEqual( [ 403, 200 ] );
Expand Down
21 changes: 3 additions & 18 deletions test/e2e/specs/editor/plugins/post-type-templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ test.describe( 'Post type templates', () => {
.focus();
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'Backspace' );
await page.click( 'role=button[name="Save draft"i]' );
await expect(
page.locator(
'role=button[name="Dismiss this notice"i] >> text=Draft saved'
)
).toBeVisible();
await editor.saveDraft();
await page.reload();

const expectedContent = await page.evaluate( ( content ) => {
Expand All @@ -72,12 +67,7 @@ test.describe( 'Post type templates', () => {
await page.keyboard.press( 'ArrowDown' );
await pageUtils.pressKeys( 'primary+A' );
await page.keyboard.press( 'Backspace' );
await page.click( 'role=button[name="Save draft"i]' );
await expect(
page.locator(
'role=button[name="Dismiss this notice"i] >> text=Draft saved'
)
).toBeVisible();
await editor.saveDraft();
await page.reload();

await expect.poll( editor.getEditedPostContent ).toBe( '' );
Expand Down Expand Up @@ -133,12 +123,7 @@ test.describe( 'Post type templates', () => {
.locator( 'role=document[name="Block: Image"i]' )
.focus();
await page.keyboard.press( 'Backspace' );
await page.click( 'role=button[name="Save draft"i]' );
await expect(
page.locator(
'role=button[name="Dismiss this notice"i] >> text=Draft saved'
)
).toBeVisible();
await editor.saveDraft();
await page.reload();

await expect.poll( editor.getEditedPostContent ).toBe( '' );
Expand Down
8 changes: 1 addition & 7 deletions test/e2e/specs/editor/various/multi-block-selection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,7 @@ test.describe( 'Multi-block selection', () => {
attributes: { content: 'test' },
} );

await page.getByRole( 'button', { name: 'Save draft' } ).click();
await expect(
page
.getByRole( 'button', { name: 'Dismiss this notice' } )
.filter( { hasText: 'Draft saved' } )
).toBeVisible();

await editor.saveDraft();
await page.reload();
// To do: run with iframe.
await editor.switchToLegacyCanvas();
Expand Down
5 changes: 1 addition & 4 deletions test/e2e/specs/editor/various/new-post.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ test.describe( 'new editor state', () => {
.locator( 'role=textbox[name="Add title"i]' )
.type( 'Here is the title' );
// Save the post as a draft.
await page.click( 'role=button[name="Save draft"i]' );
await page.waitForSelector(
'role=button[name="Dismiss this notice"] >> text=Draft saved'
);
await editor.saveDraft();

// Reload the browser so a post is loaded with a title.
await page.reload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ class PostEditorTemplateMode {
// Save the post
// Saving shouldn't be necessary but unfortunately,
// there's a template resolution bug forcing us to do so.
await this.page.click( 'role=button[name="Save draft"i]' );
await this.page.waitForSelector(
'role=button[name="Dismiss this notice"] >> text=Draft saved'
);
await this.editor.saveDraft();
}

async createNewTemplate( templateName ) {
Expand Down
28 changes: 4 additions & 24 deletions test/e2e/specs/editor/various/undo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,7 @@ test.describe( 'undo', () => {
.locator( 'role=button[name="Add default block"i]' )
.click();
await page.keyboard.type( 'test' );
await page.click( 'role=button[name="Save draft"i]' );
await expect(
page.locator(
'role=button[name="Dismiss this notice"i] >> text=Draft saved'
)
).toBeVisible();
await editor.saveDraft();
await page.reload();
await editor.canvas.locator( '[data-type="core/paragraph"]' ).click();
await pageUtils.pressKeys( 'primary+a' );
Expand Down Expand Up @@ -344,12 +339,7 @@ test.describe( 'undo', () => {
.locator( 'role=button[name="Add default block"i]' )
.click();
await page.keyboard.type( 'original' );
await page.click( 'role=button[name="Save draft"i]' );
await expect(
page.locator(
'role=button[name="Dismiss this notice"i] >> text=Draft saved'
)
).toBeVisible();
await editor.saveDraft();
await page.reload();

// Issue is demonstrated by forcing state merges (multiple inputs) on
Expand Down Expand Up @@ -384,12 +374,7 @@ test.describe( 'undo', () => {
.locator( 'role=button[name="Add default block"i]' )
.click();
await page.keyboard.type( '1' );
await page.click( 'role=button[name="Save draft"i]' );
await expect(
page.locator(
'role=button[name="Dismiss this notice"i] >> text=Draft saved'
)
).toBeVisible();
await editor.saveDraft();
await pageUtils.pressKeys( 'primary+z' );

await expect.poll( editor.getEditedPostContent ).toBe( '' );
Expand Down Expand Up @@ -420,12 +405,7 @@ test.describe( 'undo', () => {
.click();

await page.keyboard.type( '1' );
await page.click( 'role=button[name="Save draft"i]' );
await expect(
page.locator(
'role=button[name="Dismiss this notice"i] >> text=Draft saved'
)
).toBeVisible();
await editor.saveDraft();
await page.reload();

// Expect undo button to be disabled.
Expand Down

1 comment on commit 24add08

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 24add08.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6506603459
📝 Reported issues:

Please sign in to comment.