This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
99 lines (84 loc) · 4.13 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const dotenv = require('dotenv');
const fs = require('fs').promises;
const logger = require('pino')({ level: process.env.LOG_LEVEL || 'info' });
const puppeteer = require('puppeteer');
const _ = require('underscore');
dotenv.config();
(async () => {
const username = process.env.ESSELUNGA_USERNAME;
const password = process.env.ESSELUNGA_PASSWORD;
const homeUrl = 'https://www.esselungaacasa.it/';
const loginSelector = 'esselunga-welcome-header a[ng-click="$ctrl.login()"]';
const usernameSelector = '#loginForm #gw_username';
const passwordSelector = '#loginForm #gw_password';
const loginButtonSelector = '#loginForm button[type="submit"]';
const checkoutSelector = 'esselunga-checkout-widget #cassa';
//const modalDialogOpenSelector = '#remodalDialog.remodal-is-opened';
const modalDialogClosedSelector = '#remodalDialog.remodal-is-closed';
const checkoutNextStepSelector = 'esselunga-checkout-wizard-navigator #checkoutNextStep';
const selectSlotSelector = 'esselunga-slots input[type="radio"]';
const selectAvailableSlotSelector = 'esselunga-slots input[type="radio"].disponibile';
const nextSlotSelector = 'esselunga-slots .selezione a[ng-click="slotCtrl.next()"]';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(homeUrl);
logger.debug(homeUrl);
await page.waitForSelector(loginSelector);
await page.click(loginSelector);
logger.debug(loginSelector);
await page.waitForSelector(usernameSelector);
await page.waitForSelector(passwordSelector);
await page.waitForSelector(loginButtonSelector);
await page.type(usernameSelector, username);
await page.type(passwordSelector, password);
await page.click(loginButtonSelector);
logger.debug(loginButtonSelector);
await page.waitForNavigation({ waitUntil: 'networkidle2' });
await page.waitForSelector(checkoutSelector);
await page.click(checkoutSelector);
logger.debug(checkoutSelector);
//await page.waitForSelector(modalDialogOpenSelector);
//logger.debug(modalDialogOpenSelector);
await page.waitForSelector(modalDialogClosedSelector);
logger.debug(modalDialogClosedSelector);
await page.waitForSelector(checkoutNextStepSelector);
await page.click(checkoutNextStepSelector);
logger.debug('step 1: %s', page.url());
//await page.waitForSelector(modalDialogOpenSelector);
//logger.debug(modalDialogOpenSelector);
await page.waitForSelector(modalDialogClosedSelector);
logger.debug(modalDialogClosedSelector);
await page.waitForSelector(nextSlotSelector);
for (let i = 0; i < 7; i++) {
await page.click(nextSlotSelector);
}
await page.waitForSelector(selectSlotSelector);
let classNames = await page.$$eval(selectSlotSelector, inputs => inputs.map(input => input.className));
let countByClassName = _.countBy(classNames, _.identity);
logger.info(countByClassName);
logger.debug('step 2: %s', page.url());
if (countByClassName['disponibile']) {
let pageContent = await page.content();
await fs.writeFile('content-' + new Date().toISOString(), pageContent);
await page.waitForSelector(selectAvailableSlotSelector);
await page.click(selectAvailableSlotSelector);
await page.waitForSelector(checkoutNextStepSelector);
await page.click(checkoutNextStepSelector);
//await page.waitForSelector(modalDialogOpenSelector);
//logger.debug(modalDialogOpenSelector);
await page.waitForSelector(modalDialogClosedSelector);
logger.debug(modalDialogClosedSelector);
logger.debug('step 3: %s', page.url());
await page.waitForSelector('a.btn.fast-checkout');
await page.click('a.btn.fast-checkout');
logger.debug('click a.btn.fast-checkout');
//await page.waitForSelector(modalDialogOpenSelector);
//logger.debug(modalDialogOpenSelector);
await page.waitForSelector(modalDialogClosedSelector);
logger.debug(modalDialogClosedSelector);
logger.debug('step 4: %s', page.url());
}
await browser.close();
})().catch(reason => {
logger.error(reason);
});