diff --git a/src/scrapers/base-scraper-with-browser.js b/src/scrapers/base-scraper-with-browser.js index 4be4a291..25896d5c 100644 --- a/src/scrapers/base-scraper-with-browser.js +++ b/src/scrapers/base-scraper-with-browser.js @@ -9,21 +9,29 @@ const VIEWPORT_WIDTH = 1024; const VIEWPORT_HEIGHT = 768; const OK_STATUS = 200; -function getKeyByValue(object, value) { - return Object.keys(object).find((key) => { - const compareTo = object[key]; - let result = false; - - result = compareTo.find((item) => { - if (item instanceof RegExp) { - return item.test(value); +async function getKeyByValue(object, value) { + const keys = Object.keys(object); + for (const key of keys) { + const conditions = object[key]; + + for (const condition of conditions) { + let result = false; + + if (condition instanceof RegExp) { + result = condition.test(value); + } else if (typeof condition === 'function') { + result = await condition(); + } else { + result = value.toLowerCase() === condition.toLowerCase(); } - return value.toLowerCase() === item.toLowerCase(); - }); + if (result) { + return Promise.resolve(key); + } + } + } - return !!result; - }); + return Promise.resolve(LOGIN_RESULT.UNKNOWN_ERROR); } function handleLoginResult(scraper, loginResult) { @@ -32,6 +40,7 @@ function handleLoginResult(scraper, loginResult) { scraper.emitProgress(SCRAPE_PROGRESS_TYPES.LOGIN_SUCCESS); return { success: true }; case LOGIN_RESULT.INVALID_PASSWORD: + case LOGIN_RESULT.UNKNOWN_ERROR: scraper.emitProgress(SCRAPE_PROGRESS_TYPES.LOGIN_FAILED); return { success: false, @@ -132,7 +141,7 @@ class BaseScraperWithBrowser extends BaseScraper { } const current = await getCurrentUrl(this.page, true); - const loginResult = getKeyByValue(loginOptions.possibleResults, current); + const loginResult = await getKeyByValue(loginOptions.possibleResults, current); return handleLoginResult(this, loginResult); } diff --git a/src/scrapers/leumi-card.js b/src/scrapers/leumi-card.js index 34b513c0..47fbdc48 100644 --- a/src/scrapers/leumi-card.js +++ b/src/scrapers/leumi-card.js @@ -29,10 +29,14 @@ const TWO_MONTHS_POSTPONED_TYPE_NAME = 'דחוי חודשיים'; const MONTHLY_CHARGE_PLUS_INTEREST_TYPE_NAME = 'חודשי + ריבית'; const CREDIT_TYPE_NAME = 'קרדיט'; +const INVALID_DETAILS_SELECTOR = '#popupWrongDetails'; +const LOGIN_ERROR_SELECTOR = '#popupCardHoldersLoginError'; + function redirectOrDialog(page) { return Promise.race([ waitForRedirect(page, 20000, false, [BASE_WELCOME_URL, `${BASE_WELCOME_URL}/`]), - waitUntilElementFound(page, '#popupWrongDetails', true), + waitUntilElementFound(page, INVALID_DETAILS_SELECTOR, true), + waitUntilElementFound(page, LOGIN_ERROR_SELECTOR, true), ]); } @@ -311,11 +315,16 @@ async function fetchTransactions(browser, options, navigateToFunc) { return allResults; } -function getPossibleLoginResults() { +function getPossibleLoginResults(page) { const urls = {}; urls[LOGIN_RESULT.SUCCESS] = [`${BASE_WELCOME_URL}/homepage/personal`]; urls[LOGIN_RESULT.CHANGE_PASSWORD] = [`${BASE_ACTIONS_URL}/Anonymous/Login/PasswordExpired.aspx`]; - urls[LOGIN_RESULT.INVALID_PASSWORD] = [`${BASE_ACTIONS_URL}/Anonymous/Login/CardHoldersLogin.aspx`]; + urls[LOGIN_RESULT.INVALID_PASSWORD] = [async () => { + return elementPresentOnPage(page, INVALID_DETAILS_SELECTOR); + }]; + urls[LOGIN_RESULT.UNKNOWN_ERROR] = [async () => { + return elementPresentOnPage(page, LOGIN_ERROR_SELECTOR); + }]; return urls; } @@ -339,7 +348,7 @@ class LeumiCardScraper extends BaseScraperWithBrowser { } }, postAction: async () => redirectOrDialog(this.page), - possibleResults: getPossibleLoginResults(), + possibleResults: getPossibleLoginResults(this.page), }; }