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 e2e test for preview existing post #8039

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions tests/e2e/specs/posts/preview-existing-post.test.js
Original file line number Diff line number Diff line change
@@ -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 });

Check failure on line 10 in tests/e2e/specs/posts/preview-existing-post.test.js

View workflow job for this annotation

GitHub Actions / Test with SCRIPT_DEBUG enabled / Run E2E tests

[chromium] › posts/preview-existing-post.test.js:35:3 › Preview Existing Post › Can Preview Existing Post

2) [chromium] › posts/preview-existing-post.test.js:35:3 › Preview Existing Post › Can Preview Existing Post Error: Not logged in 8 | const title = 'preview post'; 9 | testInfo.data = { title }; > 10 | await admin.createNewPost({ title }); | ^ 11 | await editor.publishPost(); 12 | }); 13 | at Admin.visitAdminPage (/home/runner/work/wordpress-develop/wordpress-develop/node_modules/@wordpress/e2e-test-utils-playwright/src/admin/visit-admin-page.ts:36:9) at Admin.createNewPost (/home/runner/work/wordpress-develop/wordpress-develop/node_modules/@wordpress/e2e-test-utils-playwright/src/admin/create-new-post.ts:41:2) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/posts/preview-existing-post.test.js:10:5

Check failure on line 10 in tests/e2e/specs/posts/preview-existing-post.test.js

View workflow job for this annotation

GitHub Actions / Test with SCRIPT_DEBUG disabled / Run E2E tests

[chromium] › posts/preview-existing-post.test.js:35:3 › Preview Existing Post › Can Preview Existing Post

2) [chromium] › posts/preview-existing-post.test.js:35:3 › Preview Existing Post › Can Preview Existing Post Error: Not logged in 8 | const title = 'preview post'; 9 | testInfo.data = { title }; > 10 | await admin.createNewPost({ title }); | ^ 11 | await editor.publishPost(); 12 | }); 13 | at Admin.visitAdminPage (/home/runner/work/wordpress-develop/wordpress-develop/node_modules/@wordpress/e2e-test-utils-playwright/src/admin/visit-admin-page.ts:36:9) at Admin.createNewPost (/home/runner/work/wordpress-develop/wordpress-develop/node_modules/@wordpress/e2e-test-utils-playwright/src/admin/create-new-post.ts:41:2) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/posts/preview-existing-post.test.js:10:5

Check failure on line 10 in tests/e2e/specs/posts/preview-existing-post.test.js

View workflow job for this annotation

GitHub Actions / Test with SCRIPT_DEBUG disabled / Run E2E tests

[chromium] › posts/preview-existing-post.test.js:35:3 › Preview Existing Post › Can Preview Existing Post

2) [chromium] › posts/preview-existing-post.test.js:35:3 › Preview Existing Post › Can Preview Existing Post Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Not logged in 8 | const title = 'preview post'; 9 | testInfo.data = { title }; > 10 | await admin.createNewPost({ title }); | ^ 11 | await editor.publishPost(); 12 | }); 13 | at Admin.visitAdminPage (/home/runner/work/wordpress-develop/wordpress-develop/node_modules/@wordpress/e2e-test-utils-playwright/src/admin/visit-admin-page.ts:36:9) at Admin.createNewPost (/home/runner/work/wordpress-develop/wordpress-develop/node_modules/@wordpress/e2e-test-utils-playwright/src/admin/create-new-post.ts:41:2) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/posts/preview-existing-post.test.js:10:5
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);
});
});
Loading