Skip to content

Commit

Permalink
Create a11y report
Browse files Browse the repository at this point in the history
  • Loading branch information
nwmac committed Dec 18, 2024
1 parent e41e370 commit fa4d97c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,11 @@ jobs:
TEST_USERNAME: admin
TEST_A11Y: true

- name: Upload html report
- name: Upload report
uses: actions/upload-artifact@v4
with:
name: accessibility-report
path: cypress/accessibility/accessibility.html

- name: Upload screenshots
uses: actions/upload-artifact@v4
with:
name: ${{github.run_number}}-${{github.run_attempt}}-a11y-screenshots
path: cypress/screenshots
path: cypress/accessibility

unit-test:
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export default defineConfig({
require('@cypress/grep/src/plugin')(config);
// For more info: https://www.npmjs.com/package/cypress-delete-downloads-folder

require('./cypress/support/plugins/accessibility').default(on, config);
// Load Accessibility plugin if configured
if (process.env.TEST_A11Y) {
require('./cypress/support/plugins/accessibility').default(on, config);
}

on('task', { removeDirectory });

Expand Down
11 changes: 2 additions & 9 deletions cypress/support/commands/accessiblity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,10 @@ function getAccessibilityViolationsCallback(description?: string) {

cy.screenshot(`a11y_${ Cypress.currentTest.title }_${ index }`);

// cy.screenshot(`a11y_${ Cypress.currentTest.title }_${ index }`, {
// onAfterScreenshot($el, props) {
// a11yScreenshot({
// titlePath: testPath,
// props,
// });
// },
// });

// Record the screenshot against the test and move it into the a11y folder
cy.task('a11yScreenshot', {
titlePath: testPath,
test: Cypress.currentTest,
name: `a11y_${ Cypress.currentTest.title }_${ index }`
});

Expand Down
35 changes: 31 additions & 4 deletions cypress/support/plugins/accessibility/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export type Options = {
titlePath: string[];
};

type Screenshot = {
name: string;
specName: string;
path: string;
};

// Root chain
const chain: TestViolation[] = [{
name: 'Root',
Expand All @@ -56,6 +62,7 @@ const chain: TestViolation[] = [{
}];

const allViolations = [] as any[];
const screenshots = [] as Screenshot[];
let folder;

// Tidy up the chain
Expand Down Expand Up @@ -136,7 +143,19 @@ function registerHooks(on, config) {
const { titlePath, name } = options;
const found = createPath(titlePath);

found.screenshot = name;
// Move the screenshot to the accessibility folder
const details = screenshots.find((s) => s.name === name);

if (details) {
found.screenshot = path.join(details.specName, `${ name }.png`);
const screenFolder = path.join(folder, found.screenshot);
const destFile = path.join(screenFolder, `${ name }.png`);

if (!fs.existsSync(screenFolder)) {
fs.mkdirSync(screenFolder);
}
fs.renameSync(details.path, destFile);
}

return null;
}
Expand Down Expand Up @@ -169,11 +188,21 @@ function registerHooks(on, config) {
chain.push(newSpec);
});

on('after:spec', (spec) => {
on('after:spec', () => {
// Pop the spec off of the chain
chain.pop();
});

on('after:screenshot', (details) => {
const { name, specName, path } = details;

screenshots.push({
name,
specName,
path
});
});

on('after:run', () => {
const root = chain[0];

Expand All @@ -191,8 +220,6 @@ function registerHooks(on, config) {

fs.writeFileSync(path.join(folder, 'accessibility.html'), reportHTML);

// Write the validation data to disk and transform to HTML

return null;
});

Expand Down

0 comments on commit fa4d97c

Please sign in to comment.