-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
289 lines (221 loc) · 9.12 KB
/
start.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import re
from getpass import getpass
from time import sleep
from dotenv import load_dotenv
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
def open_time_overview_page(driver):
try:
workday_hamburger_menu = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '[data-uxi-element-id="global-nav-button"]')))
except TimeoutException:
raise ValueError("Loading the Workday page took too much time")
sleep(2)
workday_hamburger_menu.click()
sleep(2)
for item in driver.find_elements(By.CSS_SELECTOR, '[data-automation-id="globalNavAppItemLink"]'):
if item.text == "Time":
time_elem = item
break
time_elem.click()
try:
WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '[title="Time Off Balance"]')))
except TimeoutException:
raise ValueError("Loading the Workday page took too much time")
def open_this_week_page(driver):
open_time_overview_page(driver)
sleep(2)
for button in driver.find_elements(By.TAG_NAME, "button"):
if "This Week" in button.accessible_name:
button.click()
break
def rh_sso_login(driver, login, psw):
try:
username_elem = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "username")))
psw_elem = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "password")))
except TimeoutException:
raise ValueError("Loading the RH SSO Login page took too much time")
username_elem.clear()
username_elem.send_keys(login)
psw_elem.clear()
psw_elem.send_keys(psw)
submit_button = driver.find_element(By.ID, "submit")
submit_button.click()
def get_quick_add_button(driver):
try:
actions_button = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '[title="Actions"]')))
actions_button.click()
except TimeoutException:
raise ValueError("Loading the Workday page took too much time")
try:
return driver.find_element(By.CSS_SELECTOR, '[data-automation-label="Quick Add"]')
except NoSuchElementException:
return None
def click_next_week_button(driver):
next_week_button = driver.find_element(By.CSS_SELECTOR, '[data-automation-id="nextMonthButton"]')
next_week_button.click()
sleep(2)
def click_previous_week_button(driver):
previous_week_button = driver.find_element(By.CSS_SELECTOR, '[data-automation-id="prevMonthButton"]')
previous_week_button.click()
sleep(2)
def find_first_week(driver):
"""
Find first week where is accessible "Quick Add" option under "Actions".
"""
while True:
if not get_quick_add_button(driver):
click_next_week_button(driver)
break
click_previous_week_button(driver)
def get_pre_filled_hours(driver):
pre_filled_hours = {
"Mon": {"index": 0, "hours": None},
"Tue": {"index": 1, "hours": None},
"Wed": {"index": 2, "hours": None},
"Thu": {"index": 3, "hours": None},
"Fri": {"index": 4, "hours": None},
}
for index in range(5):
css_selector = f'[data-automation-id="hoursEntered_{index}"]'
working_hours_label = driver.find_element(By.CSS_SELECTOR, css_selector)
for k, v in pre_filled_hours.items():
if v["index"] == index:
pre_filled_hours[k]["hours"] = int(working_hours_label.text.split()[1])
break
return pre_filled_hours
def select_days(driver, pre_filled_hours):
day_added = False
day_ids = {
"Mon": "56$525708",
"Tue": "56$525705",
"Wed": "56$525706",
"Thu": "56$525703",
"Fri": "56$525709",
}
for day, id in day_ids.items():
if pre_filled_hours[day]["hours"] != 0:
continue
day_label = driver.find_element(By.CSS_SELECTOR, f'[data-uxi-form-item-label-id="{id}"]').get_property("id")
checkbox_id = day_label.strip("-formLabel")
day_checkbox = driver.find_element(By.CSS_SELECTOR, f'[id="{checkbox_id}"]')
if day_checkbox.get_attribute("data-automationcheckboxenabled") == "true":
day_checkbox.click()
day_added = True
sleep(3)
return day_added
def click_next_button(driver):
sleep(1)
driver.execute_script("window.scrollBy(0, 500)")
try:
next_button = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '[data-automation-id="wd-CommandButton"]')))
except TimeoutException:
raise ValueError("Loading the Workday page took too much time")
sleep(1)
next_button.click()
sleep(2)
def select_out_reason(driver, option):
out_reason_elem = driver.find_element(By.CSS_SELECTOR, '[data-automation-id="selectWidget"]')
out_reason_elem.click()
sleep(1)
meal_option_elem = driver.find_element(By.CSS_SELECTOR, f'[data-automation-label="{option}"]')
meal_option_elem.click()
sleep(1)
def click_add_button(driver):
add_button = driver.find_element(By.CSS_SELECTOR, '[data-automation-id="panelSetAddButton"]')
add_button.click()
sleep(2)
def get_in_and_out_elements(driver):
labels = driver.find_elements(By.TAG_NAME, "label")
for l in labels:
if l.text == "In":
in_label_id = l.get_attribute("data-uxi-form-item-label-id")
if l.text == "Out":
out_label_id = l.get_attribute("data-uxi-form-item-label-id")
inputs = driver.find_elements(By.TAG_NAME, "input")
for i in inputs:
if i.get_attribute("aria-labelledby") and in_label_id in i.get_attribute("aria-labelledby"):
in_elem = i
if i.get_attribute("aria-labelledby") and out_label_id in i.get_attribute("aria-labelledby"):
out_elem = i
return in_elem, out_elem
def load_hours_pattern():
template = os.environ.get("TEMPLATE")
pattern = r"^(?P<in1>([1-9]|[01][0-9]|2[0-3]):([0-5][0-9]))-(?P<out1>([1-9]|[01][0-9]|2[0-3]):([0-5][0-9]))($|(\+)(?P<in2>([1-9]|[01][0-9]|2[0-3]):([0-5][0-9]))-(?P<out2>([1-9]|[01][0-9]|2[0-3]):([0-5][0-9]))$)"
match = re.match(pattern, template)
if not match:
raise ValueError("Invalid TEMPLATE for working hours.")
in1 = match.group("in1")
out1 = match.group("out1")
in2 = match.group("in2")
out2 = match.group("out2")
return in1, out1, in2, out2
def fill_in_working_hours(driver, hours_pattern):
in1, out1, in2, out2 = hours_pattern
in_elem, out_elem = get_in_and_out_elements(driver)
in_elem.send_keys(in1)
out_elem.send_keys(out1)
sleep(2)
if not in2:
return
select_out_reason(driver, "Meal")
click_add_button(driver)
in_elem, out_elem = get_in_and_out_elements(driver)
in_elem.send_keys(in2)
out_elem.send_keys(out2)
sleep(2)
def click_ok_button(driver):
driver.execute_script("window.scrollBy(0, 500)")
ok_button = driver.find_element(By.CSS_SELECTOR, '[data-automation-id="wd-CommandButton"]')
ok_button.click()
sleep(3)
def click_back_button(driver):
driver.execute_script("window.scrollBy(0, 500)")
back_button = driver.find_element(By.CSS_SELECTOR, '[title="Back"]')
back_button.click()
sleep(1)
def click_cancel_button(driver):
driver.execute_script("window.scrollBy(0, 500)")
cancel_button = driver.find_element(By.CSS_SELECTOR, '[data-automation-id="wd-CommandButton_uic_cancelButton"]')
cancel_button.click()
sleep(3)
def main():
load_dotenv()
print("Starting 'End of the month' task ...")
print(f"Using template {os.getenv('TEMPLATE')}")
print(f"Login with {os.getenv('LOGIN')}")
print("(TEMPLATE and LOGIN can be set in the .env file)")
login = os.environ.get("LOGIN")
psw = getpass("PASSWORD: ")
driver = webdriver.Chrome()
driver.get("https://wd5.myworkday.com/redhat")
rh_sso_login(driver, login, psw)
open_this_week_page(driver)
find_first_week(driver)
hours_pattern = load_hours_pattern()
while True:
data_range_title_elem = driver.find_element(By.CSS_SELECTOR, '[data-automation-id="dateRangeTitle"]')
quick_add_button = get_quick_add_button(driver)
sleep(1)
if not quick_add_button:
break
print(f"Processing week {data_range_title_elem.text}")
pre_filled_hours = get_pre_filled_hours(driver)
quick_add_button.click()
click_next_button(driver)
days_selected = select_days(driver, pre_filled_hours)
if days_selected:
fill_in_working_hours(driver, hours_pattern)
click_ok_button(driver)
else:
click_back_button(driver)
click_cancel_button(driver)
click_next_week_button(driver)
sleep(2)
driver.quit()
print("... end of 'End of the month' task")
if __name__ == "__main__":
main()