-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
257 lines (198 loc) · 7.23 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const config = require('./config');
const puppeteer = require('puppeteer');
(async () => {
console.log('===================>>> Running on', new Date());
const browser = await puppeteer.launch({
args: ['--no-sandbox'],
headless: config.isProduction,
});
try {
for (credentials of config.credentials) {
console.log(credentials);
let page = await doLogin(browser, credentials);
await sleep(1000);
await doSignOff(browser, page);
await markAttandance(browser, page);
if (credentials.markApproveAttendance) {
await approveAttendance(browser, page);
await approveClockIn(browser, page);
}
if (credentials.approveLeaves) {
await approveLeaves(browser, page);
}
await doLogOut(page);
}
} catch (e) {
console.log(e);
} finally {
await browser.close();
console.log('===================>>> Completed on', new Date());
}
})();
async function doSignOff(browser, page) {
try {
let isSignOffExist = await page.evaluate(() => {
let el = document.querySelector(".policies_sign_off")
return !!el
});
if (isSignOffExist) {
console.log("Doing signoffs");
let maxSignOff = 10;
let attribute = await getSignOffAttribute(page);
await sleep(2000);
while (attribute && maxSignOff--) {
try {
await page.click('#'+attribute);
} catch (error) {
console.log("The element didn't appear.")
}
await sleep(2000);
attribute = await getSignOffAttribute(page);
await sleep(2000);
}
}
} catch(err) {
console.log('error while marking signoff');
console.log(err);
}
}
async function getSignOffAttribute(page) {
return await page.evaluate(() => {
let attr = null;
if (document.querySelector('.policies_sign_off .btn.ripple')) {
attr = document.querySelector('.policies_sign_off .btn.ripple').parentElement.getAttribute('resource');
if (attr) {
document.querySelector(".policies_sign_off .btn.ripple").click();
}
let isConfirmation = document.querySelector('.isAgreeCheckbox');
if (isConfirmation) {
isConfirmation.click();
}
}
return attr;
});
}
async function doLogin(browser, credentials) {
const page = await browser.newPage();
const loginPageUrl = `${config.baseUrl}/user/login`;
await page.goto(loginPageUrl);
await page.waitForFunction('document.readyState === "complete"');
await page.$eval('#UserLogin_username', (el, credentials) => el.value = credentials.email, credentials);
await page.$eval('#UserLogin_password', (el, credentials) => el.value = credentials.password, credentials);
await page.click('#login-submit');
await page.waitForFunction('document.readyState === "complete"');
return page;
}
async function doLogOut(page) {
const url = `${config.baseUrl}/user/logout`;
await page.goto(url);
await page.waitForFunction('document.readyState === "complete"');
console.log('Logout done');
}
async function markAttandance(browser, page) {
const url = `${config.baseUrl}/attendance#`;
try {
console.log('marking attendance');
await page.goto(url);
await page.waitForFunction('document.readyState === "complete"');
await sleep(1000);
const button = await page.$('#attendance_request');
await button.evaluate(b => b.click());
await sleep(2000);
await page.select('#AttendanceRequestForm_request_type', '2');
await page.select('#reasons_value', 'a62df79c2a2839');
await page.select('#AttendanceRequestForm_clock_out_hrs', '20');
await page.$eval('#AttendanceRequestForm_message', el => el.value = '.');
const submitButton = await page.$('#add_request_btn');
await submitButton.evaluate(b => b.click());
await sleep(2000);
} catch(err) {
console.log('error while marking attendance');
console.log(err);
}
}
async function approveClockIn(browser, page) {
const url = `${config.baseUrl}/tasksApi/GetTasks`;
try {
await navigateToPage(page, url, 1000);
const attendanceRequest = await page.$('#attendance');
if (!attendanceRequest) {
return;
}
await attendanceRequest.evaluate(b => b.click());
await sleep(1000);
await page.evaluate(() => {
document.querySelector(".bulk-check").click();
});
await page.evaluate(() => {
document.querySelector(".open_filter[data-action='Approve']").click();
});
await sleep(2000);
await page.evaluate(() => {
document.querySelector(".sidebar-form .sidebar-actions .request-action .btn").click();
});
await sleep(10000);
} catch(err) {
console.log(err);
}
}
async function approveAttendance(browser, page) {
const url = `${config.baseUrl}/tasksApi/GetTasks`;
try {
await navigateToPage(page, url, 1000);
const attendanceRequest = await page.$('#attendance_request');
if (!attendanceRequest) {
return;
}
await attendanceRequest.evaluate(b => b.click());
await sleep(1000);
await page.evaluate(() => {
document.querySelector(".bulk-select-cell > .select-all").click();
});
await page.evaluate(() => {
document.querySelector(".action-button[data-action='approve_request']").click();
});
await sleep(2000);
await page.evaluate(() => {
document.querySelector(".sidebar-form > .sidebar-actions > div > button").click();
});
await sleep(10000);
} catch(err) {
console.log(err);
}
}
async function approveLeaves(browser, page) {
const url = `${config.baseUrl}/tasksApi/GetTasks`;
try {
await navigateToPage(page, url, 1000);
const leaveRequest = await page.$('#leave_task');
if (!leaveRequest) {
return;
}
await leaveRequest.evaluate(b => b.click());
await sleep(1000);
await page.evaluate(() => {
document.querySelector(".bulk-select-cell > .select-all").click();
});
await page.evaluate(() => {
document.querySelector(".action-button[data-action='approve_request']").click();
});
await sleep(2000);
await page.evaluate(() => {
document.querySelector(".sidebar-form > .sidebar-actions > div > button").click();
});
await sleep(10000);
} catch(err) {
console.log(err);
}
}
async function navigateToPage(page, url, sleepTime) {
await page.goto(url);
await page.waitForFunction('document.readyState === "complete"');
if (sleepTime) {
await sleep(sleepTime);
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}