Skip to content

Commit 357d959

Browse files
committed
Retry table cell assertions on StaleElementReferenceException
1 parent 57c0ea4 commit 357d959

File tree

1 file changed

+72
-47
lines changed

1 file changed

+72
-47
lines changed

components/dash-table/tests/selenium/test_sizing.py

Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from selenium.common.exceptions import StaleElementReferenceException
12
from selenium.webdriver.common.by import By
23

34
import dash
@@ -50,44 +51,71 @@
5051
)
5152

5253

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):
7255
# this test is very dependent on the table's implementation details.. we are testing that all the cells are
7356
# the same width after all..
7457

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)
7761

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+
)
8282

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)
8585

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
88112

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
91119

92120

93121
def szng003_on_prop_change_impl(
@@ -110,11 +138,10 @@ def callback(n_clicks):
110138

111139
test.start_server(app)
112140

113-
target = test.find_element("#table")
114-
cells_are_same_width(target, target)
141+
cells_are_same_width(test, "#table", "#table")
115142

116143
test.find_element("#btn").click()
117-
cells_are_same_width(target, target)
144+
cells_are_same_width(test, "#table", "#table")
118145

119146
assert test.get_log_errors() == []
120147

@@ -223,12 +250,12 @@ def update_styles(n_clicks):
223250
for style in styles:
224251
display = style.get("style_table", dict()).get("display")
225252
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
229255

230256
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)
232259
if target is None:
233260
assert table is not None
234261
assert (
@@ -240,7 +267,7 @@ def update_styles(n_clicks):
240267
== "none"
241268
)
242269
else:
243-
cells_are_same_width(target, table)
270+
cells_are_same_width(test, target_selector, table_selector)
244271

245272
test.find_element("#btn").click()
246273

@@ -275,12 +302,10 @@ def test_szng002_percentages_result_in_same_widths(test):
275302

276303
test.start_server(app)
277304

278-
target = test.find_element("#table0")
279-
cells_are_same_width(target, target)
305+
cells_are_same_width(test, "#table0", "#table0")
280306

281307
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))
284309

285310
assert test.get_log_errors() == []
286311

@@ -308,10 +333,10 @@ def on_focus(test, props, data_fn):
308333
for i in range(len(baseProps1.get("columns"))):
309334
table2.cell(0, i).click()
310335

311-
t1 = test.find_element("#table1")
312-
t2 = test.find_element("#table2")
336+
t1 = "#table1"
337+
t2 = "#table2"
313338

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)
316341

317342
assert test.get_log_errors() == []

0 commit comments

Comments
 (0)