This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathconftest.py
552 lines (427 loc) · 17.3 KB
/
conftest.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import pytest
from dash.testing.browser import Browser
from preconditions import preconditions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
_validate_col = lambda col: (isinstance(col, str) and len(col) > 0) or (
isinstance(col, int) and col >= 0
)
_validate_id = lambda id: isinstance(id, str) and len(id) > 0
_validate_key = lambda key: isinstance(key, str) and len(key) == 1
_validate_keys = lambda keys: isinstance(keys, str) and len(keys) > 0
_validate_mixin = lambda mixin: isinstance(mixin, DataTableMixin)
_validate_row = lambda row: isinstance(row, int) and row >= 0
_validate_selector = lambda selector: isinstance(selector, str) and len(selector) > 0
_validate_state = lambda state: state in [_READY, _LOADING, _ANY]
_validate_target = lambda target: isinstance(target, DataTableFacade)
_READY = ".dash-spreadsheet:not(.dash-loading)"
_LOADING = ".dash-spreadsheet.dash-loading"
_ANY = ".dash-spreadsheet"
_TIMEOUT = 10
class HoldKeyContext:
@preconditions(_validate_mixin, _validate_key)
def __init__(self, mixin, key):
self.mixin = mixin
self.key = key
def __enter__(self):
ActionChains(self.mixin.driver).key_down(self.key).perform()
def __exit__(self, type, value, traceback):
ActionChains(self.mixin.driver).key_up(self.key).perform()
class DataTableCellFacade(object):
@preconditions(
_validate_id, _validate_mixin, _validate_row, _validate_col, _validate_state
)
def __init__(self, id, mixin, row, col, state=_ANY):
self.id = id
self.mixin = mixin
self.row = row
self.col = col
self.state = state
def _get_cell_value(self):
return self.get().find_element_by_css_selector(".dash-cell-value")
def click(self):
return self.get().click()
def double_click(self):
ac = ActionChains(self.mixin.driver)
ac.move_to_element(self._get_cell_value())
ac.pause(1) # sometimes experiencing incorrect behavior on scroll otherwise
ac.double_click()
return ac.perform()
def exists(self):
self.mixin._wait_for_table(self.id, self.state)
return (
len(
self.mixin.find_elements(
'#{} {} tbody td.dash-cell.column-{}[data-dash-row="{}"]:not(.phantom-cell)'.format(
self.id, self.state, self.col, self.row
)
)
)
== 1
if isinstance(self.col, int)
else len(
self.mixin.find_elements(
'#{} {} tbody td.dash-cell[data-dash-column="{}"][data-dash-row="{}"]:not(.phantom-cell)'.format(
self.id, self.state, self.col, self.row
)
)
)
== 1
)
def get(self):
self.mixin._wait_for_table(self.id, self.state)
return (
self.mixin.find_element(
'#{} {} tbody td.dash-cell.column-{}[data-dash-row="{}"]:not(.phantom-cell)'.format(
self.id, self.state, self.col, self.row
)
)
if isinstance(self.col, int)
else self.mixin.find_element(
'#{} {} tbody td.dash-cell[data-dash-column="{}"][data-dash-row="{}"]:not(.phantom-cell)'.format(
self.id, self.state, self.col, self.row
)
)
)
def is_dropdown(self):
el = self.get().find_elements_by_css_selector(".Select-arrow")
return len(el) == 1
def is_input(self):
el = self.get().find_elements_by_css_selector(".dash-cell-value")
return len(el) == 1 and el[0].get_attribute("type") is not None
def get_text(self):
el = self._get_cell_value()
value = el.get_attribute("value")
return (
value
if value is not None and value != ""
else el.get_attribute("innerHTML")
)
def move_to(self):
ac = ActionChains(self.mixin.driver)
ac.move_to_element(self._get_cell_value())
return ac.perform()
def is_active(self):
input = self.get().find_element_by_css_selector("input")
return "focused" in input.get_attribute("class").split(" ")
def is_selected(self):
cell = self.get()
return "cell--selected" in cell.get_attribute("class").split(" ")
def is_focused(self):
cell = self.get()
return "focused" in cell.get_attribute("class").split(" ")
def is_value_focused(self):
el = self._get_cell_value()
return "focused" in el.get_attribute("class").split(" ")
def open_dropdown(self):
cell = self.get()
cell.find_element_by_css_selector(".Select-arrow").click()
class DataTableColumnFacade(object):
@preconditions(_validate_id, _validate_mixin, _validate_col, _validate_state)
def __init__(self, id, mixin, col, state=_ANY):
self.id = id
self.mixin = mixin
self.col = col
self.state = state
@preconditions(_validate_row)
def get(self, row=0):
self.mixin._wait_for_table(self.id, self.state)
return (
self.mixin.find_element(
"#{} {} tbody tr:nth-of-type({}) th.dash-header.column-{}:not(.phantom-cell)".format(
self.id, self.state, row + 1, self.col
)
)
if isinstance(self.col, int)
else self.mixin.find_element(
'#{} {} tbody tr:nth-of-type({}) th.dash-header[data-dash-column="{}"]:not(.phantom-cell)'.format(
self.id, self.state, row + 1, self.col
)
)
)
def exists(self, row=0):
self.mixin._wait_for_table(self.id, self.state)
els = (
self.mixin.find_elements(
"#{} {} tbody tr:nth-of-type({}) th.dash-header.column-{}:not(.phantom-cell)".format(
self.id, self.state, row + 1, self.col
)
)
if isinstance(self.col, int)
else self.mixin.find_elements(
'#{} {} tbody tr:nth-of-type({}) th.dash-header[data-dash-column="{}"]:not(.phantom-cell)'.format(
self.id, self.state, row + 1, self.col
)
)
)
return len(els) != 0
@preconditions(_validate_row)
def clear(self, row=0):
self.get(row).find_element_by_css_selector(".column-header--clear").click()
@preconditions(_validate_row)
def delete(self, row=0):
self.get(row).find_element_by_css_selector(".column-header--delete").click()
@preconditions(_validate_row)
def edit(self, row=0):
self.get(row).find_element_by_css_selector(".column-header--edit").click()
@preconditions(_validate_row)
def get_text(self, row=0):
el = self.get(row).find_element_by_css_selector("span.column-header-name")
return el.get_attribute("innerHTML") if el is not None else None
@preconditions(_validate_row)
def hide(self, row=0):
self.get(row).find_element_by_css_selector(".column-header--hide").click()
@preconditions(_validate_row)
def is_selected(self, row=0):
return (
self.get(row)
.find_element_by_css_selector(".column-header--select input")
.is_selected()
)
@preconditions(_validate_row)
def move_to(self, row=0):
ac = ActionChains(self.mixin.driver)
ac.move_to_element(self.get(row))
return ac.perform()
@preconditions(_validate_row)
def select(self, row=0):
self.get(row).find_element_by_css_selector(
".column-header--select input"
).click()
@preconditions(_validate_row)
def sort(self, row=0):
self.get(row).find_element_by_css_selector(".column-header--sort").click()
def filter(self):
return (
self.mixin.find_element(
"#{} {} tbody tr th.dash-filter.column-{}:not(.phantom-cell)".format(
self.id, self.state, self.col
)
)
if isinstance(self.col, int)
else self.mixin.find_element(
'#{} {} tbody tr th.dash-filter[data-dash-column="{}"]:not(.phantom-cell)'.format(
self.id, self.state, self.col
)
)
)
def filter_click(self):
self.filter().click()
def filter_invalid(self):
return "invalid" in self.filter().get_attribute("class").split(" ")
def filter_value(self, value=None):
if value is None:
return (
self.filter()
.find_element_by_css_selector("input")
.get_attribute("value")
)
else:
self.filter_click()
self.mixin.driver.switch_to.active_element.send_keys(value + Keys.ENTER)
class DataTableRowFacade(object):
@preconditions(_validate_id, _validate_mixin, _validate_row, _validate_state)
def __init__(self, id, mixin, row, state=_ANY):
self.id = id
self.mixin = mixin
self.row = row
self.state = state
def delete(self):
return self.mixin.find_elements(
"#{} {} tbody tr td.dash-delete-cell:not(.phantom-cell)".format(
self.id, self.state
)
)[self.row].click()
def select(self):
return self.mixin.find_elements(
"#{} {} tbody tr td.dash-select-cell:not(.phantom-cell)".format(
self.id, self.state
)
)[self.row].click()
def is_selected(self):
return (
self.mixin.find_elements(
"#{} {} tbody tr td.dash-select-cell:not(.phantom-cell)".format(
self.id, self.state
)
)[self.row]
.find_element_by_css_selector("input")
.is_selected()
)
class DataTablePagingActionFacade(object):
@preconditions(_validate_id, _validate_mixin, _validate_selector)
def __init__(self, id, mixin, selector):
self.id = id
self.mixin = mixin
self.selector = selector
def click(self):
self.mixin._wait_for_table(self.id)
return self.mixin.find_element("#{} {}".format(self.id, self.selector)).click()
def exists(self):
self.mixin._wait_for_table(self.id)
el = self.mixin.find_element("#{} {}".format(self.id, self.selector))
return el is not None and el.is_enabled()
class DataTablePagingCurrentFacade(object):
@preconditions(_validate_id, _validate_mixin)
def __init__(self, id, mixin):
self.id = id
self.mixin = mixin
def click(self):
self.mixin._wait_for_table(self.id)
return self.mixin.find_element("#{} input.current-page".format(self.id)).click()
def get_value(self):
self.mixin._wait_for_table(self.id)
return self.mixin.find_element(
"#{} input.current-page".format(self.id)
).get_attribute("placeholder")
class DataTablePagingFacade(object):
@preconditions(_validate_id, _validate_mixin)
def __init__(self, id, mixin):
self.id = id
self.mixin = mixin
self.current = DataTablePagingCurrentFacade(self.id, self.mixin)
self.first = DataTablePagingActionFacade(
self.id, self.mixin, "button.first-page"
)
self.last = DataTablePagingActionFacade(self.id, self.mixin, "button.last-page")
self.next = DataTablePagingActionFacade(self.id, self.mixin, "button.next-page")
self.previous = DataTablePagingActionFacade(
self.id, self.mixin, "button.previous-page"
)
def exists(self):
self.mixin._wait_for_table(self.id)
return len(self.mixin.find_elements(".previous-next-container")) != 0
class DataTableTooltipFacade(object):
@preconditions(_validate_id, _validate_mixin)
def __init__(self, id, mixin):
self.id = id
self.mixin = mixin
def _get_tooltip(self):
return self.mixin.find_element(".dash-tooltip")
def get(self):
return self._get_tooltip()
def exists(self):
self.mixin._wait_for_table(self.id)
tooltip = self._get_tooltip()
return tooltip is not None and tooltip.is_displayed()
def missing(self):
self.mixin._wait_for_table(self.id)
return len(self.mixin.find_elements(".dash-tooltip")) == 0
def get_text(self):
return (
self._get_tooltip()
.find_element_by_css_selector(".dash-table-tooltip")
.get_attribute("innerHTML")
)
class DataTableToggleColumnsFacade(object):
@preconditions(_validate_id, _validate_mixin)
def __init__(self, id, mixin):
self.id = id
self.mixin = mixin
def open(self):
if not self.is_opened():
self.mixin.find_element("#{} .show-hide".format(self.id)).click()
def close(self):
if self.is_opened():
self.mixin.find_element("#{} .show-hide".format(self.id)).click()
def get_hidden(self):
els = self.mixin.find_elements("#table .show-hide-menu input")
return list(filter(lambda el: not el.is_selected(), els))
def get_hidden_values(self):
return [el.get_attribute("value") for el in self.get_hidden()]
def get_visible(self):
els = self.mixin.find_elements("#table .show-hide-menu input")
return list(filter(lambda el: el.is_selected(), els))
def get_visible_values(self):
return [el.get_attribute("value") for el in self.get_visible()]
def is_opened(self):
return len(self.mixin.find_elements("#{} .show-hide-menu".format(self.id))) != 0
class DataTableFacade(object):
@preconditions(_validate_id, _validate_mixin)
def __init__(self, id, mixin):
self.id = id
self.mixin = mixin
self.paging = DataTablePagingFacade(id, mixin)
self.tooltip = DataTableTooltipFacade(id, mixin)
@preconditions(_validate_row, _validate_col, _validate_state)
def cell(self, row, col, state=_ANY):
return DataTableCellFacade(self.id, self.mixin, row, col, state)
@preconditions(_validate_col, _validate_state)
def column(self, col, state=_ANY):
return DataTableColumnFacade(self.id, self.mixin, col, state)
@preconditions(_validate_row, _validate_state)
def row(self, row, state=_ANY):
return DataTableRowFacade(self.id, self.mixin, row, state)
@preconditions(_validate_state)
def toggle_columns(self, state=_ANY):
return DataTableToggleColumnsFacade(self.id, self.mixin)
def is_ready(self):
return self.mixin._wait_for_table(self.id, _READY)
def is_loading(self):
return self.mixin._wait_for_table(self.id, _LOADING)
class DataTableMixin(object):
@preconditions(_validate_id, _validate_state)
def _wait_for_table(self, id, state=_ANY):
return WebDriverWait(self.driver, _TIMEOUT).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "#{} {}".format(id, state))
)
)
@preconditions(_validate_id)
def table(self, id):
return DataTableFacade(id, self)
def get_table_ids(self):
return self.driver.execute_script(
"""
return Array.from(
document.querySelectorAll('.dash-spreadsheet-container')
).map(
e => e.parentElement.getAttribute('id')
)
"""
)
def copy(self):
with self.hold(Keys.CONTROL):
self.send_keys("c")
def paste(self):
with self.hold(Keys.CONTROL):
self.send_keys("v")
@preconditions(_validate_key)
def hold(self, key):
return HoldKeyContext(self, key)
def get_selected_text(self):
return self.driver.execute_script("return window.getSelection().toString()")
@preconditions(_validate_keys)
def send_keys(self, keys):
self.driver.switch_to.active_element.send_keys(keys)
class DataTableComposite(Browser, DataTableMixin):
def __init__(self, server, **kwargs):
super(DataTableComposite, self).__init__(**kwargs)
self.server = server
self.READY = _READY
self.LOADING = _LOADING
self.ANY = _ANY
def get_log_errors(self):
return list(filter(lambda i: i.get("level") != "WARNING", self.get_logs()))
def start_server(self, app, **kwargs):
"""start the local server with app"""
# start server with app and pass Dash arguments
self.server(app, **kwargs)
# set the default server_url, it implicitly call wait_for_page
self.server_url = self.server.url
@pytest.fixture
def test(request, dash_thread_server, tmpdir):
with DataTableComposite(
dash_thread_server,
browser=request.config.getoption("webdriver"),
remote=request.config.getoption("remote"),
remote_url=request.config.getoption("remote_url"),
headless=request.config.getoption("headless"),
options=request.config.hook.pytest_setup_options(),
download_path=tmpdir.mkdir("dt-download").strpath,
percy_finalize=request.config.getoption("nopercyfinalize"),
) as dc:
yield dc