Skip to content

Commit

Permalink
feat: add possibility to close cookie consents (or other popus); fix:…
Browse files Browse the repository at this point in the history
… some bugfixes for scroll elements in view
  • Loading branch information
forsti0506 committed Aug 25, 2021
1 parent 1e2cde0 commit 26dbef4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 59 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export interface Config {
runOnly: RunOnly | TagValue[] | string[];
crawl: boolean;
name: string;

cookieSelector?: string; // provide a selector for clicking cookie consent button (example: "[id*=cookie] a, [class*=cookie] a, [id*=cookie] button, [class*=cookie] button, [id*=didomi] button")
cookieText?: string; //provides a text for cookie consent text (example: "^(Alle akzeptieren|Akzeptieren|Verstanden|Zustimmen|Okay|OK|Alle Cookies akzeptieren)$")
}
```

Expand Down
2 changes: 2 additions & 0 deletions lib/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface Config {
runOnly: RunOnly | TagValue[] | string[];
crawl: boolean;
name: string;
cookieText?: string;
cookieSelector?: string;
}

interface AxeConfig {
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/analyze-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Page } from 'puppeteer';
import { ResultByUrl } from '../models/a11y-sitechecker-result';
import { makeScreenshotsWithErrorsBorderd } from './make-sreenshots-with-errors-borderd';
import { createUrlResult } from './create-url-result';
import { acceptCookieConsent } from './accept-cookies';

const savedScreenshotHtmls: string[] = [];

Expand All @@ -19,6 +20,9 @@ export async function analyzeUrl(
): Promise<ResultByUrl | null> {
if ((await page.url()) !== url) {
await page.goto(url, { waitUntil: 'load' });
if(config.cookieText && config.cookieSelector) {
await acceptCookieConsent(page, config);
}
await waitForHTML(page, config.timeout, config.debugMode);
} else {
debug(config.debugMode, 'URL already open.' + url);
Expand Down
38 changes: 26 additions & 12 deletions lib/utils/make-sreenshots-with-errors-borderd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { Config } from '../models/config';
import { debug, saveScreenshot } from './helper-functions';
import { v4 as uuidv4 } from 'uuid';

const uniqueNamePerUrl: Map<string, {id: string, count:number}> = new Map();

export async function makeScreenshotsWithErrorsBorderd(
resultByUrl: ResultByUrl,
page: Page,
config: Config,
savedScreenshotHtmls: string[],
): Promise<void> {
if(!uniqueNamePerUrl.get(resultByUrl.url)) {
uniqueNamePerUrl.set(resultByUrl.url, {id: uuidv4(), count: 0});
}
const currentMapObject = uniqueNamePerUrl.get(resultByUrl.url)!;
debug(config.debugMode, 'make screenshots with border');
page.on('console', (log) => {
debug(config.debugMode, log.text());
Expand All @@ -20,19 +26,19 @@ export async function makeScreenshotsWithErrorsBorderd(
debug(
config.debugMode,
e.message +
'. Ignored because normally it means that Function already there. (Adding debug to winwo in expose object)',
'. Ignored because normally it means that the function is already exposed. (Adding debug to window in expose object)',
);
}
for (const result of resultByUrl.violations) {
for (const node of result.nodes) {
if (!savedScreenshotHtmls.includes(node.html)) {
debug(config.debugMode, 'Adding border to: ' + JSON.stringify(node.target[0]));
await page.evaluate(
debug(config.debugMode, '(Count:' + currentMapObject.count + ')Adding border to: ' + JSON.stringify(node.target[0]));
const isVisible = await page.evaluate(
async (elementSelector, debugMode) => {
const dom: Element = document.querySelector(elementSelector);
let elementVisible = false;
if (dom) {
let currentDom = dom;
let elementVisible = false;
let k = 0;
const tolerance = 0.01;
const percentX = 90;
Expand All @@ -43,10 +49,9 @@ export async function makeScreenshotsWithErrorsBorderd(

const elementRect = currentDom.getBoundingClientRect();
const parentRects: DOMRect[] = [];

while (currentDom.parentElement != null) {
parentRects.push(currentDom.parentElement.getBoundingClientRect());
currentDom = currentDom.parentElement;
currentDom = currentDom.parentElement;
}

elementVisible = parentRects.every(function (parentRect) {
Expand All @@ -61,10 +66,12 @@ export async function makeScreenshotsWithErrorsBorderd(
return (
visiblePercentageX + tolerance > percentX &&
visiblePercentageY + tolerance > percentY
);
) && elementRect.top < window.innerHeight && elementRect.bottom >= 0;
});

if (!elementVisible) dom.scrollIntoView();
if (!elementVisible) {
dom.scrollIntoView();
currentDom = dom;
}
k++;
}
if (dom.tagName === 'A') {
Expand All @@ -89,13 +96,20 @@ export async function makeScreenshotsWithErrorsBorderd(
} else {
window.debug(debugMode, 'No element found with selector ' + elementSelector);
}
return elementVisible;
},
node.target[0],
config.debugMode,
);
const image = uuidv4() + '.png';
await saveScreenshot(page, config.imagesPath, image, config.saveImages);
node.image = image;
if(isVisible) {
const image = currentMapObject.id + '_' + currentMapObject.count + '.png';
await saveScreenshot(page, config.imagesPath, image, config.saveImages);
node.image = image;
currentMapObject.count++;
} else {
debug(config.debugMode, JSON.stringify(node.target[0]) + ' is not visible anytime');
}

await page.evaluate((element) => {
const dom = document.querySelector(element);
if (dom) {
Expand Down
22 changes: 11 additions & 11 deletions lib/utils/mark-all-tabable-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export async function markAllTabableItems(
};
const isElementVisible = async (element): Promise<boolean> => {
const tolerance = 0.01;
const percentX = 70;
const percentY = 70;
const percentX = 90;
const percentY = 90;
let currentDom = element;

const elementRect = currentDom.getBoundingClientRect();
Expand Down Expand Up @@ -169,19 +169,20 @@ export async function markAllTabableItems(
}
}
tabbingNumber++;
elementsFromEvaluationParsed.elementsByVisibility.push({
element: element.id,
visible: elementVisible,
});
}

if (elementVisible && !firstVisibleElement) {
firstVisibleElement = true;
}

elementsFromEvaluationParsed.elementsByVisibility.push({
element: element.id,
visible: elementVisible,
});
await window.debug(

window.debug(
debugMode,
element.tagName + ' is visible: ' + elementVisible + 'and got number' + i,
element.tagName + ' is visible: ' + elementVisible + ' and got number ' + i,
);
}
i++;
Expand All @@ -192,6 +193,7 @@ export async function markAllTabableItems(
let firstVisibleElement = false;
let i = 0;
let tabbingNumber = elementsFromEvaluationParsed.currentIndex;
console.log('yessinger: ' + tabbingNumber)
for (const elementSelector of elementsFromEvaluationParsed.elementsByVisibility) {
const element = document.getElementById(elementSelector.element);
if (element) {
Expand All @@ -205,13 +207,11 @@ export async function markAllTabableItems(

if (!elementVisible) element.scrollIntoView();
k++;
console.log('step: ' + k);
}
console.log('elementSelector' + elementSelector + 'visibility' + elementVisible);
console.log('elementSelector: ' + JSON.stringify(elementSelector) + ', visibility ' + elementVisible);

if (elementVisible && !firstVisibleElement) {
firstVisibleElement = true;
console.log('i am here elementnumer:' + tabbingNumber);
const oldElementsToRemove = Array.from(document.querySelectorAll('[id^=span_id]'));
for (const oldElement of oldElementsToRemove) {
oldElement.remove();
Expand Down
41 changes: 6 additions & 35 deletions lib/utils/save-results-to-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,44 +75,15 @@ export async function saveResultsToFile(config: Config, sitecheckerResult: A11yS
defineExtraTags(sitecheckerResult, config);
fs.writeFileSync(fileToSave, JSON.stringify(siteResult, null, 4));
fs.writeFileSync(config.resultsPath + 'files.json', JSON.stringify(fileObject, null, 4));
const basePath = config.resultsPathPerUrl + getEscaped(id) + '_' + sitecheckerResult.testEnvironment?.windowWidth + '_' + sitecheckerResult.testEnvironment?.windowHeight;

const violationsPath =
config.resultsPathPerUrl +
getEscaped(id) +
'_' +
sitecheckerResult.testEnvironment?.windowWidth +
'_' +
sitecheckerResult.testEnvironment?.windowHeight +
'_violations.json';
fs.writeFileSync(violationsPath, JSON.stringify(sitecheckerResult.violations, null, 4));
const violationsPath = basePath + '_violations.json';
const incompletesPath = basePath + '_incompletes.json';
const passesPath = basePath + '_passes.json';
const inapplicablesPath = basePath + '_inapplicables.json';

const incompletesPath =
config.resultsPathPerUrl +
getEscaped(id) +
'_' +
sitecheckerResult.testEnvironment?.windowWidth +
'_' +
sitecheckerResult.testEnvironment?.windowHeight +
'_incompletes.json';
fs.writeFileSync(violationsPath, JSON.stringify(sitecheckerResult.violations, null, 4));
fs.writeFileSync(incompletesPath, JSON.stringify(sitecheckerResult.incomplete, null, 4));

const passesPath =
config.resultsPathPerUrl +
getEscaped(id) +
'_' +
sitecheckerResult.testEnvironment?.windowWidth +
'_' +
sitecheckerResult.testEnvironment?.windowHeight +
'_passes.json';
fs.writeFileSync(passesPath, JSON.stringify(sitecheckerResult.passes, null, 4));

const inapplicablesPath =
config.resultsPathPerUrl +
getEscaped(id) +
'_' +
sitecheckerResult.testEnvironment?.windowWidth +
'_' +
sitecheckerResult.testEnvironment?.windowHeight +
'_inapplicables.json';
fs.writeFileSync(inapplicablesPath, JSON.stringify(sitecheckerResult.inapplicable, null, 4));
}
6 changes: 6 additions & 0 deletions lib/utils/setup-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export function setupConfig(options: OptionValues): Config {
if (configFile.clickableItemSelector) {
config.clickableItemSelector = configFile.clickableItemSelector;
}
if (configFile.cookieSelector) {
config.cookieSelector = configFile.cookieSelector;
}
if (configFile.cookieText) {
config.cookieText = configFile.cookieText;
}
if (configFile.crawl) {
if(config.urlsToAnalyze?.length === 1 && configFile.crawl) {
config.crawl = true;
Expand Down

0 comments on commit 26dbef4

Please sign in to comment.