From e994e1d0099154c61c6f22f04366caf010321408 Mon Sep 17 00:00:00 2001 From: TBardini Date: Fri, 17 Jan 2025 17:35:39 -0500 Subject: [PATCH] feat: refactor test files for improved readability and maintainability --- dist/playwright.config.js | 93 +++++++++++++++++++ .../dakota/desktop/test1.e2e.desktop.spec.js | 6 +- .../e2e/MiniMarket.e2e.desktop.spec.js | 6 +- dist/tests/utils/customReport.js | 12 ++- dist/tests/utils/generalTestUtils.js | 9 +- dist/tests/utils/screenshotTests.js | 28 ++++-- dist/tests/utils/types.js | 1 - 7 files changed, 130 insertions(+), 25 deletions(-) create mode 100644 dist/playwright.config.js diff --git a/dist/playwright.config.js b/dist/playwright.config.js new file mode 100644 index 0000000..bd9f8e8 --- /dev/null +++ b/dist/playwright.config.js @@ -0,0 +1,93 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const test_1 = require("@playwright/test"); +/** + * Read environment variables from file. + * https://github.com/motdotla/dotenv + */ +// require('dotenv').config(); +/** + * See https://playwright.dev/docs/test-configuration. + */ +exports.default = (0, test_1.defineConfig)({ + testDir: "./tests", + /* Run tests in files in parallel */ + fullyParallel: true, + /* Fail the build on CI if you accidentally left test.only in the source code. */ + forbidOnly: !!process.env.CI, + /* Retry on CI only */ + retries: process.env.CI ? 2 : 0, + /* Opt out of parallel tests on CI. */ + workers: process.env.CI ? 1 : undefined, + /* Reporter to use. See https://playwright.dev/docs/test-reporters */ + reporter: [ + ["html"], + ["json", { outputFile: "./test-results/test-results.json" }], // Built-in json reporter + ], + /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ + use: { + // video: { + // mode: "on", + // size: { width: 640, height: 480 } + // }, + /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ + trace: "on-first-retry", + }, + /* Configure projects for major browsers */ + projects: [ + { + name: "chromium", + use: { ...test_1.devices["Desktop Chrome"] }, + testMatch: "**/*.desktop.spec.ts", // Only run desktop tests + snapshotDir: `__screenshots__/`, // Save screenshots to this directory + }, + { + name: "firefox", + use: { ...test_1.devices["Desktop Firefox"] }, + testMatch: "**/*.desktop.spec.ts", // Only run desktop tests + snapshotDir: `__screenshots__/`, // Save screenshots to this directory + }, + { + name: "webkit", + use: { ...test_1.devices["Desktop Safari"] }, + testMatch: "**/*.desktop.spec.ts", // Only run desktop tests + snapshotDir: `__screenshots__/`, // Save screenshots to this directory + }, + { + name: "IOS", + use: { ...test_1.devices["iPhone SE"] }, + testMatch: "**/*.mobile.spec.ts", // Only run mobile tests + snapshotDir: `__screenshots__/`, // Save screenshots to this directory + }, + { + name: "Android", + use: { ...test_1.devices["Galaxy S III"] }, + testMatch: "**/*.mobile.spec.ts", // Only run mobile tests + snapshotDir: `__screenshots__/`, // Save screenshots to this directory + }, + /* Test against mobile viewports. */ + // { + // name: 'Mobile Chrome', + // use: { ...devices['Pixel 5'] }, + // }, + // { + // name: 'Mobile Safari', + // use: { ...devices['iPhone 12'] }, + // }, + /* Test against branded browsers. */ + // { + // name: 'Microsoft Edge', + // use: { ...devices['Desktop Edge'], channel: 'msedge' }, + // }, + // { + // name: 'Google Chrome', + // use: { ...devices['Desktop Chrome'], channel: 'chrome' }, + // }, + ], + /* Run your local dev server before starting the tests */ + // webServer: { + // command: 'npm run start', + // url: 'http://127.0.0.1:3000', + // reuseExistingServer: !process.env.CI, + // }, +}); diff --git a/dist/tests/apps/dakota/desktop/test1.e2e.desktop.spec.js b/dist/tests/apps/dakota/desktop/test1.e2e.desktop.spec.js index cec2a03..6e3cd0a 100644 --- a/dist/tests/apps/dakota/desktop/test1.e2e.desktop.spec.js +++ b/dist/tests/apps/dakota/desktop/test1.e2e.desktop.spec.js @@ -1,14 +1,10 @@ "use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; Object.defineProperty(exports, "__esModule", { value: true }); const test_1 = require("@playwright/test"); const customReport_1 = require("../../../utils/customReport"); -const reportId_json_1 = __importDefault(require("../../../../next-js/public/data/reportId.json")); test_1.test.describe("Test1 @smoke @regression @sanity", () => { (0, test_1.test)("Test1", async ({ page }) => { - const testReportId = reportId_json_1.default.reportId; + // const testReportId = reportId.reportId; await page.goto("https://www.dakotasoft.com/"); test_1.test.beforeEach(async ({}, testInfo) => { const params = { diff --git a/dist/tests/apps/globallinkgoMarketing/desktop/e2e/MiniMarket.e2e.desktop.spec.js b/dist/tests/apps/globallinkgoMarketing/desktop/e2e/MiniMarket.e2e.desktop.spec.js index bef1ab7..8183ab6 100644 --- a/dist/tests/apps/globallinkgoMarketing/desktop/e2e/MiniMarket.e2e.desktop.spec.js +++ b/dist/tests/apps/globallinkgoMarketing/desktop/e2e/MiniMarket.e2e.desktop.spec.js @@ -19,7 +19,6 @@ const report = [ test_1.test.describe("Mini Market @smoke", () => { test_1.test.setTimeout(130000); const testReportId = reportId_json_1.default.reportId; - // eslint-disable-next-line no-empty-pattern test_1.test.beforeEach(async ({}, testInfo) => { (0, customReport_1.customReportTestInfoTestDescribe)({ testInfo: testInfo, @@ -35,12 +34,11 @@ test_1.test.describe("Mini Market @smoke", () => { // Setup the test environment (0, generalTestUtils_1.goToPort)(miniMarketUrlPort); report.forEach((data) => { - const { email } = data; (0, test_1.test)("Stripe Payment", async ({ page }, testInfo) => { testInfo.duration; testInfo.snapshotSuffix; // Custom report screenshot name & path - let imageName; + let imageName = ""; const pathScreenshot = `next-test-report/public/test-report`; try { // Click on the "Start Shopping" button @@ -84,7 +82,7 @@ test_1.test.describe("Mini Market @smoke", () => { pathScreenshot: pathScreenshot, imageName: imageName, jiraRef: "TICKET-001", - errorMessage: error, + errorMessage: error instanceof Error ? error.message : String(error), }); } }); diff --git a/dist/tests/utils/customReport.js b/dist/tests/utils/customReport.js index 528571e..f8e041f 100644 --- a/dist/tests/utils/customReport.js +++ b/dist/tests/utils/customReport.js @@ -4,7 +4,7 @@ exports.customReportTestInfoTestDescribe = customReportTestInfoTestDescribe; exports.customReportTestInfoCondition = customReportTestInfoCondition; exports.customReportTestInfoConditionPass = customReportTestInfoConditionPass; function customReportTestInfoTestDescribe(data) { - const { testInfo, testReportId, testReportName, testPath, typeOfTest, tickets } = data; + const { testInfo, testReportId, testReportName, testPath, typeOfTest, tickets, } = data; const testParams = { testReportId: testReportId, testPath: testPath, @@ -32,7 +32,10 @@ async function customReportTestInfoCondition(customReportData) { screenshotPath: imageName, }; const htmlDataReportDataString = JSON.stringify(htmlCustomReportData); - testInfo.annotations.push({ type: data.id ?? "default-type", description: htmlDataReportDataString }); + testInfo.annotations.push({ + type: data.id ?? "default-type", + description: htmlDataReportDataString, + }); throw new Error(errorMessage); } async function customReportTestInfoConditionPass(customReportData) { @@ -51,5 +54,8 @@ async function customReportTestInfoConditionPass(customReportData) { screenshotPath: imageName, }; const htmlDataReportDataString = JSON.stringify(htmlCustomReportData); - testInfo.annotations.push({ type: data.id ?? "default-type", description: htmlDataReportDataString }); + testInfo.annotations.push({ + type: data.id ?? "default-type", + description: htmlDataReportDataString, + }); } diff --git a/dist/tests/utils/generalTestUtils.js b/dist/tests/utils/generalTestUtils.js index 63c86d7..de9eef3 100644 --- a/dist/tests/utils/generalTestUtils.js +++ b/dist/tests/utils/generalTestUtils.js @@ -11,7 +11,6 @@ const test_1 = require("@playwright/test"); const goToPort = (port) => { test_1.test.beforeEach(async ({ page }) => { await page.goto(`${port}?testing=groovy`, { waitUntil: "load" }); - // eslint-disable-next-line no-console page.on("console", (msg) => console.log(msg.text())); }); }; @@ -23,7 +22,9 @@ const goToProject = (projectName) => { await page.getByPlaceholder("Search...").fill(projectName); await page.getByPlaceholder("Search...").press("ArrowDown"); // Check if the project with the specified name exists - const projectExists = await page.getByRole("menuitem", { name: projectName, exact: true }).isVisible(); + const projectExists = await page + .getByRole("menuitem", { name: projectName, exact: true }) + .isVisible(); if (!projectExists) { // Throw an error if the project is not found throw new Error(`Project "${projectName}" not found. Provide a valid project name.`); @@ -83,6 +84,8 @@ async function checkIconAndTooltipVisibility(page, iconId, dataObject, dataKey) } // Clicks a tab or button based on the tab or button name async function clickTabOrButton(page, testId, dataObject, tabButtonName) { - const tabButton = page.locator(`[data-testid="${testId}"]`, { hasText: dataObject[tabButtonName] }); + const tabButton = page.locator(`[data-testid="${testId}"]`, { + hasText: dataObject[tabButtonName], + }); await tabButton.click(); } diff --git a/dist/tests/utils/screenshotTests.js b/dist/tests/utils/screenshotTests.js index ce4a168..9669473 100644 --- a/dist/tests/utils/screenshotTests.js +++ b/dist/tests/utils/screenshotTests.js @@ -5,42 +5,52 @@ exports.checkFullPageScreenshotWithMask = checkFullPageScreenshotWithMask; exports.checkElementScreenshot = checkElementScreenshot; exports.checkElementScreenshotWithMask = checkElementScreenshotWithMask; exports.checkScreenshotPixelRatio = checkScreenshotPixelRatio; -// screenshotTests.ts const test_1 = require("@playwright/test"); // Function to check full page screenshot without mask -async function checkFullPageScreenshot({ page, screenshotName, fullPage = false }) { +async function checkFullPageScreenshot({ page, screenshotName, fullPage = false, }) { await page.waitForLoadState("load"); await page.waitForTimeout(3000); await page.click("body"); - await (0, test_1.expect)(page).toHaveScreenshot(screenshotName, { fullPage, animations: "disabled", timeout: 10000 }); + await (0, test_1.expect)(page).toHaveScreenshot(screenshotName, { + fullPage, + animations: "disabled", + timeout: 10000, + }); } // Function to check full page screenshot with mask -async function checkFullPageScreenshotWithMask({ page, screenshotName, fullPage = false, maskTestIds = [] }) { +async function checkFullPageScreenshotWithMask({ page, screenshotName, fullPage = false, maskTestIds = [], }) { // Create masks for dynamic elements we want to exclude from the screenshot const mask = maskTestIds.map((testId) => page.locator(`[data-testid="${testId}"]`)); // Wait for each masked element to be visible before taking the screenshot for (const maskLocator of mask) { await maskLocator.first().waitFor({ state: "visible", timeout: 10000 }); } - await (0, test_1.expect)(page).toHaveScreenshot(screenshotName, { fullPage, mask, timeout: 10000 }); + await (0, test_1.expect)(page).toHaveScreenshot(screenshotName, { + fullPage, + mask, + timeout: 10000, + }); } // Function to check screenshot of a specific element with no mask -async function checkElementScreenshot({ page, testId, screenshotName }) { +async function checkElementScreenshot({ page, testId, screenshotName, }) { const locator = page.locator(`[data-testid="${testId}"]`); await locator.waitFor({ state: "visible" }); await page.waitForTimeout(1000); await (0, test_1.expect)(locator).toHaveScreenshot(screenshotName, { timeout: 10000 }); } // Function to check screenshot of specific element with mask -async function checkElementScreenshotWithMask({ page, testId, screenshotName, maskTestIds = [] }) { +async function checkElementScreenshotWithMask({ page, testId, screenshotName, maskTestIds = [], }) { const locator = page.locator(`[data-testid="${testId}"]`); await locator.waitFor({ state: "visible" }); await page.waitForTimeout(1000); const mask = maskTestIds.map((testId) => page.locator(`[data-testid="${testId}"]`)); - await (0, test_1.expect)(locator).toHaveScreenshot(screenshotName, { mask, timeout: 10000 }); + await (0, test_1.expect)(locator).toHaveScreenshot(screenshotName, { + mask, + timeout: 10000, + }); } // Function to check screenshot with pixel ratio considerations -async function checkScreenshotPixelRatio({ page, testId, screenshotName, maxDiffPixelRatioNum }) { +async function checkScreenshotPixelRatio({ page, testId, screenshotName, maxDiffPixelRatioNum, }) { const locator = page.locator(`[data-testid="${testId}"]`); await locator.waitFor({ state: "visible" }); await page.waitForTimeout(1000); diff --git a/dist/tests/utils/types.js b/dist/tests/utils/types.js index 04363ad..c8ad2e5 100644 --- a/dist/tests/utils/types.js +++ b/dist/tests/utils/types.js @@ -1,3 +1,2 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -;