From fd4c3d1e53dd2621af179e9bb41b1dac4de00959 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Thu, 14 Sep 2023 14:28:05 +0700 Subject: [PATCH] Fixed flaky announcement test for Admin X (#18130) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue - Attempts to resolve a flaky test. --- ### 🤖 Generated by Copilot at 1a7d420 Refactor announcement bar preview test to use a helper function for checking text in iframes. This improves code quality and readability. --- .../test/e2e/site/announcementbar.test.ts | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/apps/admin-x-settings/test/e2e/site/announcementbar.test.ts b/apps/admin-x-settings/test/e2e/site/announcementbar.test.ts index f8d756b2e3..348345afd7 100644 --- a/apps/admin-x-settings/test/e2e/site/announcementbar.test.ts +++ b/apps/admin-x-settings/test/e2e/site/announcementbar.test.ts @@ -24,41 +24,35 @@ test.describe('Announcement Bar', async () => { await section.getByRole('button', {name: 'Customize'}).click(); - // // Homepage and post preview await page.waitForSelector('[data-testid="announcement-bar-preview-iframe"]'); - const iframeCount = await page.$$eval('[data-testid="announcement-bar-preview-iframe"] > iframe', frames => frames.length); - expect(iframeCount).toEqual(2); + const checkTextInIframes = async (iframesHandles, textToSearch) => { + let textExists = false; + for (const iframeHandle of iframesHandles) { + const frame = await iframeHandle.contentFrame(); + const textFound = await frame?.$eval('body', (body, text) => { + return body.innerText.includes(text); + }, textToSearch); + if (textFound) { + textExists = true; + break; + } + } + return textExists; + }; + + const iframesHandleHome = await page.$$('[data-testid="announcement-bar-preview-iframe"] > iframe'); + const textExistsInHomeIframes = await checkTextInIframes(iframesHandleHome, 'homepage preview'); + expect(textExistsInHomeIframes).toBeTruthy(); const modal = page.getByTestId('announcement-bar-modal'); - - await page.waitForSelector('[data-testid="announcement-bar-preview-iframe"]'); - - // Get the iframes inside the modal - const iframesHandleHome = await page.$$('[data-testid="announcement-bar-preview-iframe"] > iframe'); - - const homeiframeHandler = iframesHandleHome[1]; // index 1 since that's the visible iframe - const frameHome = await homeiframeHandler.contentFrame(); - const textExistsInFirstIframe = await frameHome?.$eval('body', (body, textToSearch) => { - return body.innerText.includes(textToSearch); - }, 'homepage preview'); - - expect(textExistsInFirstIframe).toBeTruthy(); - await modal.getByTestId('design-toolbar').getByRole('tab', {name: 'Post'}).click(); await page.waitForSelector('[data-testid="announcement-bar-preview-iframe"]'); const iframesHandlePost = await page.$$('[data-testid="announcement-bar-preview-iframe"] > iframe'); - - const postiframeHandler = iframesHandlePost[0]; // index 0 since that's the visible iframe - const framePost = await postiframeHandler.contentFrame(); - - const textExistsInSecondIframe = await framePost?.$eval('body', (body, textToSearch) => { - return body.innerText.includes(textToSearch); - }, 'post preview'); - - expect(textExistsInSecondIframe).toBeTruthy(); + const textExistsInPostIframes = await checkTextInIframes(iframesHandlePost, 'post preview'); + expect(textExistsInPostIframes).toBeTruthy(); }); // TODO - lexical isn't loading in the preview