Skip to content

Commit

Permalink
fix: about:blank error in consent screen, node 16 deprectations
Browse files Browse the repository at this point in the history
  • Loading branch information
forsti0506 committed Feb 19, 2022
1 parent 186c0bd commit 365a8ce
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 54 deletions.
89 changes: 49 additions & 40 deletions lib/utils/accept-consent-screens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Page } from 'puppeteer';
import { Config } from '../models/config';
import { debug, waitForHTML } from './helper-functions';
import { debug, log, waitForHTML } from './helper-functions';
import { saveScreenshot } from './helper-saving-screenshots';

let count = 0;
Expand All @@ -9,48 +9,57 @@ export async function acceptCookieConsent(page: Page, config: Config): Promise<v
// await new Promise((resolve) => setTimeout(resolve, 2000)); //wait for frames to be loaded; ToDo: consider alternatives
await waitForHTML(page, config.timeout, config.debugMode);
const frames = page.frames();
frames.filter((f) => f.url() !== 'about:blank');
let cookieId;
for (const frame of frames) {
debug(config.debugMode, 'Check frame ' + (frame.name() || frame.url()) + ' for consent button');
if (config.cookieSelector && config.cookieText) {
cookieId = await frame.evaluate(
(cookieSelector, cookieText, count) => {
const elements = document.querySelectorAll(cookieSelector);
const cookieElements = Array.from(elements).filter((d) =>
RegExp(cookieText, 'i').test(d.textContent.trim()),
);
if (cookieElements && cookieElements.length > 0) {
const element: HTMLElement = cookieElements[0];
if (!element.id) {
element.setAttribute('id', 'consent_screen_' + count);
try {
for (const frame of frames) {
debug(config.debugMode, 'Check frame ' + (frame.name() || frame.url()) + ' for consent button');
if (config.cookieSelector && config.cookieText) {
cookieId = await frame.evaluate(
(cookieSelector, cookieText, count) => {
const elements = document.querySelectorAll(cookieSelector);
const cookieElements = Array.from(elements).filter((d) =>
RegExp(cookieText, 'i').test(d.textContent.trim()),
);
if (cookieElements && cookieElements.length > 0) {
const element: HTMLElement = cookieElements[0];
if (!element.id) {
element.setAttribute('id', 'consent_screen_' + count);
}
return cookieElements[0].id;
} else {
return undefined;
}
return cookieElements[0].id;
} else {
return undefined;
}
},
config.cookieSelector,
config.cookieText,
count,
);
}
},
config.cookieSelector,
config.cookieText,
count,
);
}

if (cookieId) {
await saveScreenshot(
page,
config.imagesPath,
'consent_screen_' + count + '.png',
config.saveImages,
config.debugMode,
);
await frame.evaluate((cookieId) => {
const element = document.getElementById(cookieId);
element?.click();
}, cookieId);
count++;
break;
} else {
debug(config.debugMode, 'No cookie element found. Iframe Name or Url: ' + (frame.name() || frame.url()));
if (cookieId) {
await saveScreenshot(
page,
config.imagesPath,
'consent_screen_' + count + '.png',
config.saveImages,
config.debugMode,
);
await frame.evaluate((cookieId) => {
const element = document.getElementById(cookieId);
element?.click();
}, cookieId);
count++;
break;
} else {
debug(
config.debugMode,
'No cookie element found. Iframe Name or Url: ' + (frame.name() || frame.url()),
);
}
}
} catch (e: any) {
debug(config.debugMode, e);
log('Error in capturing consent Frames. Ignore it! ');
}
}
4 changes: 2 additions & 2 deletions lib/utils/analyze-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Config, SitecheckerViewport } from '../models/config';
import { analyzeUrl } from './analyze-url';
import { clickingElements } from './clicking-elements';
import { getLinks } from './get-links';
import { log } from './helper-functions';
import { debug, log } from './helper-functions';

const elementsToClick: Map<string, string[]> = new Map<string, string[]>();

Expand Down Expand Up @@ -78,7 +78,7 @@ export async function analyzeSite(
const results = lastValueFrom(
from(links.entries()).pipe(
mergeMap(async ([i, link]) => {
log(config.debugMode, 'Visiting ' + i + ' of ' + (links.length - 1));
debug(config.debugMode, 'Visiting ' + i + ' of ' + (links.length - 1));
const page = await browser.newPage();
const viewport = firstpage.viewport();
if (viewport) {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/analyze-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function analyzeUrl(
alreadyVisited: Map<string, SitecheckerViewport>,
savedScreenshotHtmls: Map<string, string>,
): Promise<ResultByUrl | null> {
if ((await page.url()) !== url) {
if (page.url() !== url) {
await page.goto(url, { waitUntil: 'load' });
if (config.cookieText && config.cookieSelector) {
await acceptCookieConsent(page, config);
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function analyzeUrl(
if (axeResults) {
urlResult = await createUrlResult(url, axeResults);
await makeScreenshotsWithErrorsBorderd(urlResult, page, config, savedScreenshotHtmls);
const events = await markAllEvents(page);
const events = await markAllEvents(page, config);
await markAllTabableItems(page, url, config, urlResult, events);

return urlResult;
Expand Down
9 changes: 4 additions & 5 deletions lib/utils/make-sreenshots-with-errors-borderd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ export async function makeScreenshotsWithErrorsBorderd(
currentMapObject = uniqueNamePerUrl.get(resultByUrl.url);
}

debug(config.debugMode, 'make screenshots with border');
debug(config.debugMode, 'make screenshots with border for ' + page.url());
try {
await page.exposeFunction('debug', debug);
await page.evaluate(exposeDepsJs({ isElementVisible }));
await page.evaluate(exposeDepsJs({ highestZIndex }));
await page.evaluate(exposeDepsJs({ elementIntersected }));
} catch (e: any) {
if (config.debugMode) {
error(e.message + '. Ignored because normally it means that function already exposed');
}
}

await page.evaluate(exposeDepsJs({ isElementVisible }));
await page.evaluate(exposeDepsJs({ highestZIndex }));
await page.evaluate(exposeDepsJs({ elementIntersected }));

for (const result of resultByUrl.violations) {
for (const node of result.nodes) {
const alreadyScreenshotedImage = savedScreenshotHtmls.get(node.html);
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/mark-all-events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Config } from './../models/config';
import { Page, Protocol } from 'puppeteer';
import { debug } from './helper-functions';

export async function markAllEvents(page: Page): Promise<{ [key: string]: string[] }> {
export async function markAllEvents(page: Page, config: Config): Promise<{ [key: string]: string[] }> {
debug(config.debugMode, 'mark all events for ' + page.url());
const allElements: string[] = JSON.parse(
await page.evaluate(() => {
const allElements = Array.from(document.querySelectorAll('*'));
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/setup-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ describe('setup-config', () => {
const config = setupConfig({ config: 'tests/setup-config/config_setup_config.json' });
prepareWorkspace(config);
expect(fs.existsSync('results/' + getEscaped(config.name))).toBe(true);
fs.rmdirSync('results', { recursive: true });
fs.rmSync('results', { recursive: true });
});

test('prepare-workspace with folder mentioned and save images true', async () => {
const config = setupConfig({ config: 'tests/setup-config/config_with_images_and_results.json' });
prepareWorkspace(config);
expect(fs.existsSync('tests/results/' + getEscaped(config.name))).toBe(true);
expect(fs.existsSync('tests/results/' + getEscaped(config.name) + '/images')).toBe(true);
fs.rmdirSync('tests/results', { recursive: true });
fs.rmSync('tests/results', { recursive: true });
});

test('prepare-workspace with folder mentioned and save images true and image folder already here', async () => {
Expand All @@ -108,7 +108,7 @@ describe('setup-config', () => {
prepareWorkspace(config);
expect(fs.existsSync('tests/results/' + getEscaped(config.name))).toBe(true);
expect(fs.existsSync('tests/results/' + getEscaped(config.name) + '/images')).toBe(true);
fs.rmdirSync('tests/results', { recursive: true });
fs.rmSync('tests/results', { recursive: true });
} else {
fail();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/setup-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function prepareWorkspace(config: Config): void {
if (config.imagesPath && !fs.existsSync(config.imagesPath) && config.saveImages) {
fs.mkdirSync(config.imagesPath, { recursive: true });
} else if (config.imagesPath && config.saveImages) {
fs.rmdirSync(config.imagesPath, { recursive: true });
fs.rmSync(config.imagesPath, { recursive: true });
fs.mkdirSync(config.imagesPath, { recursive: true });
}
if (config.resultsPathPerUrl && !fs.existsSync(config.resultsPathPerUrl)) {
Expand Down

0 comments on commit 365a8ce

Please sign in to comment.