Updated script to only take screenshots on failed steps

This commit is contained in:
2025-10-03 12:44:12 -07:00
parent 87e99cb78a
commit 32d4994eb0
14 changed files with 511 additions and 175 deletions

View File

@@ -383,31 +383,31 @@ const takeScreenshot = async (driver, stepIndex, stepDescription) => {
};
const clearOldResults = () => {
// Remove old screenshots
//Remove old screenshots
const screenshotDir = path.join('screenshots', 'login');
if (fs.existsSync(screenshotDir)) {
fs.rmSync(screenshotDir, { recursive: true, force: true });
}
fs.mkdirSync(screenshotDir, { recursive: true });
// Remove old Excel file
//Remove old Excel file
if (fs.existsSync('loginResults.xlsx')) {
fs.unlinkSync('loginResults.xlsx');
}
};
try {
// Initialize workbook and worksheet
//Initialize workbook and worksheet
clearOldResults();
// Initialize workbook and worksheet
//Initialize workbook and worksheet
workbook = XLSX.utils.book_new();
worksheet = XLSX.utils.aoa_to_sheet([
['Step Number', 'Step Description', 'Status', 'Timestamp', 'Screenshot']
]);
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
// Log test start time
//Log test start time
worksheet = logStep(worksheet, 'Test Start', 'Started', '', startTime, '');
let allStepsPassed = true;
@@ -416,13 +416,37 @@ try {
const stepIndex = steps.indexOf(step) + 1;
const timestamp = new Date().toISOString();
try {
try {
//Screenshot of failed step
await step.action();
const screenshotPath = await takeScreenshot(driver, stepIndex, step.description);
worksheet = logStep(worksheet, step.description, 'Passed', stepIndex, timestamp, screenshotPath);
//Log a successful step with no screenshot
worksheet = logStep(
worksheet,
step.description,
'Passed',
stepIndex,
timestamp,
null //No screenshot for passed steps
);
} catch (error) {
const screenshotPath = await takeScreenshot(driver, stepIndex, `${step.description}_Failed`);
worksheet = logStep(worksheet, step.description, 'Failed', stepIndex, timestamp, screenshotPath);
//Only take screenshot if the step fails
const screenshotPath = await takeScreenshot(
driver,
stepIndex,
`${step.description}_Failed`
);
worksheet = logStep(
worksheet,
step.description,
'Failed',
stepIndex,
timestamp,
screenshotPath
);
console.error(`Step ${stepIndex} FAILED: ${error.message}`);
testStatus = 'Failed';
allStepsPassed = false;
@@ -440,11 +464,11 @@ try {
testStatus = 'Failed';
}
// Log test end time
//Log test end time
const endTime = new Date().toISOString();
worksheet = logStep(worksheet, 'Test End', testStatus, '', endTime, '');
// Write the updated worksheet back to the workbook and save it
//Write the updated worksheet back to the workbook and save it
workbook.Sheets['Sheet1'] = worksheet;
XLSX.writeFile(workbook, 'loginResults.xlsx');
} finally {