Skip to content

Commit 5ce8204

Browse files
chriddypAkronix
authored andcommitted
use more reliable wait_for utilities
1 parent b9d0167 commit 5ce8204

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

tests/test_render.py

+44-49
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import dash_html_components as html
55
import dash_core_components as dcc
66
from .IntegrationTests import IntegrationTests
7-
from .utils import assert_clean_console, invincible, wait_for
7+
from .utils import assert_clean_console, wait_for
88
from multiprocessing import Value
99
import time
1010
import re
@@ -14,12 +14,28 @@
1414

1515
class Tests(IntegrationTests):
1616
def setUp(self):
17-
def wait_for_element_by_id(id):
18-
wait_for(lambda: None is not invincible(
19-
lambda: self.driver.find_element_by_id(id)
20-
))
21-
return self.driver.find_element_by_id(id)
22-
self.wait_for_element_by_id = wait_for_element_by_id
17+
pass
18+
19+
def wait_for_element_by_css_selector(self, selector):
20+
start_time = time.time()
21+
while time.time() < start_time + 20:
22+
try:
23+
return self.driver.find_element_by_css_selector(selector)
24+
except Exception as e:
25+
pass
26+
time.sleep(0.25)
27+
raise e
28+
29+
def wait_for_text_to_equal(self, selector, assertion_text):
30+
start_time = time.time()
31+
while time.time() < start_time + 20:
32+
el = self.wait_for_element_by_css_selector(selector)
33+
try:
34+
return self.assertEqual(el.text, assertion_text)
35+
except Exception as e:
36+
pass
37+
time.sleep(0.25)
38+
raise e
2339

2440
def request_queue_assertions(
2541
self, check_rejected=True, expected_length=None):
@@ -85,7 +101,7 @@ def test_initial_state(self):
85101

86102
self.startServer(app)
87103

88-
el = self.wait_for_element_by_id('_dash-app-content')
104+
el = self.wait_for_element_by_css_selector('#_dash-app-content')
89105

