1
+ from selenium .common .exceptions import StaleElementReferenceException
1
2
from selenium .webdriver .common .by import By
2
3
3
4
import dash
50
51
)
51
52
52
53
53
- def cells_are_same_width (target , table ):
54
- wait .until (lambda : abs (target .size ["width" ] - table .size ["width" ]) <= 1 , 3 )
55
-
56
- target_cells = target .find_elements (
57
- By .CSS_SELECTOR , ".cell-1-1 > table > tbody > tr:last-of-type > *"
58
- )
59
- table_r0c0_cells = table .find_elements (
60
- By .CSS_SELECTOR , ".cell-0-0 > table > tbody > tr:last-of-type > *"
61
- )
62
- table_r0c1_cells = table .find_elements (
63
- By .CSS_SELECTOR , ".cell-0-1 > table > tbody > tr:last-of-type > *"
64
- )
65
- table_r1c0_cells = table .find_elements (
66
- By .CSS_SELECTOR , ".cell-1-0 > table > tbody > tr:last-of-type > *"
67
- )
68
- table_r1c1_cells = table .find_elements (
69
- By .CSS_SELECTOR , ".cell-1-1 > table > tbody > tr:last-of-type > *"
70
- )
71
-
54
+ def cells_are_same_width (test , target_selector , table_selector ):
72
55
# this test is very dependent on the table's implementation details.. we are testing that all the cells are
73
56
# the same width after all..
74
57
75
- # make sure the r1c1 fragment contains all the cells
76
- assert len (target_cells ) == len (table_r1c1_cells )
58
+ def assertions ():
59
+ target = test .wait_for_element (target_selector )
60
+ table = test .wait_for_element (table_selector )
77
61
78
- # for each cell of each fragment, allow a difference of up to 1px either way since
79
- # the resize algorithm can be off by 1px for cycles
80
- for i , target_cell in enumerate (target_cells ):
81
- assert abs (target_cell .size ["width" ] - table_r1c1_cells [i ].size ["width" ]) <= 1
62
+ wait .until (
63
+ lambda : target .size ["width" ] != 0
64
+ and abs (target .size ["width" ] - table .size ["width" ]) <= 1 ,
65
+ 3 ,
66
+ )
67
+ target_cells = target .find_elements (
68
+ By .CSS_SELECTOR , ".cell-1-1 > table > tbody > tr:last-of-type > *"
69
+ )
70
+ table_r0c0_cells = table .find_elements (
71
+ By .CSS_SELECTOR , ".cell-0-0 > table > tbody > tr:last-of-type > *"
72
+ )
73
+ table_r0c1_cells = table .find_elements (
74
+ By .CSS_SELECTOR , ".cell-0-1 > table > tbody > tr:last-of-type > *"
75
+ )
76
+ table_r1c0_cells = table .find_elements (
77
+ By .CSS_SELECTOR , ".cell-1-0 > table > tbody > tr:last-of-type > *"
78
+ )
79
+ table_r1c1_cells = table .find_elements (
80
+ By .CSS_SELECTOR , ".cell-1-1 > table > tbody > tr:last-of-type > *"
81
+ )
82
82
83
- if len ( table_r0c0_cells ) != 0 :
84
- assert abs ( target_cell . size [ "width" ] - table_r0c0_cells [ i ]. size [ "width" ]) <= 1
83
+ # make sure the r1c1 fragment contains all the cells
84
+ assert len ( target_cells ) == len ( table_r1c1_cells )
85
85
86
- if len (table_r0c1_cells ) != 0 :
87
- assert abs (target_cell .size ["width" ] - table_r0c1_cells [i ].size ["width" ]) <= 1
86
+ # for each cell of each fragment, allow a difference of up to 1px either way since
87
+ # the resize algorithm can be off by 1px for cycles
88
+ for i , target_cell in enumerate (target_cells ):
89
+ assert (
90
+ abs (target_cell .size ["width" ] - table_r1c1_cells [i ].size ["width" ]) <= 1
91
+ )
92
+
93
+ if len (table_r0c0_cells ) != 0 :
94
+ assert (
95
+ abs (target_cell .size ["width" ] - table_r0c0_cells [i ].size ["width" ])
96
+ <= 1
97
+ )
98
+
99
+ if len (table_r0c1_cells ) != 0 :
100
+ assert (
101
+ abs (target_cell .size ["width" ] - table_r0c1_cells [i ].size ["width" ])
102
+ <= 1
103
+ )
104
+
105
+ if len (table_r1c0_cells ) != 0 :
106
+ assert (
107
+ abs (target_cell .size ["width" ] - table_r1c0_cells [i ].size ["width" ])
108
+ <= 1
109
+ )
110
+
111
+ retry = 0
88
112
89
- if len (table_r1c0_cells ) != 0 :
90
- assert abs (target_cell .size ["width" ] - table_r1c0_cells [i ].size ["width" ]) <= 1
113
+ while retry < 3 :
114
+ try :
115
+ assertions ()
116
+ break
117
+ except StaleElementReferenceException :
118
+ retry += 1
91
119
92
120
93
121
def szng003_on_prop_change_impl (
@@ -110,11 +138,10 @@ def callback(n_clicks):
110
138
111
139
test .start_server (app )
112
140
113
- target = test .find_element ("#table" )
114
- cells_are_same_width (target , target )
141
+ cells_are_same_width (test , "#table" , "#table" )
115
142
116
143
test .find_element ("#btn" ).click ()
117
- cells_are_same_width (target , target )
144
+ cells_are_same_width (test , "#table" , "#table" )
118
145
119
146
assert test .get_log_errors () == []
120
147
@@ -223,12 +250,12 @@ def update_styles(n_clicks):
223
250
for style in styles :
224
251
display = style .get ("style_table" , dict ()).get ("display" )
225
252
width = style .get ("style_table" , dict ()).get ("width" )
226
- target = (
227
- test .find_element ("#table{}" .format (width )) if display != "none" else None
228
- )
253
+ target_selector = "#table{}" .format (width )
254
+ target = test .find_element (target_selector ) if display != "none" else None
229
255
230
256
for variation in variations :
231
- table = test .find_element ("#{}" .format (variation ["id" ]))
257
+ table_selector = "#{}" .format (variation ["id" ])
258
+ table = test .find_element (table_selector )
232
259
if target is None :
233
260
assert table is not None
234
261
assert (
@@ -240,7 +267,7 @@ def update_styles(n_clicks):
240
267
== "none"
241
268
)
242
269
else :
243
- cells_are_same_width (target , table )
270
+ cells_are_same_width (test , target_selector , table_selector )
244
271
245
272
test .find_element ("#btn" ).click ()
246
273
@@ -275,12 +302,10 @@ def test_szng002_percentages_result_in_same_widths(test):
275
302
276
303
test .start_server (app )
277
304
278
- target = test .find_element ("#table0" )
279
- cells_are_same_width (target , target )
305
+ cells_are_same_width (test , "#table0" , "#table0" )
280
306
281
307
for i in range (1 , len (variations )):
282
- table = test .find_element ("#table{}" .format (i ))
283
- cells_are_same_width (target , table )
308
+ cells_are_same_width (test , "#table0" , "#table{}" .format (i ))
284
309
285
310
assert test .get_log_errors () == []
286
311
@@ -308,10 +333,10 @@ def on_focus(test, props, data_fn):
308
333
for i in range (len (baseProps1 .get ("columns" ))):
309
334
table2 .cell (0 , i ).click ()
310
335
311
- t1 = test . find_element ( "#table1" )
312
- t2 = test . find_element ( "#table2" )
336
+ t1 = "#table1"
337
+ t2 = "#table2"
313
338
314
- cells_are_same_width (t1 , t1 )
315
- cells_are_same_width (t1 , t2 )
339
+ cells_are_same_width (test , t1 , t1 )
340
+ cells_are_same_width (test , t1 , t2 )
316
341
317
342
assert test .get_log_errors () == []
0 commit comments