|
| 1 | +/************************************************** |
| 2 | + * Usage: |
| 3 | + * |
| 4 | + * 1. npm run test |
| 5 | + * |
| 6 | + * Will run the entire set of tests including this lighthouse test against all URL's |
| 7 | + * found in the tmp/downloaded-urls.txt file. |
| 8 | + * |
| 9 | + * |
| 10 | + * 2. npm run test-lighthouse |
| 11 | + * |
| 12 | + * Will run only the lighthouse accessibility tests against all URL's |
| 13 | + * found in the tmp/downloaded-urls.txt file. |
| 14 | + * |
| 15 | + * |
| 16 | + * 3. npm run test-lighthouse-url {VALID URL} |
| 17 | + * |
| 18 | + * Will run the lighthouse accessibilty tests against a single URL. |
| 19 | + * For example: npm run test-lighthouse-url https://www.useragentman.com/enable/index.php |
| 20 | + * |
| 21 | + * |
| 22 | + * Note: |
| 23 | + * |
| 24 | + * If the tmp/downloaded-urls.txt file does not exist, you can manually create one |
| 25 | + * or you can generate one by first running npm run test-lighthouse. |
| 26 | + *********************************************** */ |
| 27 | + |
| 28 | +const { spawn } = require('child_process'); |
| 29 | +const fs = require('fs'); |
| 30 | + |
| 31 | +const SUMMARY_PATH = 'report/lighthouse/summary.json'; |
| 32 | +const REPORT_PATH = 'report/lighthouse/'; |
| 33 | +const RED_TXT = '\x1b[31m%s\x1b[0m'; |
| 34 | +const GREEN_TXT = '\x1b[32m%s\x1b[0m'; |
| 35 | +const YELLOW_TXT = '\x1b[33m%s\x1b[0m'; |
| 36 | +const SCORE_THRESHOLD = 1; |
| 37 | + |
| 38 | +const getOptions = () => { |
| 39 | + const singleUrl = process.argv[2]; |
| 40 | + const downloadedUrls = 'tmp/downloaded-urls.txt'; |
| 41 | + const isValidUrl = URL.canParse(singleUrl); |
| 42 | + |
| 43 | + if (singleUrl !== undefined && !isValidUrl) { |
| 44 | + console.error( |
| 45 | + `Error: ${singleUrl} is not a valid URL. Please include http:// or https://`, |
| 46 | + ); |
| 47 | + process.exit(1); |
| 48 | + } |
| 49 | + |
| 50 | + const args = isValidUrl |
| 51 | + ? [`-s ${singleUrl} --params "--only-categories=accessibility"`] |
| 52 | + : [`-f ${downloadedUrls} --params "--only-categories=accessibility"`]; |
| 53 | + |
| 54 | + const numPages = isValidUrl ? 1 : getNumPages(downloadedUrls); |
| 55 | + |
| 56 | + return { |
| 57 | + command: './node_modules/.bin/lighthouse-batch', |
| 58 | + args, |
| 59 | + numPages, |
| 60 | + }; |
| 61 | +}; |
| 62 | + |
| 63 | +const readFileSync = (path, encoding = 'utf-8') => { |
| 64 | + try { |
| 65 | + return fs.readFileSync(path, encoding); |
| 66 | + } catch (err) { |
| 67 | + throw new Error(`Error reading file at ${path}: ${err.message}`); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +const fileExists = (path) => { |
| 72 | + if (!fs.existsSync(path)) { |
| 73 | + throw new Error(`File not found: ${path}`); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +const getNumPages = (downloadedUrls) => { |
| 78 | + fileExists(downloadedUrls); |
| 79 | + return readFileSync(downloadedUrls).split('\n').filter(Boolean).length; |
| 80 | +}; |
| 81 | + |
| 82 | +const getReport = (fileName) => { |
| 83 | + fileExists(fileName); |
| 84 | + return JSON.parse(readFileSync(fileName)); |
| 85 | +}; |
| 86 | + |
| 87 | +const logPageStatus = ({ fileName }) => { |
| 88 | + const { runtimeError, requestedUrl, categories } = getReport(fileName); |
| 89 | + |
| 90 | + if (runtimeError) { |
| 91 | + console.log( |
| 92 | + YELLOW_TXT, |
| 93 | + `🚫 Error scanning: ${requestedUrl} - ${runtimeError.message}\n`, |
| 94 | + ); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const statusColor = |
| 99 | + categories.accessibility.score >= SCORE_THRESHOLD ? GREEN_TXT : RED_TXT; |
| 100 | + const statusMessage = |
| 101 | + categories.accessibility.score >= SCORE_THRESHOLD |
| 102 | + ? '✅ Pass' |
| 103 | + : '❌ Fail'; |
| 104 | + |
| 105 | + console.log(statusColor, `${statusMessage}: ${requestedUrl}\n`); |
| 106 | +}; |
| 107 | + |
| 108 | +const printIssuesSummary = (audits, url, fileName, score) => { |
| 109 | + console.log(RED_TXT, `\n${url} failed scan with score: ${score}%:\n`); |
| 110 | + console.log( |
| 111 | + RED_TXT, |
| 112 | + `Visit https://googlechrome.github.io/lighthouse/viewer/ and upload ${fileName} to see the full accessibility report.\n`, |
| 113 | + ); |
| 114 | + |
| 115 | + Object.values(audits).forEach( |
| 116 | + ({ score, id, title, description, details }, index) => { |
| 117 | + if (score < 1 && score !== null) { |
| 118 | + console.log(` Issue ${index + 1}: ${id}\n`); |
| 119 | + console.log(` Title: ${title}\n`); |
| 120 | + console.log( |
| 121 | + ` Selector: ${details?.items[0]?.node?.selector}\n`, |
| 122 | + ); |
| 123 | + console.log(` Description: ${description}\n\n`); |
| 124 | + } |
| 125 | + }, |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +const formatSummary = () => { |
| 130 | + fileExists(SUMMARY_PATH); |
| 131 | + const summary = JSON.parse(readFileSync(SUMMARY_PATH)); |
| 132 | + |
| 133 | + let passCount = 0, |
| 134 | + failCount = 0, |
| 135 | + errorCount = 0; |
| 136 | + |
| 137 | + console.log('\nLighthouse Scan Summary ...\n'); |
| 138 | + |
| 139 | + summary.forEach((item) => { |
| 140 | + const fileName = `${REPORT_PATH}${item.file}`; |
| 141 | + const report = getReport(fileName); |
| 142 | + const score = Number(item.score) * 100; |
| 143 | + |
| 144 | + if (item?.error) return errorCount++; |
| 145 | + if (report.categories.accessibility.score === SCORE_THRESHOLD) |
| 146 | + return passCount++; |
| 147 | + |
| 148 | + printIssuesSummary(report.audits, item.url, fileName, score); |
| 149 | + failCount++; |
| 150 | + }); |
| 151 | + |
| 152 | + const totalCount = passCount + failCount + errorCount; |
| 153 | + const statusColor = failCount === 0 ? GREEN_TXT : RED_TXT; |
| 154 | + console.log( |
| 155 | + statusColor, |
| 156 | + `Scan complete: ${passCount}/${totalCount} URLs passed\n`, |
| 157 | + ); |
| 158 | +}; |
| 159 | + |
| 160 | +const runLighthouseBatch = () => { |
| 161 | + const { numPages } = getOptions(); |
| 162 | + |
| 163 | + console.log( |
| 164 | + `\nLighthouse Scan Started on ${numPages} pages, this may take awhile ...\n`, |
| 165 | + ); |
| 166 | + |
| 167 | + return new Promise((resolve, reject) => { |
| 168 | + const { command, args } = getOptions(); |
| 169 | + const child = spawn(command, args, { shell: true }); |
| 170 | + |
| 171 | + child.stderr.on('data', (data) => { |
| 172 | + data.toString() |
| 173 | + .split('\n') |
| 174 | + .filter((line) => |
| 175 | + line.includes('Printer json output written to'), |
| 176 | + ) |
| 177 | + .forEach((line) => { |
| 178 | + const match = line.match( |
| 179 | + /(?<=Printer json output written to\s).*$/, |
| 180 | + ); |
| 181 | + if (match) logPageStatus({ fileName: match[0] }); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + child.on('error', (err) => |
| 186 | + reject( |
| 187 | + new Error(`Failed to run the lighthouse scan: ${err.message}`), |
| 188 | + ), |
| 189 | + ); |
| 190 | + |
| 191 | + child.on('close', (code) => { |
| 192 | + if (code === 0) resolve(); |
| 193 | + else |
| 194 | + reject( |
| 195 | + new Error( |
| 196 | + `Lighthouse batch scan failed with exit code ${code}.`, |
| 197 | + ), |
| 198 | + ); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}; |
| 202 | + |
| 203 | +runLighthouseBatch() |
| 204 | + .then(formatSummary) |
| 205 | + .catch((err) => |
| 206 | + console.error(`Error running runLighthouseBatch: ${err.message}`), |
| 207 | + ); |
0 commit comments