-
Notifications
You must be signed in to change notification settings - Fork 1
/
cetesdirecto.js
72 lines (58 loc) · 2.47 KB
/
cetesdirecto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { sanitize } from './helpers.js';
const selectors = {
checkUsernameBtn: '#continuarBtn',
loginBtn: '#accederBtn',
logoutLink: '#btnsUserWeb [data-name="cerrarSesion"]:nth-child(4)',
passwordInput: '#pwdId',
portfolioLink: '#portafolioMenu',
instrumentNameText: '.nombreInstrumento .txtInstrumento',
instrumentValueText: '.valorInstrumento .totalInstrumento',
instruments: '.instrumento',
instrumentsTotalText: '.totalInstrumentos .totalInstrumentosNumeros .txtInstrumento',
usernameInput: '#userId',
};
const urls = {
login: 'https://www.cetesdirecto.com/SSOSVD_wls/',
portfolio: 'https://www.cetesdirecto.com/web/loadPortafolio',
};
async function login(page, username, password) {
await page.goto(urls.login);
await page.waitForSelector(selectors.usernameInput, { visible: true });
await page.type(selectors.usernameInput, username);
await page.click(selectors.checkUsernameBtn);
await page.waitForSelector(selectors.passwordInput, { visible: true });
await page.type(selectors.passwordInput, password);
await page.click(selectors.loginBtn);
}
// Session needs to be closed, otherwise you won't be able to log in again for ~15 mins
async function logout(page) {
await page.click(selectors.logoutLink);
await page.waitForSelector(selectors.usernameInput, { visible: true });
}
async function getPortfolioSummary(page) {
await page.waitForSelector(selectors.portfolioLink, { visible: true });
await page.goto(urls.portfolio);
await page.waitForSelector(selectors.instrumentsTotalText, { visible: true });
const $instrumentsTotal = await page.$(selectors.instrumentsTotalText);
const instrumentsTotalText = await page.evaluate(element => element.textContent, $instrumentsTotal);
const $instruments = await page.$$(selectors.instruments);
const instrumentsDetail = await Promise.all($instruments.map(async ($instrument) => {
const $instrumentName = await $instrument.$(selectors.instrumentNameText);
const instrumentNameText = await page.evaluate(element => element.textContent, $instrumentName);
const $instrumentValue = await $instrument.$(selectors.instrumentValueText);
const instrumentValueText = await page.evaluate(element => element.textContent, $instrumentValue);
return {
name: instrumentNameText,
value: sanitize(instrumentValueText),
};
}));
return {
instruments: instrumentsDetail,
instrumentsTotal: sanitize(instrumentsTotalText),
};
}
export {
login,
logout,
getPortfolioSummary,
};