Skip to content

Commit 40df590

Browse files
committedMar 1, 2021
(bbva) Extract example code from library
1 parent 5128648 commit 40df590

File tree

2 files changed

+67
-46
lines changed

2 files changed

+67
-46
lines changed
 

‎bbva-example.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import puppeteer from 'puppeteer';
2+
3+
import {
4+
login,
5+
logout,
6+
getSummary,
7+
} from './bbva.js';
8+
9+
const {
10+
BBVA_CARD_NUMBER,
11+
BBVA_PASSWORD,
12+
BBVA_OTP,
13+
PUPPETEER_HEADLESS = 'true',
14+
} = process.env;
15+
16+
async function main() {
17+
if (!BBVA_CARD_NUMBER) {
18+
console.error(`BBVA_CARD_NUMBER was not set!`);
19+
process.exit(1);
20+
}
21+
22+
if (!BBVA_PASSWORD) {
23+
console.error(`BBVA_PASSWORD was not set!`);
24+
process.exit(1);
25+
}
26+
27+
if (!BBVA_OTP) {
28+
console.error(`BBVA_OTP was not set!`);
29+
process.exit(1);
30+
}
31+
32+
const browser = await puppeteer.launch({
33+
headless: PUPPETEER_HEADLESS === 'true',
34+
});
35+
const page = await browser.newPage();
36+
37+
// Set a wide viewport so that all product cards are visible and interaction is simpler
38+
await page.setViewport({ width: 1280, height: 800 });
39+
40+
await login(page, BBVA_CARD_NUMBER, BBVA_PASSWORD, BBVA_OTP);
41+
42+
const summary = await getSummary(page);
43+
44+
console.log('BBVA summary:', summary);
45+
46+
await logout(page);
47+
await browser.close();
48+
}
49+
50+
(async () => {
51+
await main();
52+
})();

‎bbva.js

+15-46
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import puppeteer from 'puppeteer';
2-
3-
const {
4-
BBVA_CARD_NUMBER,
5-
BBVA_PASSWORD,
6-
BBVA_OTP,
7-
PUPPETEER_HEADLESS = 'true',
8-
} = process.env;
1+
import { sanitize } from './helpers.js';
92

103
const urls = {
114
home: 'https://www.bbva.mx/',
@@ -72,14 +65,18 @@ async function login(page, cardNumber, password, otp) {
7265
}
7366

7467
// Session needs to be closed, otherwise you won't be able to log in again for ~15 mins
75-
async function logout(page, homeContentFrameA) {
68+
async function logout(page) {
69+
const homeContentFrameA = await getHomeContentFrameFromPage(page);
70+
7671
await homeContentFrameA.click(selectors.logoutBtn);
7772

7873
// TODO: Fix selector
7974
await page.waitForSelector(selectors.acceptLogoutLink, { visible: true });
8075
}
8176

82-
async function getSummary(homeContentFrameA) {
77+
async function getSummary(page) {
78+
const homeContentFrameA = await getHomeContentFrameFromPage(page);
79+
8380
await homeContentFrameA.waitForSelector(selectors.homeFrameB, { visible: true });
8481

8582
const $homeFrameB = await homeContentFrameA.$(selectors.homeFrameB);
@@ -102,51 +99,23 @@ async function getSummary(homeContentFrameA) {
10299

103100
return {
104101
title: productCardTitleText,
105-
amount: productCardAmountText,
102+
amount: sanitize(productCardAmountText),
106103
};
107104
}));
108105

109106
return productStatuses.filter((productStatus) => productStatus !== null);
110107
}
111108

112-
async function main() {
113-
if (!BBVA_CARD_NUMBER) {
114-
console.error(`BBVA_CARD_NUMBER was not set!`);
115-
process.exit(1);
116-
}
117-
118-
if (!BBVA_PASSWORD) {
119-
console.error(`BBVA_PASSWORD was not set!`);
120-
process.exit(1);
121-
}
122-
123-
if (!BBVA_OTP) {
124-
console.error(`BBVA_OTP was not set!`);
125-
process.exit(1);
126-
}
127-
128-
const browser = await puppeteer.launch({
129-
headless: PUPPETEER_HEADLESS === 'true',
130-
});
131-
const page = await browser.newPage();
132-
133-
// Set a wide viewport so that all product cards are visible and interaction is simpler
134-
await page.setViewport({ width: 1280, height: 800 });
135-
136-
await login(page, BBVA_CARD_NUMBER, BBVA_PASSWORD, BBVA_OTP);
137-
109+
async function getHomeContentFrameFromPage(page) {
138110
// The BBVA homepage has an iframe inside a frame for whatever reason...
139111
const $homeFrameA = await page.$(selectors.homeFrameA);
140112
const homeContentFrameA = await $homeFrameA.contentFrame();
141113

142-
const summary = await getSummary(homeContentFrameA);
143-
144-
console.log('BBVA summary:', summary);
145-
146-
await logout(page, homeContentFrameA);
147-
await browser.close();
114+
return homeContentFrameA;
148115
}
149116

150-
(async () => {
151-
await main();
152-
})();
117+
export {
118+
login,
119+
logout,
120+
getSummary,
121+
};

0 commit comments

Comments
 (0)