import { Page, expect } from '@playwright/test' import * as po from './actions' import { AxeResults } from 'axe-core' export class Accessibility { #page: Page constructor({ page }: { page: Page }) { this.#page = page } getSelectors(): { [key: string]: string } { return po.selectors } async getAccessibilityConformityViolations( include: string, disabledRules?: string[] ): Promise { return await po.analyzeAccessibilityConformityViolations({ page: this.#page, include, disabledRules }) } async getSevereAccessibilityViolations( include: string, disabledRules?: string[] ): Promise { const violations = await this.getAccessibilityConformityViolations(include, disabledRules) return violations.filter( (violation) => violation.impact === 'critical' || violation.impact === 'serious' ) } async getAccessibilityConformityViolationsWithExclusions( include: string, exclude: string | string[] ): Promise { return await po.analyzeAccessibilityConformityViolationsWithExclusions({ page: this.#page, include, exclude }) } switchToCondensedTableView(): Promise { return po.switchToCondensedTableView({ page: this.#page }) } switchToDefaultTableView(): Promise { return po.switchToDefaultTableView({ page: this.#page }) } showDisplayOptions(): Promise { return po.showDisplayOptions({ page: this.#page }) } closeDisplayOptions(): Promise { return po.closeDisplayOptions({ page: this.#page }) } openFilesContextMenu(): Promise { return po.openFilesContextMenu({ page: this.#page }) } exitContextMenu(): Promise { return po.exitContextMenu({ page: this.#page }) } selectNew(): Promise { return po.selectNew({ page: this.#page }) } selectFolderOptionWithinNew(): Promise { return po.selectFolderOptionWithinNew({ page: this.#page }) } cancelCreatingNewFolder(): Promise { return po.cancelCreatingNewFolder({ page: this.#page }) } selectUpload(): Promise { return po.selectUpload({ page: this.#page }) } selectFileThroughCheckbox(): Promise { return po.checkFileCheckbox({ page: this.#page }) } deselectFileThroughCheckbox(): Promise { return po.uncheckFileCheckbox({ page: this.#page }) } static async assertNoSevereA11yViolations( page: Page, selectors: string[], selectorLabel: string, disabledRules?: string[] ): Promise { const a11yObject = new Accessibility({ page }) const allViolations: AxeResults['violations'] = [] for (const selector of selectors) { const include = a11yObject.getSelectors()[selector] || selector const violations = await a11yObject.getSevereAccessibilityViolations(include, disabledRules) allViolations.push(...violations) } expect( allViolations, `Found ${allViolations.length} severe accessibility violations in ${selectorLabel}` ).toHaveLength(0) } }