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' ;
9
2
10
3
const urls = {
11
4
home : 'https://www.bbva.mx/' ,
@@ -72,14 +65,18 @@ async function login(page, cardNumber, password, otp) {
72
65
}
73
66
74
67
// 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
+
76
71
await homeContentFrameA . click ( selectors . logoutBtn ) ;
77
72
78
73
// TODO: Fix selector
79
74
await page . waitForSelector ( selectors . acceptLogoutLink , { visible : true } ) ;
80
75
}
81
76
82
- async function getSummary ( homeContentFrameA ) {
77
+ async function getSummary ( page ) {
78
+ const homeContentFrameA = await getHomeContentFrameFromPage ( page ) ;
79
+
83
80
await homeContentFrameA . waitForSelector ( selectors . homeFrameB , { visible : true } ) ;
84
81
85
82
const $homeFrameB = await homeContentFrameA . $ ( selectors . homeFrameB ) ;
@@ -102,51 +99,23 @@ async function getSummary(homeContentFrameA) {
102
99
103
100
return {
104
101
title : productCardTitleText ,
105
- amount : productCardAmountText ,
102
+ amount : sanitize ( productCardAmountText ) ,
106
103
} ;
107
104
} ) ) ;
108
105
109
106
return productStatuses . filter ( ( productStatus ) => productStatus !== null ) ;
110
107
}
111
108
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 ) {
138
110
// The BBVA homepage has an iframe inside a frame for whatever reason...
139
111
const $homeFrameA = await page . $ ( selectors . homeFrameA ) ;
140
112
const homeContentFrameA = await $homeFrameA . contentFrame ( ) ;
141
113
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 ;
148
115
}
149
116
150
- ( async ( ) => {
151
- await main ( ) ;
152
- } ) ( ) ;
117
+ export {
118
+ login ,
119
+ logout ,
120
+ getSummary ,
121
+ } ;
0 commit comments