From b83b3d33fe371d11b06839bdd86410ffe7ff49a8 Mon Sep 17 00:00:00 2001 From: Gustavo Lira Date: Tue, 24 Dec 2024 10:34:11 -0300 Subject: [PATCH 1/3] test Signed-off-by: Gustavo Lira --- .../e2e/catalog-scaffolded-from-link.spec.ts | 21 ++-- .../support/pages/catalog-import.ts | 49 +++++++++ e2e-tests/playwright/utils/ui-helper.ts | 101 ++++++++---------- 3 files changed, 103 insertions(+), 68 deletions(-) diff --git a/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts b/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts index e9cecc120b..3fbabe9a31 100644 --- a/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts +++ b/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts @@ -86,7 +86,7 @@ test.describe.serial("Link Scaffolded Templates to Catalog Items", () => { await uiHelper.openSidebar("Catalog"); await uiHelper.clickByDataTestId("user-picker-all"); await uiHelper.searchInputPlaceholder("scaffoldedfromlink-\n"); - await clickOnScaffoldedFromLink(); + await catalogImport.clickOnScaffoldedFromLink(); await uiHelper.clickTab("Dependencies"); @@ -95,17 +95,18 @@ test.describe.serial("Link Scaffolded Templates to Catalog Items", () => { const nodeSelector = 'g[data-testid="node"]'; // Selector for nodes // Verify text inside the 'label' selector - await uiHelper.verifyTextInSelector(labelSelector, "ownerOf"); - await uiHelper.verifyTextInSelector(labelSelector, "/ ownedBy"); - await uiHelper.verifyTextInSelector(labelSelector, "scaffoldedFrom"); + await catalogImport.verifyTextInSelector(labelSelector, "ownerOf"); + await catalogImport.verifyTextInSelector(labelSelector, "/ ownedBy"); + await catalogImport.verifyTextInSelector(labelSelector, "scaffoldedFrom"); // Verify text inside the 'node' selector - await uiHelper.verifyPartialTextInSelector( + await catalogImport.verifyTextInSelector( nodeSelector, reactAppDetails.componentPartialName, + false ); - await uiHelper.verifyTextInSelector( + await catalogImport.verifyTextInSelector( nodeSelector, "Create React App Template", ); @@ -145,12 +146,4 @@ test.describe.serial("Link Scaffolded Templates to Catalog Items", () => { await page.close(); }); - async function clickOnScaffoldedFromLink() { - const selector = - 'a[href*="/catalog/default/component/test-scaffoldedfromlink-"]'; - await page.locator(selector).first().waitFor({ state: "visible" }); - const link = await page.locator(selector).first(); - await expect(link).toBeVisible(); - await link.click(); - } }); diff --git a/e2e-tests/playwright/support/pages/catalog-import.ts b/e2e-tests/playwright/support/pages/catalog-import.ts index dbbffe5d76..2dde7011d7 100644 --- a/e2e-tests/playwright/support/pages/catalog-import.ts +++ b/e2e-tests/playwright/support/pages/catalog-import.ts @@ -44,6 +44,54 @@ export class CatalogImport { await expect(this.page.getByTestId("code-snippet")).toContainText(text); await this.uiHelper.clickButton("Close"); } + + async clickOnScaffoldedFromLink() { + const selector = + 'a[href*="/catalog/default/component/test-scaffoldedfromlink-"]'; + await this.page.locator(selector).first().waitFor({ state: "visible" }); + const link = this.page.locator(selector).first(); + await expect(link).toBeVisible(); + await link.click(); + } + + async verifyTextInSelector( + selector: string, + expectedText: string, + exactMatch: boolean = true, + ) { + const elements = this.page.locator(selector); + const count = await elements.count(); + + for (let i = 0; i < count; i++) { + const textContent = await elements.nth(i).textContent(); + + if (textContent) { + const isMatch = exactMatch + ? textContent.trim() === expectedText.trim() + : textContent.includes(expectedText); + + if (isMatch) { + expect(textContent).toContain(expectedText); // Playwright assertion + console.log( + `Text "${expectedText}" verified successfully in selector: ${selector}`, + ); + return; + } + } + } + + const allTextContent = await elements.allTextContents(); + console.error( + `Verification failed for text: Expected "${expectedText}". Selector content: ${allTextContent.join(", ")}`, + ); + throw new Error( + exactMatch + ? `Expected exact text "${expectedText}" not found in selector "${selector}".` + : `Expected partial text "${expectedText}" not found in selector "${selector}".`, + ); + } + + } export class BackstageShowcase { @@ -145,4 +193,5 @@ export class BackstageShowcase { await this.uiHelper.verifyRowsInTable([allPRs[i].title], false); } } + } diff --git a/e2e-tests/playwright/utils/ui-helper.ts b/e2e-tests/playwright/utils/ui-helper.ts index 2dba22b519..2dd5950407 100644 --- a/e2e-tests/playwright/utils/ui-helper.ts +++ b/e2e-tests/playwright/utils/ui-helper.ts @@ -209,60 +209,53 @@ export class UIhelper { await expect(elementLocator).toBeVisible(); } - async verifyTextInSelector(selector: string, expectedText: string) { - const elementLocator = this.page - .locator(selector) - .getByText(expectedText, { exact: true }); - - try { - await elementLocator.waitFor({ state: "visible", timeout: 10000 }); - const actualText = (await elementLocator.textContent()) || "No content"; - - if (actualText.trim() !== expectedText.trim()) { - console.error( - `Verification failed for text: Expected "${expectedText}", but got "${actualText}"`, - ); - throw new Error( - `Expected text "${expectedText}" not found. Actual content: "${actualText}".`, - ); - } - console.log( - `Text "${expectedText}" verified successfully in selector: ${selector}`, - ); - } catch (error) { - const allTextContent = await this.page - .locator(selector) - .allTextContents(); - console.error( - `Verification failed for text: Expected "${expectedText}". Selector content: ${allTextContent.join(", ")}`, - ); - throw error; - } - } - - async verifyPartialTextInSelector(selector: string, partialText: string) { - try { - const elements = await this.page.locator(selector); - const count = await elements.count(); - - for (let i = 0; i < count; i++) { - const textContent = await elements.nth(i).textContent(); - if (textContent && textContent.includes(partialText)) { - console.log( - `Found partial text: ${partialText} in element: ${textContent}`, - ); - return; - } - } - - throw new Error( - `Verification failed: Partial text "${partialText}" not found in any elements matching selector "${selector}".`, - ); - } catch (error) { - console.error(error.message); - throw error; - } - } + // async verifyTextInSelector(selector: string, expectedText: string) { + // const elementLocator = this.page + // .locator(selector) + // .getByText(expectedText, { exact: true }); + // + // try { + // await elementLocator.waitFor({ state: "visible", timeout: 10000 }); + // const actualText = (await elementLocator.textContent()) || "No content"; + // + // if (actualText.trim() !== expectedText.trim()) { + // console.error( + // `Verification failed for text: Expected "${expectedText}", but got "${actualText}"`, + // ); + // throw new Error( + // `Expected text "${expectedText}" not found. Actual content: "${actualText}".`, + // ); + // } + // console.log( + // `Text "${expectedText}" verified successfully in selector: ${selector}`, + // ); + // } catch (error) { + // const allTextContent = await this.page + // .locator(selector) + // .allTextContents(); + // console.error( + // `Verification failed for text: Expected "${expectedText}". Selector content: ${allTextContent.join(", ")}`, + // ); + // throw error; + // } + // } + // + // async verifyPartialTextInSelector(selector: string, partialText: string) { + // const elements = this.page.locator(selector); + // const count = await elements.count(); + // + // for (let i = 0; i < count; i++) { + // const textContent = await elements.nth(i).textContent(); + // if (textContent?.includes(partialText)) { + // expect(textContent).toContain(partialText); + // return; + // } + // } + // + // throw new Error( + // `Expected to find at least one element containing partial text "${partialText}" within selector "${selector}", but none were found.`, + // ); + // } async verifyColumnHeading( rowTexts: string[] | RegExp[], From 785cb27ae45d87428a342c464f7dc62c20655637 Mon Sep 17 00:00:00 2001 From: Gustavo Lira Date: Tue, 24 Dec 2024 11:32:18 -0300 Subject: [PATCH 2/3] test Signed-off-by: Gustavo Lira --- e2e-tests/playwright/support/pages/catalog-import.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/e2e-tests/playwright/support/pages/catalog-import.ts b/e2e-tests/playwright/support/pages/catalog-import.ts index 2dde7011d7..6a1f3db8a7 100644 --- a/e2e-tests/playwright/support/pages/catalog-import.ts +++ b/e2e-tests/playwright/support/pages/catalog-import.ts @@ -63,7 +63,11 @@ export class CatalogImport { const count = await elements.count(); for (let i = 0; i < count; i++) { - const textContent = await elements.nth(i).textContent(); + const element = elements.nth(i); + + await element.waitFor({ state: 'visible', timeout: 10000 }); + + const textContent = await element.textContent(); if (textContent) { const isMatch = exactMatch @@ -80,6 +84,7 @@ export class CatalogImport { } } + // If no matching element was found, it throws an error directly const allTextContent = await elements.allTextContents(); console.error( `Verification failed for text: Expected "${expectedText}". Selector content: ${allTextContent.join(", ")}`, @@ -91,7 +96,6 @@ export class CatalogImport { ); } - } export class BackstageShowcase { From a84e228c32cc497a492361b87ab3c9135a1bb9ca Mon Sep 17 00:00:00 2001 From: Gustavo Lira Date: Tue, 24 Dec 2024 11:54:48 -0300 Subject: [PATCH 3/3] test Signed-off-by: Gustavo Lira --- e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts b/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts index 3fbabe9a31..6380bed004 100644 --- a/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts +++ b/e2e-tests/playwright/e2e/catalog-scaffolded-from-link.spec.ts @@ -94,6 +94,8 @@ test.describe.serial("Link Scaffolded Templates to Catalog Items", () => { const labelSelector = 'g[data-testid="label"]'; // Selector for labels const nodeSelector = 'g[data-testid="node"]'; // Selector for nodes + await page.waitForSelector(labelSelector); + await page.waitForSelector(nodeSelector); // Verify text inside the 'label' selector await catalogImport.verifyTextInSelector(labelSelector, "ownerOf"); await catalogImport.verifyTextInSelector(labelSelector, "/ ownedBy");