Skip to content

Commit

Permalink
fixed scan idx and page close detection (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thumster authored Nov 2, 2023
1 parent 58b9149 commit b6cf769
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
11 changes: 5 additions & 6 deletions crawlers/custom/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ export const runAxeScan = async (
});
};

export const processPage = async (
page,
{
export const processPage = async (page, processPageParams) => {
// make sure to update processPageParams' scannedIdx
processPageParams.scannedIdx += 1;
const {
scannedIdx,
needsReviewItems,
blacklistedPatterns,
Expand All @@ -130,8 +131,7 @@ export const processPage = async (
intermediateScreenshotsPath,
urlsCrawled,
randomToken,
},
) => {
} = processPageParams;
try {
await page.waitForLoadState('networkidle', { timeout: 10000 });
await page.waitForLoadState('domcontentloaded');
Expand Down Expand Up @@ -168,7 +168,6 @@ export const processPage = async (
y: window.scrollY,
}));

scannedIdx += 1;
const pageImagePath = await screenshotFullPage(page, intermediateScreenshotsPath, scannedIdx);

guiInfoLog(guiInfoStatusTypes.SCANNED, {
Expand Down
16 changes: 14 additions & 2 deletions crawlers/runCustom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-env browser */

import { chromium } from 'playwright';
import { createCrawleeSubFolders } from './commonCrawlerFunc.js';
import { cleanUp } from '../utils.js';
Expand Down Expand Up @@ -63,7 +62,20 @@ const runCustom = async (
const page = await context.newPage();
await page.goto(url);

await Promise.all(pageClosePromises);
// to execute and wait for all pages to close
// idea is for promise to be pending until page.on('close') detected
const allPagesClosedPromise = async promises =>
Promise.all(promises)
// necessary to recheck as during time of execution, more pages added
.then(() => {
if (Object.keys(pagesDict).length > 0) {
return allPagesClosedPromise(promises);
}

return Promise.resolve(true);
});

await allPagesClosedPromise(pageClosePromises);
} catch (error) {
log('PLAYWRIGHT EXECUTION ERROR', error);
process.exit(1);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b6cf769

Please sign in to comment.