Skip to content

Commit 9155530

Browse files
feat: add exclude selectors input to accessibility scanner
Agent-Logs-Url: https://github.com/github/accessibility-scanner/sessions/13d504eb-d5cb-4e30-994f-b988560b788d Co-authored-by: abdulahmad307 <204748719+abdulahmad307@users.noreply.github.com>
1 parent 5331452 commit 9155530

5 files changed

Lines changed: 28 additions & 3 deletions

File tree

.github/actions/find/action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ inputs:
2222
color_scheme:
2323
description: 'Playwright colorScheme setting: https://playwright.dev/docs/api/class-browser#browser-new-context-option-color-scheme'
2424
required: false
25+
exclude:
26+
description: 'Stringified JSON array of CSS selectors to exclude from the Axe scan'
27+
required: false
2528

2629
outputs:
2730
findings_file:

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export async function findForUrl(
1313
includeScreenshots: boolean = false,
1414
reducedMotion?: ReducedMotionPreference,
1515
colorScheme?: ColorSchemePreference,
16+
exclude?: string[],
1617
): Promise<Finding[]> {
1718
const browser = await playwright.chromium.launch({
1819
headless: true,
@@ -56,7 +57,7 @@ export async function findForUrl(
5657
}
5758

5859
if (scansContext.shouldPerformAxeScan) {
59-
await runAxeScan({page, addFinding})
60+
await runAxeScan({page, addFinding, exclude})
6061
}
6162
} catch (e) {
6263
core.error(`Error during accessibility scan: ${e}`)
@@ -69,13 +70,18 @@ export async function findForUrl(
6970
async function runAxeScan({
7071
page,
7172
addFinding,
73+
exclude,
7274
}: {
7375
page: playwright.Page
7476
addFinding: (findingData: Finding, options?: {includeScreenshots?: boolean}) => Promise<void>
77+
exclude?: string[]
7578
}) {
7679
const url = page.url()
7780
core.info(`Scanning ${url}`)
78-
const rawFindings = await new AxeBuilder({page}).analyze()
81+
const axeBuilder = exclude && exclude.length > 0
82+
? exclude.reduce((builder, selector) => builder.exclude(selector), new AxeBuilder({page}))
83+
: new AxeBuilder({page})
84+
const rawFindings = await axeBuilder.analyze()
7985

8086
if (rawFindings) {
8187
for (const violation of rawFindings.violations) {

.github/actions/find/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,20 @@ export default async function () {
3434
colorScheme = colorSchemeInput as ColorSchemePreference
3535
}
3636

37+
const excludeInput = core.getInput('exclude', {required: false})
38+
let exclude: string[] | undefined
39+
if (excludeInput) {
40+
try {
41+
exclude = JSON.parse(excludeInput) as string[]
42+
} catch {
43+
throw new Error(`Input 'exclude' must be a valid JSON array of CSS selectors. Received: ${excludeInput}`)
44+
}
45+
}
46+
3747
const findings = []
3848
for (const url of urls) {
3949
core.info(`Preparing to scan ${url}`)
40-
const findingsForUrl = await findForUrl(url, authContext, includeScreenshots, reducedMotion, colorScheme)
50+
const findingsForUrl = await findForUrl(url, authContext, includeScreenshots, reducedMotion, colorScheme, exclude)
4151
if (findingsForUrl.length === 0) {
4252
core.info(`No accessibility gaps were found on ${url}`)
4353
continue

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
# reduced_motion: no-preference # Optional: Playwright reduced motion configuration option
5959
# color_scheme: light # Optional: Playwright color scheme configuration option
6060
# scans: '["axe","reflow-scan"]' # Optional: An array of scans (or plugins) to be performed. If not provided, only Axe will be performed.
61+
# exclude: '["iframe","#third-party-widget"]' # Optional: A JSON array of CSS selectors to exclude from the Axe scan.
6162
```
6263

6364
> 👉 Update all `REPLACE_THIS` placeholders with your actual values. See [Action Inputs](#action-inputs) for details.
@@ -130,6 +131,7 @@ Trigger the workflow manually or automatically based on your configuration. The
130131
| `reduced_motion` | No | Playwright `reducedMotion` setting for scan contexts. Allowed values: `reduce`, `no-preference` | `reduce` |
131132
| `color_scheme` | No | Playwright `colorScheme` setting for scan contexts. Allowed values: `light`, `dark`, `no-preference` | `dark` |
132133
| `scans` | No | An array of scans (or plugins) to be performed. If not provided, only Axe will be performed. | `'["axe", "reflow-scan", ...other plugins]'` |
134+
| `exclude` | No | A stringified JSON array of CSS selectors to exclude from the Axe scan. Useful for iframes, third-party widgets, or user-generated content. | `'["iframe","#third-party-widget"]'` |
133135

134136
---
135137

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ inputs:
5151
scans:
5252
description: 'Stringified JSON array of scans to perform. If not provided, only Axe will be performed'
5353
required: false
54+
exclude:
55+
description: 'Stringified JSON array of CSS selectors to exclude from the Axe scan'
56+
required: false
5457

5558
outputs:
5659
results:
@@ -114,6 +117,7 @@ runs:
114117
reduced_motion: ${{ inputs.reduced_motion }}
115118
color_scheme: ${{ inputs.color_scheme }}
116119
scans: ${{ inputs.scans }}
120+
exclude: ${{ inputs.exclude }}
117121
- name: File
118122
id: file
119123
uses: ./../../_actions/github/accessibility-scanner/current/.github/actions/file

0 commit comments

Comments
 (0)