1- import { Builder , By , WebDriver } from "selenium-webdriver" ;
1+ import { Builder } from "selenium-webdriver" ;
2+ import * as chrome from "selenium-webdriver/chrome" ;
23import { AxeBuilder } from "@axe-core/webdriverjs" ;
34import axios from "axios" ;
45import { parseStringPromise } from "xml2js" ;
6+ import * as path from "path" ;
7+ import * as os from "os" ;
8+ import * as fs from "fs" ;
59
610const SITEMAP_URL = "http://localhost:3000/sitemap.xml" ;
711
@@ -10,28 +14,44 @@ async function getUrlsFromSitemap(url: string): Promise<string[]> {
1014 if ( res . status !== 200 ) throw new Error ( "Failed to fetch sitemap" ) ;
1115 const text = res . data ;
1216 const result = await parseStringPromise ( text ) ;
13- // Type cast omdat xml2js een vrij losse structuur teruggeeft
1417 return ( result as any ) . urlset . url . map ( ( u : any ) => u . loc [ 0 ] ) ;
1518}
1619
20+ function createUniqueUserDataDir ( ) : string {
21+ const tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "chrome-user-data-" ) ) ;
22+ return tmpDir ;
23+ }
24+
1725async function runAxeOnUrls ( urls : string [ ] ) {
1826 let errorCount = 0 ;
1927 for ( const url of urls ) {
20- const driver : WebDriver = await new Builder ( ) . forBrowser ( "chrome" ) . build ( ) ;
28+ const userDataDir = createUniqueUserDataDir ( ) ;
29+ const options = new chrome . Options ( )
30+ . headless ( )
31+ . addArguments (
32+ "--no-sandbox" ,
33+ "--disable-dev-shm-usage" ,
34+ `--user-data-dir=${ userDataDir } `
35+ ) ;
36+ const driver = await new Builder ( )
37+ . forBrowser ( "chrome" )
38+ . setChromeOptions ( options )
39+ . build ( ) ;
2140 try {
2241 await driver . get ( url ) ;
2342 const results = await new AxeBuilder ( driver ) . analyze ( ) ;
2443 if ( results . violations . length > 0 ) {
2544 errorCount += results . violations . length ;
26- console . log ( `❌ WCAG violations at ${ url } :` ) ;
27- results . violations . forEach ( ( v ) =>
45+ console . log ( `WCAG violations at ${ url } :` ) ;
46+ results . violations . forEach ( ( v : any ) =>
2847 console . log ( ` [${ v . id } ] ${ v . help } : ${ v . nodes . length } elements` )
2948 ) ;
3049 } else {
3150 console . log ( `✅ ${ url } is WCAG compliant` ) ;
3251 }
3352 } finally {
3453 await driver . quit ( ) ;
54+ fs . rmSync ( userDataDir , { recursive : true , force : true } ) ;
3555 }
3656 }
3757 if ( errorCount > 0 ) process . exit ( 1 ) ;
0 commit comments