Created
August 21, 2017 09:20
-
-
Save JarvisPrestidge/c0fbc3f1f75e10db9f6d48c07d5226e6 to your computer and use it in GitHub Desktop.
Credit card navigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Responsible for collecting credit card specific account information | |
* | |
* @param {Browser} browser | |
* @param {ILogger} logger | |
* @param {string} selector | |
*/ | |
const getCreditCardAccountInfo = async (browser: Browser, logger: ILogger, selector: string) => { | |
// Navigate to statements page | |
const statementsPageSelector = `${selector} a[id*='lnkAccName_des-m-sat-xx-']`; | |
await browser.clickAndWaitEvent(statementsPageSelector); | |
// Check if statements are available to download | |
const downloadSelector = "#lnkDSPrimaryDownloadMACC"; | |
let hasAvailableStatement = await browser.exists(downloadSelector); | |
while (!hasAvailableStatement) { | |
// Check for earlier statements | |
const earlierButtonSelector = "#lnkEarlierBtnMACC"; | |
const hasEarlierButton = await browser.exists(earlierButtonSelector); | |
if (hasEarlierButton) { | |
// Check if disabled - i.e. no further past statements | |
const earlierButtonAttribute = await browser.getAttribute(earlierButtonSelector, "class"); | |
const isDisabled = /disabled/i.test(earlierButtonAttribute); | |
if (isDisabled) { | |
throw new Error("No available credit card statements found"); | |
} | |
await browser.click(earlierButtonSelector); | |
await browser.waitForDOMReady(); | |
hasAvailableStatement = await browser.exists(downloadSelector); | |
} | |
} | |
// Creates a random temp file | |
const filePath = `${env.STORE_FILESYS_PATH}/${uuid.v4()}.pdf`; | |
try { | |
// Click and download the pdf | |
await browser.click(downloadSelector); | |
await browser.download(filePath); | |
// Parse the downloaded PDF | |
const parsePDF = new Promise<IterableIterator<string>>((resolve, reject) => { | |
const pdfParser = new PDF2JSON(this, 1); | |
// Monkey patch PDF Parser | |
pdfParser.getTexts = function* (): Iterable<string> { | |
for (const textContent of this.PDFJS.rawTextContents) { | |
for (const textObj of textContent.bidiTexts) { | |
yield textObj.str; | |
} | |
} | |
}; | |
pdfParser.on("pdfParser_dataError", (errData: any) => { | |
reject(new Error(errData.parserError)); | |
}); | |
pdfParser.on("pdfParser_dataReady", (pdfData: any) => { | |
resolve(pdfParser.getTexts()); | |
}); | |
pdfParser.loadPDF(filePath); | |
}); | |
const texts: IterableIterator<string> = await parsePDF; | |
} finally { | |
// Remove temp file | |
if (await fs.exists(filePath)) { | |
await fs.unlink(filePath); | |
} | |
} | |
return; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment