-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfill-working-days.js
119 lines (85 loc) · 3.47 KB
/
fill-working-days.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
require("dotenv").config();
const puppeteer = require("puppeteer");
const { message } = require("./utils");
const { ACTIVITY_TYPE, SELECTORS, WAIT_TIMES, WAIT_TYPES } = require("./constants");
const wait = async (ms) => new Promise((r) => setTimeout(r, ms));
async function fillWorkingDays() {
const browser = await puppeteer.launch({
headless: false,
product: "chrome",
args: [
"--user-data-dir=~/Library/Application Support/Google/Chrome/Default",
],
});
const page = await browser.newPage();
await page.goto(process.env.PROJECT_URL, {
waitUntil: WAIT_TYPES.DOMCONTENTLOADED,
});
await page.waitForSelector(
SELECTORS.TABLE_ITEM,
);
const getWorkingDaysIndexes = async () => await page.evaluate((SELECTORS) => {
const validItems = [];
const items = document.querySelectorAll(SELECTORS.TABLE_ITEM)
items.forEach((item, index) => {
const filledHoursEls = item.querySelectorAll(SELECTORS.TABLE_ITEM_HOURS);
const weekendInfoEl = item.querySelector(SELECTORS.TABLE_ITEM_WEEKEND);
const hasValidContent = filledHoursEls && filledHoursEls.length > 0;
if (hasValidContent && !weekendInfoEl) {
validItems.push(index)
}
})
return validItems
}, SELECTORS)
const getHoursWithoutActivity = async () => await page.evaluate((SELECTORS) => {
const hoursWithoutActivityText = document.querySelector(SELECTORS.TIME_WITHOUT_ACTIVITY)
return hoursWithoutActivityText?.innerText?.replace('Horas sem atividade ', '') || '00:00'
}, SELECTORS)
const getCurrentWorkingDay = async (index) => await page.evaluate((index, SELECTORS) => {
const currentWorkingDay = document.querySelectorAll(SELECTORS.TABLE_ITEM_DATE)[index]
return currentWorkingDay.innerText
}, index, SELECTORS)
const validWorkingDaysIndex = await getWorkingDaysIndexes()
for (const index of validWorkingDaysIndex) {
const currentWorkingDay = await getCurrentWorkingDay(index)
message.info(`Filling: ${currentWorkingDay}`)
try {
const linkElements = await page.$$(SELECTORS.TABLE_ITEM_LINK)
const link = linkElements[index];
await link.click()
await page.waitForSelector(SELECTORS.TIME_WITHOUT_ACTIVITY)
const dropdownElement = await page.$(SELECTORS.DROPDOWN)
await dropdownElement.click();
// Wait for dropdown open animation
await wait(WAIT_TIMES.SHORT)
// TODO: Find a better way to select the option based on the text
const option = await page.$(SELECTORS.DROPDOWN_OPTION)
await option.click();
const hoursWithoutActivity = await getHoursWithoutActivity()
if (hoursWithoutActivity === '00:00') {
await page.goBack({
waitUntil: WAIT_TYPES.DOMCONTENTLOADED,
})
await page.waitForSelector(SELECTORS.TABLE_ITEM_LINK)
message.success(`Day ${currentWorkingDay} filled successfully!`)
continue
}
await page.type(SELECTORS.INPUT_TIME, hoursWithoutActivity);
await page.type(SELECTORS.INPUT_ACTIVITY, ACTIVITY_TYPE);
const submitButton = await page.$(SELECTORS.SUBMIT_BUTTON)
await submitButton.click()
await wait(WAIT_TIMES.LONG)
await page.goBack({
waitUntil: WAIT_TYPES.DOMCONTENTLOADED,
})
await page.waitForSelector(SELECTORS.TABLE_ITEM_LINK)
message.success(`Day ${currentWorkingDay} filled successfully!`)
} catch (error) {
message.error(error)
}
}
await browser.close()
}
module.exports = {
fillWorkingDays
}