Files
Playwright-A11y-Dashboard/services/PlaywrightService.js
2025-05-22 23:46:37 -05:00

63 lines
1.4 KiB
JavaScript

// import playwright dependencies
const { test, expect } = require('../helpers/axe-test.js');
const { chromium } = require('playwright');
const { injectAxe, checkA11y } = require('axe-playwright');
class PlaywrightService {
#domain; // domain of the site to be tested
// constructor
constructor(domain) {
this.#domain = domain;
}
// get sitemap url for the this domain
sitemapUrl() {
return `${this.rootUrl()}/sitemap.xml`;
}
// get the url of the site to be tested
rootUrl() {
return `https://${domain}`;
}
/**
* Get the list of urls to be tested by querying
* the sitemap and returning the list of urls
*
* @param {string} url - The URL of the sitemap
*
* @returns {Array} - The list of urls to be tested
*/
async runAccessibilityTest() {
const browser = await chromium.launch();
const page = await browser.newPage();
try {
await page.goto(this.rootUrl());
await injectAxe(page);
const results = await page.evaluate(async () => {
return await window.axe.run();
});
return results.violations;
} finally {
await browser.close();
}
return results;
}
/**
*
*/
}
module.exports = PlaywrightService;