diff --git a/accessibility-checker/src-ts/bin/achecker.js b/accessibility-checker/src-ts/bin/achecker.js index 146bd95e4..1b1bc2f29 100755 --- a/accessibility-checker/src-ts/bin/achecker.js +++ b/accessibility-checker/src-ts/bin/achecker.js @@ -181,32 +181,38 @@ getInputFileList().then(async (rptInputFiles) => { let idx = 0; let failures = []; let errors = 0; - for (let f of rptInputFiles) { - let result; - let isFile = false; - try { - isFile = fs.lstatSync(f).isFile(); - f = path.resolve(f); - } catch (e) {} - if (isFile) { - result = await aChecker.getCompliance("file://"+f, f.replace(/^file:\/\//,"").replace(/[:?&=]/g,"_")); - } else { - result = await aChecker.getCompliance(f, f.replace(/^(https?:|file:)\/\//,"").replace(/[:?&=]/g,"_")); - } - if (result) { - if (aChecker.assertCompliance(result.report) === 0) { - console.log("Passed:", f); + try { + for (let f of rptInputFiles) { + let result; + let isFile = false; + try { + isFile = fs.lstatSync(f).isFile(); + f = path.resolve(f); + } catch (e) {} + if (isFile) { + result = await aChecker.getCompliance("file://"+f, f.replace(/^file:\/\//,"").replace(/[:?&=]/g,"_")); } else { - failures.push({ - file: f, - report: result.report - }); - console.log("Failed:", f); + result = await aChecker.getCompliance(f, f.replace(/^(https?:|file:)\/\//,"").replace(/[:?&=]/g,"_")); + } + if (result) { + if (aChecker.assertCompliance(result.report) === 0) { + console.log("Passed:", f); + } else { + failures.push({ + file: f, + report: result.report + }); + console.log("Failed:", f); + } + } else { + ++errors; + console.log("Error:", f); } - } else { - ++errors; - console.log("Error:", f); } + } catch (e) { + console.error(e); + } finally { + await aChecker.close(); } if (failures.length > 0) { console.log(); @@ -218,7 +224,7 @@ getInputFileList().then(async (rptInputFiles) => { } console.log(); console.log(`${rptInputFiles.length-failures.length-errors} of ${rptInputFiles.length} passed.`) - await aChecker.close(); + //await aChecker.close(); if (failures.length !== 0 || errors !== 0) { process.exitCode = 1; } diff --git a/accessibility-checker/src/README.md b/accessibility-checker/src/README.md index f61b1fd84..09fc4b3f2 100644 --- a/accessibility-checker/src/README.md +++ b/accessibility-checker/src/README.md @@ -32,17 +32,69 @@ The following is how to perform an accessibility scan within your test cases and ```javascript const aChecker = require("accessibility-checker"); -// Perform the accessibility scan using the aChecker.getCompliance API -aChecker.getCompliance(testDataFileContent, testLabel).then((results) => { - const report = results.report; - - // Call the aChecker.assertCompliance API which is used to compare the results with the baseline object if we can find one that - // matches the same label which was provided. - const returnCode = aChecker.assertCompliance(report); - - // In the case that the violationData is not defined then trigger an error right away. - expect(returnCode).toBe(0, "Scanning " + testLabel + " failed."); -}); +try { + // Perform the accessibility scan using the aChecker.getCompliance API + aChecker.getCompliance(testDataFileContent, testLabel).then((results) => { + const report = results.report; + + // Call the aChecker.assertCompliance API which is used to compare the results with the baseline object if we can find one that + // matches the same label which was provided. + const returnCode = aChecker.assertCompliance(report); + + // In the case that the violationData is not defined then trigger an error right away. + expect(returnCode).toBe(0, "Scanning " + testLabel + " failed."); + }); +} catch (err) { + console.error(err); +} finally { + // close the engine + await aChecker.close(); +}; +``` +Note that it's critical to close the engine, otherwise, output files for the report may not be generated properly. +If you execute batch scans, the engine should be closed after all the scans are completed for better performance. The following is a sample usage scenario: +```javascript +async batchScan(rptInputFiles) { + let failures = []; + try { + for (let f of rptInputFiles) { + let result; + let isFile = false; + try { + isFile = fs.lstatSync(f).isFile(); + f = path.resolve(f); + } catch (e) {} + if (isFile) { + result = await aChecker.getCompliance("file://"+f, f.replace(/^file:\/\//,"").replace(/[:?&=]/g,"_")); + } else { + result = await aChecker.getCompliance(f, f.replace(/^(https?:|file:)\/\//,"").replace(/[:?&=]/g,"_")); + } + if (result) { + if (aChecker.assertCompliance(result.report) === 0) { + console.log("Passed:", f); + } else { + failures.push({ + file: f, + report: result.report + }); + console.log("Failed:", f); + } + } else { + console.log("Error:", f); + } + } + if (failures.length > 0) { + console.log("Failing scan details:"); + for (const fail of failures) { + console.log(aChecker.stringifyResults(fail.report)); + } + } + } catch (e) { + console.error(e); + } finally { + await aChecker.close(); + } +} ``` Refer to [Examples](https://github.com/IBMa/equal-access/tree/master/accessibility-checker/boilerplates) for sample usage scenarios.