-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathbase.py
More file actions
383 lines (327 loc) · 14.9 KB
/
Copy pathbase.py
File metadata and controls
383 lines (327 loc) · 14.9 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/python
# To run these tests you must copy one of the example files to "creds.py" in
# this directory, and edit it for your environment. There are 2 example files
# in this directory:
#
# remote_creds.example.py: Configure the Selenium tests to run with BrowserStack
# local_creds.example.py: Configure the Selenium tests to run locally
import re
from creds import SITE_URL, USER, PASS, get_driver
from selenium.webdriver.common.by import By
from selenium.common import exceptions
from contextlib import contextmanager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as exp_cond
import glob
import subprocess
import json
import time
import os
class WebTest:
driver = None
def __init__(self, cap=None):
self.read_ini()
self.driver = get_driver(cap)
self.browser = False
if 'browserName' in self.driver.capabilities:
self.browser = self.driver.capabilities['browserName'].lower()
# Ensure a consistent, generous viewport in CI to avoid responsive layout issues
try:
if os.getenv("GITHUB_ACTIONS") == "true":
self.driver.set_window_rect(x=0, y=0, width=1920, height=1080)
actual_size = self.driver.get_window_size()
if actual_size['width'] < 1000: # If still too small, try alternative method
self.driver.execute_script("window.resizeTo(1920, 1080);")
else:
current_size = self.driver.get_window_size()
self.driver.set_window_size(current_size['width'], 5000)
except Exception as e:
print(f" - Warning: Could not set window size: {e}")
self.load()
def read_ini(self):
self.modules = []
self.servers = 1
self.auth_type = ''
result = subprocess.run(['php', 'get_config.php'], stdout=subprocess.PIPE)
config_dict = json.loads(result.stdout.decode())
if 'modules' in config_dict and isinstance(config_dict['modules'], list):
self.modules += config_dict['modules']
if 'auth_type' in config_dict:
self.auth_type = config_dict['auth_type']
def load(self):
print(" - loading site")
self.go(SITE_URL)
# In headless mode, maximize_window() doesn't work and can cause issues
# Set window size explicitly instead
try:
if os.getenv("GITHUB_ACTIONS") == "true":
self.driver.set_window_rect(x=0, y=0, width=1920, height=1080)
self.driver.execute_script("window.resizeTo(1920, 1080);")
else:
self.driver.maximize_window()
except Exception:
print(" - Could not set window size :(")
if self.browser == 'safari':
try:
self.driver.set_window_size(1920,1080)
except Exception:
print(" - Could not maximize Safari")
def save_debug_artifacts(self, name):
if os.getenv("GITHUB_ACTIONS") != "true":
return
os.makedirs(f"artifacts/{name}", exist_ok=True)
with open(f"artifacts/{name}/page_dump.html", "w", encoding="utf-8") as f:
f.write(self.driver.page_source)
self.driver.save_screenshot(f"artifacts/{name}/page_screenshot.png")
logs = self.driver.get_log("browser")
with open(f"artifacts/{name}/console_logs.json", "w", encoding="utf-8") as f:
json.dump(logs, f, indent=2)
def mod_active(self, name):
# debug self.modules
echo = " - modules enabled: "
for mod in self.modules:
echo += mod + " "
print(echo)
if name in self.modules:
return True
print(" - module not enabled: %s" % name)
return False
def single_server(self):
if self.servers <= 1:
return True
print(" - servers account: %s" % self.servers)
return False
def go(self, url):
self.driver.get(url)
self.wait_for_page_ready()
def login(self, user, password):
print(" - logging in")
user_el = self.by_name('username')
pass_el = self.by_name('password')
user_el.send_keys(user)
pass_el.send_keys(password)
self.by_css('input[value=Login]').click()
def change_val(self, el, val):
self.driver.execute_script('''
var e=arguments[0]; var v=arguments[1]; e.value=v;''',
el, val)
def confirm_alert(self):
WebDriverWait(self.driver, 3).until(exp_cond.alert_is_present(), 'timed out')
alert = self.driver.switch_to.alert
alert.accept()
def logout_no_save(self):
print(" - logging out")
self.driver.find_element(By.CLASS_NAME, 'logout_link').click()
logout = self.by_id('logout_without_saving').click()
def logout(self):
print(" - logging out")
self.driver.find_element(By.CLASS_NAME, 'logout_link').click()
def end(self):
self.driver.quit()
def by_id(self, el_id):
print(" - finding element by id {0}".format(el_id))
return self.driver.find_element(By.ID, el_id)
def by_tag(self, name):
print(" - finding element by tag name {0}".format(name))
return self.driver.find_element(By.TAG_NAME, name)
def by_name(self, name):
print(" - finding element by name {0}".format(name))
return self.driver.find_element(By.NAME, name)
def by_css(self, selector):
print(" - finding element by selector {0}".format(selector))
return self.driver.find_element(By.CSS_SELECTOR, selector)
def by_class(self, class_name):
print(" - finding element by class {0}".format(class_name))
return self.driver.find_element(By.CLASS_NAME, class_name)
def wait_for_element_by_class(self, class_name, timeout=60):
"""Wait for an element to be present and visible by class name"""
print(" - waiting for element by class {0}".format(class_name))
WebDriverWait(self.driver, timeout).until(
exp_cond.presence_of_element_located((By.CLASS_NAME, class_name))
)
element = self.driver.find_element(By.CLASS_NAME, class_name)
WebDriverWait(self.driver, timeout).until(
exp_cond.visibility_of(element)
)
return element
def by_xpath(self, element_xpath):
print(" - finding element by xpath {0}".format(element_xpath))
return self.driver.find_element(By.XPATH, element_xpath)
def element_exists(self, class_name):
print(" - checking if element exists by class {0}".format(class_name))
try:
self.by_class(class_name)
return True
except Exception:
return False
def wait(self, el_type=By.TAG_NAME, el_value="body", timeout=60):
print(" - waiting for page by {0}: {1} ...".format(el_type, el_value))
element = WebDriverWait(self.driver, timeout).until(
exp_cond.presence_of_element_located((el_type, el_value)))
def wait_on_class(self, class_name, timeout=60):
self.wait(By.CLASS_NAME, class_name)
def wait_with_folder_list(self):
self.wait(By.CLASS_NAME, "main")
def wait_on_sys_message(self, timeout=60):
wait = WebDriverWait(self.driver, timeout)
element = wait.until(wait_for_non_empty_text((By.CLASS_NAME, "sys_messages"))
)
def wait_for_navigation_to_complete(self, timeout=60):
print(" - waiting for the navigation to complete...")
import time
# First, check if we have navigation state attributes (new method)
try:
# Wait for navigation to start (state changes to 'loading')
WebDriverWait(self.driver, 5).until(
lambda driver: driver.execute_script(
'return document.body.getAttribute("data-navigation-state") === "loading"'
)
)
print(" - navigation start detected")
# Then wait for navigation to complete (state changes to 'complete')
WebDriverWait(self.driver, timeout).until(
lambda driver: driver.execute_script(
'return document.body.getAttribute("data-navigation-state") === "complete"'
)
)
print(" - navigation completion detected via state attribute")
# Small delay to ensure DOM is settled
time.sleep(0.5)
return
except Exception as state_error:
print(f" - navigation state monitoring failed: {state_error}, trying fallback methods")
# Fallback 1: Try to detect fetch requests
try:
get_current_navigations_request_entries_length = lambda: self.driver.execute_script(
'return window.performance.getEntriesByType("resource").filter((r) => r.initiatorType === "fetch").length'
)
navigation_length = get_current_navigations_request_entries_length()
WebDriverWait(self.driver, min(timeout, 10)).until(
lambda driver: get_current_navigations_request_entries_length() > navigation_length
)
print(" - navigation detected via fetch requests")
time.sleep(0.5)
return
except Exception as fetch_error:
print(f" - fetch monitoring failed: {fetch_error}, trying final fallback")
# Fallback 2: Check for loading indicators and main element
try:
WebDriverWait(self.driver, 5).until_not(
lambda driver: len(driver.find_elements(By.ID, "loading_indicator")) > 0
)
print(" - loading indicator disappeared")
except:
pass
try:
WebDriverWait(self.driver, min(timeout, 15)).until(
exp_cond.presence_of_element_located((By.TAG_NAME, "main"))
)
print(" - main element present")
time.sleep(1)
except Exception as main_error:
print(f" - all navigation waiting methods failed: {state_error}, {fetch_error}, {main_error}")
time.sleep(2)
except Exception as e:
print(f" - unexpected error in navigation waiting: {e}")
time.sleep(2)
def wait_for_page_ready(self, timeout=60):
"""Wait for document readiness and idle network to reduce flakiness after navigation."""
try:
# Wait for DOM ready
WebDriverWait(self.driver, timeout).until(
lambda d: d.execute_script("return document.readyState") == "complete"
)
except Exception:
print(" - document.readyState wait failed, continuing...")
# Best-effort small delay to allow post-load JS to attach handlers in CI
try:
WebDriverWait(self.driver, 5).until(
lambda d: d.execute_script(
"return !!(window.requestIdleCallback || window.setTimeout)"
)
)
except Exception:
pass
def wait_for_settings_to_expand(self):
print(" - waiting for the settings section to expand...")
try:
# First try to find the settings button and click it
settings_button = self.by_css('[data-bs-target=".settings"]')
if settings_button.is_displayed():
# Check if settings are already expanded
try:
settings_div = self.by_class('settings')
if settings_div.is_displayed():
print(" - settings already expanded")
return
except:
pass
# Click to expand
settings_button.click()
# Wait for the settings to be displayed
try:
WebDriverWait(self.driver, 60).until(lambda x: self.by_class('settings').is_displayed())
print(" - settings expanded successfully")
except:
print(" - settings expansion timeout, continuing anyway")
else:
print(" - settings button not visible, skipping expansion")
except Exception as e:
print(f" - settings expansion failed: {e}")
# Continue anyway, the settings might already be expanded
def click_when_clickable(self, el):
print(" - waiting for element to be clickable")
try:
# Scroll element into view
self.driver.execute_script("arguments[0].scrollIntoView({block: 'center', behavior: 'instant'});", el)
# Wait for element to be clickable
WebDriverWait(self.driver, 60).until(
exp_cond.element_to_be_clickable(el)
)
# Try regular click first
try:
el.click()
return
except Exception as click_error:
print(f" - regular click failed: {click_error}")
print(" - trying JavaScript click as fallback")
# Use JavaScript click as fallback
self.driver.execute_script("arguments[0].click();", el)
except Exception as e:
print(f" - click_when_clickable failed: {e}")
# Final fallback: try JavaScript click without waiting
try:
self.driver.execute_script("arguments[0].click();", el)
except Exception as js_error:
print(f" - JavaScript click also failed: {js_error}")
raise e
def safe_click(self, element):
"""Safely click an element with retry logic"""
print(" - safely clicking element")
max_attempts = 3
for attempt in range(max_attempts):
try:
# Scroll element into view
self.driver.execute_script("arguments[0].scrollIntoView({block: 'center', behavior: 'instant'});", element)
WebDriverWait(self.driver, 60).until(exp_cond.element_to_be_clickable(element))
element.click()
return
except Exception as e:
if attempt == max_attempts - 1:
raise e
print(f" - click attempt {attempt + 1} failed, retrying...")
time.sleep(1)
def safari_workaround(self, timeout=1):
if self.browser == 'safari':
print(" - waiting {0} extra second for Safari".format(timeout))
self.driver.implicitly_wait(timeout)
class wait_for_non_empty_text(object):
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
try:
element_text = exp_cond._find_element(driver, self.locator).text.strip()
print(element_text)
return element_text != ""
except exceptions.StaleElementReferenceException:
return False