Skip to content

Commit

Permalink
custom errors, global browser refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Sep 24, 2021
1 parent 43a8f01 commit 998fac9
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 23 deletions.
3 changes: 2 additions & 1 deletion browser-global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import puppeteer from "puppeteer";
export declare const setHeadless: (headlessStatus: boolean) => void;
export declare const pageTimeoutMillis = 90000;
export declare const initializeBrowserGlobal: () => Promise<puppeteer.Browser>;
export declare const getBrowserGlobal: () => Promise<puppeteer.Browser>;
export declare const keepBrowserGlobalAlive: () => void;
export declare const cleanUpBrowserGlobal: (useForce?: boolean) => Promise<void>;
18 changes: 11 additions & 7 deletions browser-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,38 @@ export const setHeadless = (headlessStatus) => {
};
export const pageTimeoutMillis = 90000;
const browserStartupTimeoutMillis = 3 * 60000;
const browserGlobalExpiryMillis = browserStartupTimeoutMillis + (pageTimeoutMillis * 3);
const browserGlobalExpiryMillis = Math.max(browserStartupTimeoutMillis, pageTimeoutMillis) + 10000;
let browserGlobal;
let browserGlobalInitializedTime = 0;
let browserGlobalTimer;
const isBrowserGlobalExpired = () => {
if (browserGlobalInitializedTime + browserGlobalExpiryMillis < Date.now()) {
const isBrowserGlobalReady = () => {
if (browserGlobal && browserGlobalInitializedTime + browserGlobalExpiryMillis > Date.now()) {
return true;
}
return false;
};
export const initializeBrowserGlobal = async () => {
if (!browserGlobal || isBrowserGlobalExpired()) {
export const getBrowserGlobal = async () => {
if (!isBrowserGlobalReady()) {
await cleanUpBrowserGlobal();
browserGlobalInitializedTime = Date.now();
keepBrowserGlobalAlive();
browserGlobal = await puppeteer.launch({
headless,
timeout: browserStartupTimeoutMillis,
args: ["--lang-en-CA,en"]
});
keepBrowserGlobalAlive();
browserGlobalTimer = setIntervalAsync(cleanUpBrowserGlobal, browserGlobalExpiryMillis);
}
return browserGlobal;
};
export const keepBrowserGlobalAlive = () => {
browserGlobalInitializedTime = Date.now();
};
export const cleanUpBrowserGlobal = async (useForce = false) => {
if (useForce) {
browserGlobalInitializedTime = 0;
}
if (browserGlobal && isBrowserGlobalExpired()) {
if (!isBrowserGlobalReady()) {
try {
await browserGlobal.close();
}
Expand Down
21 changes: 13 additions & 8 deletions browser-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,54 @@ export const setHeadless = (headlessStatus: boolean): void => {
export const pageTimeoutMillis = 90_000;
const browserStartupTimeoutMillis = 3 * 60_000;

const browserGlobalExpiryMillis = browserStartupTimeoutMillis + (pageTimeoutMillis * 3);
const browserGlobalExpiryMillis = Math.max(browserStartupTimeoutMillis, pageTimeoutMillis) + 10_000;

let browserGlobal: puppeteer.Browser;
let browserGlobalInitializedTime = 0;
let browserGlobalTimer: SetIntervalAsyncTimer;

const isBrowserGlobalExpired = () => {
const isBrowserGlobalReady = () => {

if (browserGlobalInitializedTime + browserGlobalExpiryMillis < Date.now()) {
if (browserGlobal && browserGlobalInitializedTime + browserGlobalExpiryMillis > Date.now()) {
return true;
}

return false;
};

export const initializeBrowserGlobal = async (): Promise<puppeteer.Browser> => {
export const getBrowserGlobal = async (): Promise<puppeteer.Browser> => {

if (!browserGlobal || isBrowserGlobalExpired()) {
if (!isBrowserGlobalReady()) {

await cleanUpBrowserGlobal();

browserGlobalInitializedTime = Date.now();
keepBrowserGlobalAlive();

browserGlobal = await puppeteer.launch({
headless,
timeout: browserStartupTimeoutMillis,
args: ["--lang-en-CA,en"]
});

keepBrowserGlobalAlive();

browserGlobalTimer = setIntervalAsync(cleanUpBrowserGlobal, browserGlobalExpiryMillis);
}

return browserGlobal;
};

export const keepBrowserGlobalAlive = (): void => {
browserGlobalInitializedTime = Date.now();
};

export const cleanUpBrowserGlobal = async (useForce = false): Promise<void> => {

if (useForce) {
browserGlobalInitializedTime = 0;
}

if (browserGlobal && isBrowserGlobalExpired()) {
if (!isBrowserGlobalReady()) {

try {
await browserGlobal.close();
Expand All @@ -79,7 +84,7 @@ export const cleanUpBrowserGlobal = async (useForce = false): Promise<void> => {
} catch {
// ignore
}

browserGlobalTimer = undefined;
}

Expand Down
2 changes: 2 additions & 0 deletions config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export declare const clearanceStart_url = "https://onlineservices.wsib.on.ca/Cle
export declare const clearanceStart_searchFormSelector = "#TOKENSimpleSearchForm";
export declare const clearanceStart_searchFieldSelector = "#simpleAccountNumbersTOKEN";
export declare const clearanceResult_certificateLinkSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable a[rel='eClearanceWorkspaceContent'][href^='GCSearchCertDet']";
export declare const clearanceResult_certificateBadStandingSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding";
export declare const clearanceResult_defaultErrorMessage = "Clearance certificate link not found.";
export declare const certificate_tableSelector = "#eClearanceWorkspaceDivForm .fancytable";
export declare const certificateField_contractorLegalTradeName = "Contractor Legal / Trade Name";
export declare const certificateField_contractorAddress = "Contractor Address";
Expand Down
2 changes: 2 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const clearanceStart_url = "https://onlineservices.wsib.on.ca/Clearances/
export const clearanceStart_searchFormSelector = "#TOKENSimpleSearchForm";
export const clearanceStart_searchFieldSelector = "#simpleAccountNumbersTOKEN";
export const clearanceResult_certificateLinkSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable a[rel='eClearanceWorkspaceContent'][href^='GCSearchCertDet']";
export const clearanceResult_certificateBadStandingSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding";
export const clearanceResult_defaultErrorMessage = "Clearance certificate link not found.";
export const certificate_tableSelector = "#eClearanceWorkspaceDivForm .fancytable";
export const certificateField_contractorLegalTradeName = "Contractor Legal / Trade Name";
export const certificateField_contractorAddress = "Contractor Address";
Expand Down
2 changes: 2 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const clearanceStart_searchFieldSelector = "#simpleAccountNumbersTOKEN";
// Search Results

export const clearanceResult_certificateLinkSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable a[rel='eClearanceWorkspaceContent'][href^='GCSearchCertDet']";
export const clearanceResult_certificateBadStandingSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding";
export const clearanceResult_defaultErrorMessage = "Clearance certificate link not found.";

// Certificate

Expand Down
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const cleanRawCertificateOutput = (rawOutput) => {
export const getClearanceByAccountNumber = async (accountNumber) => {
let page;
try {
const browser = await browserGlobal.initializeBrowserGlobal();
const browser = await browserGlobal.getBrowserGlobal();
page = await browser.newPage();
page.setDefaultNavigationTimeout(browserGlobal.pageTimeoutMillis);
page.setDefaultTimeout(browserGlobal.pageTimeoutMillis);
Expand All @@ -41,20 +41,35 @@ export const getClearanceByAccountNumber = async (accountNumber) => {
if (!pageResponse.ok) {
throw new Error("Response Code = " + pageResponse.status().toString());
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector("body");
await page.$eval(config.clearanceStart_searchFieldSelector, (inputEle, accountNumber_value) => {
inputEle.value = accountNumber_value;
}, accountNumber);
await page.$eval(config.clearanceStart_searchFormSelector, (formEle) => {
formEle.submit();
});
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector("body");
let hasError = false;
await page.$eval(config.clearanceResult_certificateLinkSelector, (linkEle) => {
linkEle.click();
})
.catch(() => {
throw new Error("Clearance certificate link not found.");
hasError = true;
});
if (hasError) {
const errorMessage = await page.$eval(config.clearanceResult_certificateBadStandingSelector, (badStandingEle) => {
return badStandingEle
? badStandingEle.textContent
: config.clearanceResult_defaultErrorMessage;
})
.catch(() => {
throw new Error(config.clearanceResult_defaultErrorMessage);
});
throw new Error(errorMessage);
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector("body");
const certificateURL = page.url();
const parsedTable = await page.$eval(config.certificate_tableSelector, (tableEle) => {
Expand Down
23 changes: 21 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const getClearanceByAccountNumber = async (accountNumber: string): Promis
let page: puppeteer.Page;

try {
const browser = await browserGlobal.initializeBrowserGlobal();
const browser = await browserGlobal.getBrowserGlobal();

page = await browser.newPage();

Expand All @@ -70,6 +70,7 @@ export const getClearanceByAccountNumber = async (accountNumber: string): Promis
throw new Error("Response Code = " + pageResponse.status().toString());
}

browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector("body");

// Fill out form
Expand All @@ -82,17 +83,35 @@ export const getClearanceByAccountNumber = async (accountNumber: string): Promis
formEle.submit();
});

browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector("body");

// Find result link

let hasError = false;

await page.$eval(config.clearanceResult_certificateLinkSelector, (linkEle: HTMLAnchorElement) => {
linkEle.click();
})
.catch(() => {
throw new Error("Clearance certificate link not found.");
hasError = true;
});

if (hasError) {

const errorMessage = await page.$eval(config.clearanceResult_certificateBadStandingSelector, (badStandingEle: HTMLElement) => {
return badStandingEle
? badStandingEle.textContent
: config.clearanceResult_defaultErrorMessage;
})
.catch(() => {
throw new Error(config.clearanceResult_defaultErrorMessage);
});

throw new Error(errorMessage);
}

browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector("body");

// Parse the certificate
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cityssm/wsib-clearance-check",
"version": "3.0.0-beta",
"version": "3.0.0-beta.1",
"description": "A tool to scrape the clearance certificate status from the WSIB Online Services website.",
"keywords": [
"wsib",
Expand Down

0 comments on commit 998fac9

Please sign in to comment.