Skip to content

Commit 0fbb9d7

Browse files
committed
Extracts generareScreenshots into another function
1 parent 4dd03c7 commit 0fbb9d7

2 files changed

Lines changed: 44 additions & 35 deletions

File tree

.github/actions/find/src/findForUrl.ts

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@ import type { Finding } from './types.d.js';
22
import AxeBuilder from '@axe-core/playwright'
33
import playwright from 'playwright';
44
import { AuthContext } from './AuthContext.js';
5-
import fs from "node:fs";
6-
import path from "node:path";
7-
import crypto from "node:crypto";
8-
9-
// Use GITHUB_WORKSPACE to ensure screenshots are saved in the workflow workspace root
10-
// where the artifact upload step can find them
11-
const SCREENSHOT_DIR = path.join(
12-
process.env.GITHUB_WORKSPACE || process.cwd(),
13-
".screenshots",
14-
);
5+
import { generateScreenshots } from "./generateScreenshots.js";
156

167
export async function findForUrl(
178
url: string,
@@ -31,33 +22,10 @@ export async function findForUrl(
3122
let findings: Finding[] = [];
3223
try {
3324
const rawFindings = await new AxeBuilder({ page }).analyze();
34-
let screenshotId: string | undefined;
3525

26+
let screenshotId: string | undefined;
3627
if (includeScreenshots) {
37-
// Ensure screenshot directory exists
38-
if (!fs.existsSync(SCREENSHOT_DIR)) {
39-
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
40-
console.log(`Created screenshot directory: ${SCREENSHOT_DIR}`);
41-
} else {
42-
console.log(`Using existing screenshot directory ${SCREENSHOT_DIR}`);
43-
}
44-
45-
try {
46-
const screenshotBuffer = await page.screenshot({
47-
fullPage: true,
48-
type: "png",
49-
});
50-
51-
screenshotId = crypto.randomUUID();
52-
const filename = `${screenshotId}.png`;
53-
const filepath = path.join(SCREENSHOT_DIR, filename);
54-
55-
fs.writeFileSync(filepath, screenshotBuffer);
56-
console.log(`Screenshot saved: ${filename}`);
57-
} catch (error) {
58-
console.error("Failed to capture/save screenshot:", error);
59-
screenshotId = undefined;
60-
}
28+
screenshotId = await generateScreenshots(page);
6129
}
6230

6331
findings = rawFindings.violations.map((violation) => ({
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import crypto from "node:crypto";
4+
import type { Page } from "playwright";
5+
6+
// Use GITHUB_WORKSPACE to ensure screenshots are saved in the workflow workspace root
7+
// where the artifact upload step can find them
8+
const SCREENSHOT_DIR = path.join(
9+
process.env.GITHUB_WORKSPACE || process.cwd(),
10+
".screenshots",
11+
);
12+
13+
export const generateScreenshots = async function (page: Page) {
14+
let screenshotId: string | undefined;
15+
// Ensure screenshot directory exists
16+
if (!fs.existsSync(SCREENSHOT_DIR)) {
17+
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
18+
console.log(`Created screenshot directory: ${SCREENSHOT_DIR}`);
19+
} else {
20+
console.log(`Using existing screenshot directory ${SCREENSHOT_DIR}`);
21+
}
22+
23+
try {
24+
const screenshotBuffer = await page.screenshot({
25+
fullPage: true,
26+
type: "png",
27+
});
28+
29+
screenshotId = crypto.randomUUID();
30+
const filename = `${screenshotId}.png`;
31+
const filepath = path.join(SCREENSHOT_DIR, filename);
32+
33+
fs.writeFileSync(filepath, screenshotBuffer);
34+
console.log(`Screenshot saved: ${filename}`);
35+
} catch (error) {
36+
console.error("Failed to capture/save screenshot:", error);
37+
screenshotId = undefined;
38+
}
39+
40+
return screenshotId
41+
}

0 commit comments

Comments
 (0)