-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (127 loc) · 5.03 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const {
openBrowser, client, goto, waitFor, textBox, link, into, write, click, $, closeBrowser, goBack
} = require('taiko');
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
class MonthPayslipSummaryPage {
DOWNLOAD_ICON_SELECTOR = `//i[contains(@class, 'fa-download')]`;
async download(downloadPath, filename) {
await click($(this.DOWNLOAD_ICON_SELECTOR), {waitForNavigation: true});
// TODO can we do something so that we don't have to wait for 2 seconds to make sure the file is downloaded
await waitFor(2000);
fs.renameSync(downloadPath + '/payslip.pdf', downloadPath + '/' + filename + '.pdf');
console.log(`✅ ${filename}`);
await goBack();
}
}
class PayslipsListPage {
YEARS_DROPDOWN_SELECTOR = '.lh_date_dropdown_link';
YEARS_DROPDOWN_ROWS_XPATH = '//*[@id="abc1"]/div/div/div/div[1]/div/div[1]/div/div/h4/div/ul/li/a';
DASHBOARD_PAYSLIPS_SELECTOR = "//div[contains(@class, 'dashpaysliphis')]";
async open() {
await click($(this.DASHBOARD_PAYSLIPS_SELECTOR));
}
async download(downloadPath) {
const yearRows = await this.yearRowList();
await this.toggleYearDropdown(); // close the year dropdown
// For each row in the dropdown
for (let i = 0; i < yearRows.length; i++) {
await this.downloadPayslips(downloadPath, i);
}
}
async toggleYearDropdown() {
await click($(this.YEARS_DROPDOWN_SELECTOR), {waitForNavigation: true});
}
async yearRowList() {
await this.toggleYearDropdown();
return await $(this.YEARS_DROPDOWN_ROWS_XPATH).elements();
}
async navigateToYearIndex(rowIndex) {
await click((await this.yearRowList())[rowIndex], {waitForNavigation: true});
}
async downloadPayslips(downloadPath, yearRowIndex) {
await this.navigateToYearIndex(yearRowIndex);
const rows = await (await link('Quick View')).elements();
const monthYearDollarWrapper = await $('div.ps_col1');
const monthYearList = await monthYearDollarWrapper.elements();
for (let i = 0; i < rows.length; i++) {
await this.navigateToYearIndex(yearRowIndex)
const links = await link('Quick View')
const tmp = await links.elements();
await click(tmp[i], {waitForNavigation: true});
await waitFor(500);
const monthYearText = await monthYearList[i].text();
const monthYear = monthYearText.split("-");
await new MonthPayslipSummaryPage().download(downloadPath, `${monthYear[1]}-${monthNameToNumber(monthYear[0])}`);
}
}
}
function monthNameToNumber(monthName) {
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const number = months.indexOf(monthName) + 1;
if (number < 10) return `0${number}` ;
return number;
}
class Paybooks {
async login(username, password, domain) {
const loginPage = new LoginPage(username, password, domain);
await loginPage.login();
}
async downloadAllPayslips(downloadPath) {
const payslipsPage = new PayslipsListPage();
await payslipsPage.open()
await payslipsPage.download(downloadPath);
}
}
class LoginPage {
LOGIN_URL = 'https://apps.paybooks.in/mylogin.aspx';
username;
password;
domain;
constructor(username, password, domain) {
this.username = username;
this.password = password;
this.domain = domain;
}
async login() {
await goto(this.LOGIN_URL);
await write(this.username, into(textBox({name: 'txtUserName'})));
await write(this.password, into(textBox({name: 'txtPassword'})));
await write(this.domain, into(textBox({name: 'txtDomain'})));
await click("submit");
}
}
(async () => {
const PASS = process.env.PAYBOOKS_PASSWORD
const USER = process.env.PAYBOOKS_USERNAME
const DOMAIN = process.env.PAYBOOKS_DOMAIN
// exit if PASS, USER, DOMAIN empty
if (!PASS || !USER || !DOMAIN) {
console.log("PAYBOOKS_USERNAME, PAYBOOKS_PASSWORD or PAYBOOKS_DOMAIN env var not present")
process.exit(-1);
}
// let LAUNCH_HEADLESS = true;
// if (process.argv[2] === "--headful") {
// LAUNCH_HEADLESS = false;
// console.log("Launching in headful mode")
// }
const downloadPath = path.resolve(__dirname, 'data', 'downloaded');
mkdirp(downloadPath);
console.log("Download path set to " + downloadPath);
try {
// Set up browser
await openBrowser({headless: false});
await client().send('Page.setDownloadBehavior', {
behavior: 'allow', downloadPath: downloadPath,
});
const paybooks = new Paybooks()
await paybooks.login(USER, PASS, DOMAIN)
await paybooks.downloadAllPayslips(downloadPath);
} catch (error) {
console.error(error);
} finally {
console.log(downloadPath);
await closeBrowser();
}
})();