Skip to content

Commit

Permalink
feat: refactor test files for improved readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
TBardini committed Jan 17, 2025
1 parent f2fcc05 commit e994e1d
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 25 deletions.
93 changes: 93 additions & 0 deletions dist/playwright.config.js
Original file line number Diff line number Diff line change
@@ -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,
// },
});
6 changes: 1 addition & 5 deletions dist/tests/apps/dakota/desktop/test1.e2e.desktop.spec.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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),
});
}
});
Expand Down
12 changes: 9 additions & 3 deletions dist/tests/utils/customReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
});
}
9 changes: 6 additions & 3 deletions dist/tests/utils/generalTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
});
};
Expand All @@ -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.`);
Expand Down Expand Up @@ -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();
}
28 changes: 19 additions & 9 deletions dist/tests/utils/screenshotTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion dist/tests/utils/types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
;

0 comments on commit e994e1d

Please sign in to comment.