From 24b54694cde697539ecde4af5f4fcd3bb6845625 Mon Sep 17 00:00:00 2001 From: Apple Date: Mon, 23 Dec 2024 14:53:04 +0530 Subject: [PATCH 1/3] add e2e test for preview existing post --- .../specs/posts/preview-existing-post.spec.js | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/e2e/specs/posts/preview-existing-post.spec.js diff --git a/tests/e2e/specs/posts/preview-existing-post.spec.js b/tests/e2e/specs/posts/preview-existing-post.spec.js new file mode 100644 index 0000000000000..d5181bf65fd33 --- /dev/null +++ b/tests/e2e/specs/posts/preview-existing-post.spec.js @@ -0,0 +1,66 @@ +/** + * WordPress dependencies + */ +const { test, expect } = require('@wordpress/e2e-test-utils-playwright'); + +test.describe('Preview Existing Post', () => { + test.beforeEach(async ({ admin, editor }, testInfo) => { + const title = 'preview post'; + testInfo.data = { title }; + await admin.createNewPost({ title }); + await editor.publishPost(); + }); + + test.afterEach(async ({ requestUtils }, testInfo) => { + const { title } = testInfo.data; + await requestUtils.deleteAllPosts(); + }); + +/** + * Selects a menu item radio by its name and verifies `aria-checked=true`. + * + * @param {Page} page - The Playwright page instance. + * @param {string} menuItem - The name of the menu item to select (e.g., 'Tablet', 'Mobile', 'Desktop'). + */ + async function selectMenuItemRadio(page, menuItem) { + const menuItemRadio = page.getByRole('menuitemradio', { name: menuItem }); + + // Click the menu item radio + await menuItemRadio.click(); + + // Assert that `aria-checked=true` after the click + await expect(menuItemRadio).toHaveAttribute('aria-checked', 'true'); + } + + test('Can Preview Existing Post', async ({ page, admin }, testInfo) => { + const { title } = testInfo.data; + + // Navigate to Posts list + await admin.visitAdminPage('/'); + await page.getByRole('link', { name: 'Posts', exact: true }).click(); + + //Click on the Test Post. + await page.locator(`text="${title}"`).first().click(); + + // Click Preview button + await page.getByRole('button', { name: 'View', exact: true }).click(); + + // Test for 'Tablet' + await selectMenuItemRadio(page, 'Tablet'); + + // Test for 'Mobile' + await selectMenuItemRadio(page, 'Mobile'); + + // Test for 'Desktop' + await selectMenuItemRadio(page, 'Desktop'); + + // Open Preview in a new tab + const [popup] = await Promise.all([ + page.waitForEvent('popup'), + page.locator('text="Preview in new tab"').click(), + ]); + await popup.waitForLoadState(); + await expect(popup).toHaveURL(/preview=true/); + await expect(popup.locator('body')).toContainText(title); + }); +}); From 9a4650018d4b682203daf04dc96016b9880cd6bd Mon Sep 17 00:00:00 2001 From: Apple Date: Mon, 23 Dec 2024 15:42:27 +0530 Subject: [PATCH 2/3] Update file name --- .../specs/posts/preview-existing-post.spec.js | 66 ------------------- 1 file changed, 66 deletions(-) delete mode 100644 tests/e2e/specs/posts/preview-existing-post.spec.js diff --git a/tests/e2e/specs/posts/preview-existing-post.spec.js b/tests/e2e/specs/posts/preview-existing-post.spec.js deleted file mode 100644 index d5181bf65fd33..0000000000000 --- a/tests/e2e/specs/posts/preview-existing-post.spec.js +++ /dev/null @@ -1,66 +0,0 @@ -/** - * WordPress dependencies - */ -const { test, expect } = require('@wordpress/e2e-test-utils-playwright'); - -test.describe('Preview Existing Post', () => { - test.beforeEach(async ({ admin, editor }, testInfo) => { - const title = 'preview post'; - testInfo.data = { title }; - await admin.createNewPost({ title }); - await editor.publishPost(); - }); - - test.afterEach(async ({ requestUtils }, testInfo) => { - const { title } = testInfo.data; - await requestUtils.deleteAllPosts(); - }); - -/** - * Selects a menu item radio by its name and verifies `aria-checked=true`. - * - * @param {Page} page - The Playwright page instance. - * @param {string} menuItem - The name of the menu item to select (e.g., 'Tablet', 'Mobile', 'Desktop'). - */ - async function selectMenuItemRadio(page, menuItem) { - const menuItemRadio = page.getByRole('menuitemradio', { name: menuItem }); - - // Click the menu item radio - await menuItemRadio.click(); - - // Assert that `aria-checked=true` after the click - await expect(menuItemRadio).toHaveAttribute('aria-checked', 'true'); - } - - test('Can Preview Existing Post', async ({ page, admin }, testInfo) => { - const { title } = testInfo.data; - - // Navigate to Posts list - await admin.visitAdminPage('/'); - await page.getByRole('link', { name: 'Posts', exact: true }).click(); - - //Click on the Test Post. - await page.locator(`text="${title}"`).first().click(); - - // Click Preview button - await page.getByRole('button', { name: 'View', exact: true }).click(); - - // Test for 'Tablet' - await selectMenuItemRadio(page, 'Tablet'); - - // Test for 'Mobile' - await selectMenuItemRadio(page, 'Mobile'); - - // Test for 'Desktop' - await selectMenuItemRadio(page, 'Desktop'); - - // Open Preview in a new tab - const [popup] = await Promise.all([ - page.waitForEvent('popup'), - page.locator('text="Preview in new tab"').click(), - ]); - await popup.waitForLoadState(); - await expect(popup).toHaveURL(/preview=true/); - await expect(popup.locator('body')).toContainText(title); - }); -}); From 8ae891014b3f14691c1d885a88391ff2509d4e3b Mon Sep 17 00:00:00 2001 From: Apple Date: Mon, 23 Dec 2024 15:43:16 +0530 Subject: [PATCH 3/3] Update file name --- .../specs/posts/preview-existing-post.test.js | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/e2e/specs/posts/preview-existing-post.test.js diff --git a/tests/e2e/specs/posts/preview-existing-post.test.js b/tests/e2e/specs/posts/preview-existing-post.test.js new file mode 100644 index 0000000000000..d5181bf65fd33 --- /dev/null +++ b/tests/e2e/specs/posts/preview-existing-post.test.js @@ -0,0 +1,66 @@ +/** + * WordPress dependencies + */ +const { test, expect } = require('@wordpress/e2e-test-utils-playwright'); + +test.describe('Preview Existing Post', () => { + test.beforeEach(async ({ admin, editor }, testInfo) => { + const title = 'preview post'; + testInfo.data = { title }; + await admin.createNewPost({ title }); + await editor.publishPost(); + }); + + test.afterEach(async ({ requestUtils }, testInfo) => { + const { title } = testInfo.data; + await requestUtils.deleteAllPosts(); + }); + +/** + * Selects a menu item radio by its name and verifies `aria-checked=true`. + * + * @param {Page} page - The Playwright page instance. + * @param {string} menuItem - The name of the menu item to select (e.g., 'Tablet', 'Mobile', 'Desktop'). + */ + async function selectMenuItemRadio(page, menuItem) { + const menuItemRadio = page.getByRole('menuitemradio', { name: menuItem }); + + // Click the menu item radio + await menuItemRadio.click(); + + // Assert that `aria-checked=true` after the click + await expect(menuItemRadio).toHaveAttribute('aria-checked', 'true'); + } + + test('Can Preview Existing Post', async ({ page, admin }, testInfo) => { + const { title } = testInfo.data; + + // Navigate to Posts list + await admin.visitAdminPage('/'); + await page.getByRole('link', { name: 'Posts', exact: true }).click(); + + //Click on the Test Post. + await page.locator(`text="${title}"`).first().click(); + + // Click Preview button + await page.getByRole('button', { name: 'View', exact: true }).click(); + + // Test for 'Tablet' + await selectMenuItemRadio(page, 'Tablet'); + + // Test for 'Mobile' + await selectMenuItemRadio(page, 'Mobile'); + + // Test for 'Desktop' + await selectMenuItemRadio(page, 'Desktop'); + + // Open Preview in a new tab + const [popup] = await Promise.all([ + page.waitForEvent('popup'), + page.locator('text="Preview in new tab"').click(), + ]); + await popup.waitForLoadState(); + await expect(popup).toHaveURL(/preview=true/); + await expect(popup.locator('body')).toContainText(title); + }); +});