90106
# TODO - Make less fragile with http://lxml.de/lxmlhtml.html#html-diff
91107
rendered_dom = '''
@@ -440,17 +456,15 @@ def update_output(value):
440456

441457
self.startServer(app)
442458

443-
output1 = self.wait_for_element_by_id('output-1')
444-
wait_for(lambda: output1.text == 'initial value')
459+
self.wait_for_text_to_equal('#output-1', 'initial value')
445460
self.percy_snapshot(name='simple-callback-1')
446461

447-
input1 = self.wait_for_element_by_id('input')
462+
input1 = self.wait_for_element_by_css_selector('#input')
448463
input1.clear()
449464

450465
input1.send_keys('hello world')
451466

452-
output1 = lambda: self.wait_for_element_by_id('output-1')
453-
wait_for(lambda: output1().text == 'hello world')
467+
self.wait_for_text_to_equal('#output-1', 'hello world')
454468
self.percy_snapshot(name='simple-callback-2')
455469

456470
self.assertEqual(
@@ -558,11 +572,9 @@ def update_input(value):
558572
# editing the input should modify the sub output
559573
sub_input = self.driver.find_element_by_id('sub-input-1')
560574
sub_input.send_keys('a')
561-
wait_for(
562-
lambda: (
563-
self.driver.find_element_by_id('sub-output-1').text
564-
) == 'sub input initial valuea'
565-
)
575+
self.wait_for_text_to_equal(
576+
'#sub-output-1',
577+
'sub input initial valuea')
566578

567579
self.assertEqual(call_count.value, 2)
568580

@@ -727,14 +739,7 @@ def generic_chapter_assertions(chapter):
727739
else:
728740
value = chapters[chapter]['{}-controls'.format(chapter)].value
729741
# check the actual values
730-
wait_for(
731-
lambda: (
732-
self.driver.find_element_by_id(
733-
'{}-label'.format(chapter)
734-
).text
735-
== value
736-
)
737-
)
742+
self.wait_for_text_to_equal('#{}-label'.format(chapter), value)
738743
wait_for(
739744
lambda: (
740745
self.driver.execute_script(
@@ -880,10 +885,7 @@ def chapter3_assertions():
880885
(self.driver.find_elements_by_css_selector(
881886
'input[type="radio"]'
882887
)[3]).click()
883-
wait_for(lambda: (
884-
self.driver.find_element_by_id('body').text ==
885-
'Just a string'
886-
))
888+
self.wait_for_text_to_equal('#body', 'Just a string')
887889
self.percy_snapshot(name='chapter-4')
888890

889891
# each element should exist in the dom
@@ -954,8 +956,7 @@ def update_output_2(value):
954956

955957
self.startServer(app)
956958

957-
el = self.wait_for_element_by_id('output-1')
958-
wait_for(lambda: el.text == 'initial value')
959+
self.wait_for_text_to_equal('#output-1', 'initial value')
959960
self.percy_snapshot(name='dependencies')
960961
time.sleep(1.0)
961962
self.assertEqual(output_1_call_count.value, 1)
@@ -964,8 +965,7 @@ def update_output_2(value):
964965
input = self.driver.find_element_by_id('input')
965966

966967
input.send_keys('a')
967-
wait_for(lambda: self.driver.find_element_by_id('output-1').text
968-
== 'initial valuea')
968+
self.wait_for_text_to_equal('#output-1', 'initial valuea')
969969
time.sleep(1.0)
970970
self.assertEqual(output_1_call_count.value, 2)
971971
self.assertEqual(output_2_call_count.value, 0)
@@ -1497,7 +1497,7 @@ def dynamic_output(*args):
14971497

14981498
self.startServer(app)
14991499

1500-
self.wait_for_element_by_id('display-content').click()
1500+
self.wait_for_element_by_css_selector('#display-content').click()
15011501

15021502
time.sleep(5)
15031503

@@ -1526,7 +1526,7 @@ def update_output(n_clicks):
15261526
return n_clicks
15271527

15281528
self.startServer(app)
1529-
button = self.wait_for_element_by_id('input')
1529+
button = self.wait_for_element_by_css_selector('#input')
15301530
button.click()
15311531
button.click()
15321532
time.sleep(8)
@@ -1579,7 +1579,7 @@ def dropdown_2(value, session_id):
15791579

15801580
self.startServer(app)
15811581

1582-
self.wait_for_element_by_id('session-id')
1582+
self.wait_for_element_by_css_selector('#session-id')
15831583
time.sleep(2)
15841584
self.assertEqual(call_counts['dropdown_1'].value, 1)
15851585
self.assertEqual(call_counts['dropdown_2'].value, 1)
@@ -1612,8 +1612,8 @@ def test_callbacks_triggered_on_generated_output(self):
16121612
html.Div(id='tab-output')
16131613
])
16141614

1615-
1616-
@app.callback(Output('tab-output', 'children'), [Input('tabs', 'value')])
1615+
@app.callback(Output('tab-output', 'children'),
1616+
[Input('tabs', 'value')])
16171617
def display_content(value):
16181618
return html.Div([
16191619
html.Div(id='tab-{}-output'.format(value))
@@ -1631,27 +1631,22 @@ def display_tab2_output(value):
16311631
call_counts['tab2'].value += 1
16321632
return 'You have selected "{}"'.format(value)
16331633

1634-
16351634
self.startServer(app)
1636-
self.wait_for_element_by_id('tab-output')
1635+
self.wait_for_element_by_css_selector('#tab-output')
16371636
time.sleep(2)
16381637

16391638
self.assertEqual(call_counts['tab1'].value, 1)
16401639
self.assertEqual(call_counts['tab2'].value, 0)
1641-
wait_for(lambda: (
1642-
self.driver.find_element_by_id('tab-output').text ==
1643-
'You have selected "1"'
1644-
))
1640+
self.wait_for_text_to_equal('#tab-output', 'You have selected "a"')
1641+
self.wait_for_text_to_equal('#tab-1-output', 'You have selected "a"')
16451642

16461643
(self.driver.find_elements_by_css_selector(
16471644
'input[type="radio"]'
16481645
)[1]).click()
16491646
time.sleep(2)
16501647

1651-
wait_for(lambda: (
1652-
self.driver.find_element_by_id('tab-output').text ==
1653-
'You have selected "2"'
1654-
))
1648+
self.wait_for_text_to_equal('#tab-output', 'You have selected "a"')
1649+
self.wait_for_text_to_equal('#tab-2-output', 'You have selected "a"')
16551650
self.assertEqual(call_counts['tab1'].value, 1)
16561651
self.assertEqual(call_counts['tab2'].value, 1)
16571652

0 commit comments

Comments
 (0)