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

Migrate 'taxonomies' e2e tests to Playwright #57662

Merged
merged 2 commits into from
Jan 9, 2024
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
251 changes: 0 additions & 251 deletions packages/e2e-tests/specs/editor/various/taxonomies.test.js

This file was deleted.

136 changes: 136 additions & 0 deletions test/e2e/specs/editor/various/taxonomies.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

function generateRandomNumber() {
return Math.round( 1 + Math.random() * ( Number.MAX_SAFE_INTEGER - 1 ) );
}

test.describe( 'Taxonomies', () => {
test.beforeEach( async ( { admin, editor } ) => {
await admin.createNewPost();
await editor.openDocumentSettingsSidebar();
} );

test( 'should be able to open the categories panel and create a new main category', async ( {
editor,
page,
} ) => {
// Open the Document -> Categories panel.
const panelToggle = page.getByRole( 'button', {
name: 'Categories',
} );

if (
( await panelToggle.getAttribute( 'aria-expanded' ) ) === 'false'
) {
await panelToggle.click();
}

await page
.getByRole( 'button', {
name: 'Add New Category',
expanded: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

This line seems unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

The panel has two Add New Category buttons; the first expands the form, and the second submits. I added the expanded check as a safeguard and to highlight the intention.

Screenshot

CleanShot 2024-01-09 at 12 08 23

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I missed that 😅 If so, there is no problem.

} )
.click();
await page
.getByRole( 'textbox', { name: 'New Category Name' } )
.fill( 'z rand category 1' );
await page.keyboard.press( 'Enter' );

const categories = page.getByRole( 'group', { name: 'Categories' } );
const selectedCategories = categories.getByRole( 'checkbox', {
checked: true,
} );
const newCategory = categories.getByRole( 'checkbox', {
name: 'z rand category 1',
} );

await expect( selectedCategories ).toHaveCount( 1 );
await expect( newCategory ).toBeChecked();

await editor.canvas
.getByRole( 'textbox', { name: 'Add title' } )
.fill( 'Hello World' );
await editor.publishPost();
await page.reload();

// The category selection was persisted after the publish process.
await expect( selectedCategories ).toHaveCount( 1 );
await expect( newCategory ).toBeChecked();
} );

test( 'should be able to open the tags panel and create a new tag', async ( {
editor,
page,
} ) => {
// Open the Document -> Tags panel.
const panelToggle = page.getByRole( 'button', {
name: 'Tags',
} );

if (
( await panelToggle.getAttribute( 'aria-expanded' ) ) === 'false'
) {
await panelToggle.click();
}

const tagName = 'tag-' + generateRandomNumber();
const tags = page.locator( '.components-form-token-field__token-text' );
Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't find a better locator here. The screen readers use the Remove Tag button and its description, but PW currently can't locate elements based on descriptions.

Some workarounds are described in microsoft/playwright#18332, but decided to go with the simplest solution.


await page
.getByRole( 'combobox', { name: 'Add New Tag' } )
.fill( tagName );
await page.keyboard.press( 'Enter' );

await expect( tags ).toHaveCount( 1 );
await expect( tags ).toContainText( tagName );

await editor.canvas
.getByRole( 'textbox', { name: 'Add title' } )
.fill( 'Hello World' );
await editor.publishPost();
await page.reload();

await expect( tags ).toHaveCount( 1 );
await expect( tags ).toContainText( tagName );
} );

// See: https://github.com/WordPress/gutenberg/pull/21693.
test( `should be able to create a new tag with ' on the name`, async ( {
editor,
page,
} ) => {
// Open the Document -> Tags panel.
const panelToggle = page.getByRole( 'button', {
name: 'Tags',
} );

if (
( await panelToggle.getAttribute( 'aria-expanded' ) ) === 'false'
) {
await panelToggle.click();
}

const tagName = "tag'-" + generateRandomNumber();
const tags = page.locator( '.components-form-token-field__token-text' );

await page
.getByRole( 'combobox', { name: 'Add New Tag' } )
.fill( tagName );
await page.keyboard.press( 'Enter' );

await expect( tags ).toHaveCount( 1 );
await expect( tags ).toContainText( tagName );

await editor.canvas
.getByRole( 'textbox', { name: 'Add title' } )
.fill( 'Hello World' );
await editor.publishPost();
await page.reload();

await expect( tags ).toHaveCount( 1 );
await expect( tags ).toContainText( tagName );
} );
} );
Loading