Skip to content

Commit

Permalink
feature/IVYPORTAL-17377-Create-pipeline-to-run-Lighthouse-report
Browse files Browse the repository at this point in the history
  • Loading branch information
nhthinh-axonivy committed Dec 27, 2024
1 parent cc6b998 commit e4cdefa
Showing 1 changed file with 62 additions and 42 deletions.
104 changes: 62 additions & 42 deletions AxonIvyPortal/portal-selenium-test/lighthouse/puppeteer-script.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
const fs = require("fs");
const path = require("path");
const puppeteer = require("puppeteer");
const lighthouse = require("lighthouse");
const { URL } = require("url");
const { exec } = require("child_process");

// Read user credentials
const csvFilePath = path.join(
__dirname,
"AxonIvyPortal/portal-selenium-test/jmeter/data/users_local.csv"
);
const users = fs
.readFileSync(csvFilePath, "utf-8")
.split("\n")
.map((line) => {
const [username, password] = line.split(",");
return { username, password };
});

// Puppeteer script to navigate to the dashboard
import fs from "fs";
import path from "path";
import puppeteer from "puppeteer";
import { fileURLToPath } from "url";
import { dirname } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Add type: module to package.json
const packageJson = {
type: "module",
};

(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
try {
// Dynamically import lighthouse
const lighthouse = (await import("lighthouse")).default;

// Read user credentials
const csvFilePath = path.join(__dirname, "../jmeter/data/users_local.csv");
const users = fs
.readFileSync(csvFilePath, "utf-8")
.split("\n")
.filter(Boolean)
.map((line) => {
const [username, password] = line.trim().split(",");
return { username, password };
});

// Launch browser
const browser = await puppeteer.launch({
headless: "new",
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});

// Use the first user for login
const user = users[0];
const loginUrl = "http://localhost:8080/Portal"; // Replace with actual login URL
const page = await browser.newPage();

await page.goto(loginUrl);
await page.type("#username", user.username);
await page.type("#password", user.password);
await page.click("#login-button"); // Replace with actual login button selector
// Login process
const user = users[0];
await page.goto("http://localhost:8080/Portal");
await page.waitForSelector("#username");
await page.type("#username", user.username);
await page.type("#password", user.password);
await page.click('button[type="submit"]');

// Wait for navigation to dashboard
await page.waitForNavigation();
// Wait for dashboard
await page.waitForNavigation();

// Run Lighthouse audit
const { lhr } = await lighthouse(page.url(), {
port: new URL(browser.wsEndpoint()).port,
output: "html",
logLevel: "info",
});
// Run Lighthouse
const { lhr } = await lighthouse(page.url(), {
port: new URL(browser.wsEndpoint()).port,
output: ["json", "html"],
logLevel: "info",
onlyCategories: ["performance", "accessibility", "best-practices"],
});

// Save Lighthouse report
const reportHtml = lhr.report;
fs.writeFileSync("lighthouse-report.html", reportHtml);
// Save reports
fs.writeFileSync("lighthouse-reports/report.html", lhr.report[1]);
fs.writeFileSync(
"lighthouse-reports/report.json",
JSON.stringify(lhr, null, 2)
);

await browser.close();
await browser.close();
} catch (error) {
console.error("Error:", error);
process.exit(1);
}
})();

0 comments on commit e4cdefa

Please sign in to comment.