This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathtest_integration.py
1623 lines (1345 loc) · 55.7 KB
/
test_integration.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import base64
import itertools
from datetime import datetime
import io
import os
import sys
import time
import json
import flask
import pandas as pd
import dash
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import dash_table_experiments as dt
from dash.exceptions import PreventUpdate
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import InvalidElementStateException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from textwrap import dedent
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
from .IntegrationTests import IntegrationTests
from .utils import wait_for
from multiprocessing import Value
# Download geckodriver: https://github.com/mozilla/geckodriver/releases
# And add to path:
# export PATH=$PATH:/Users/chriddyp/Repos/dash-stuff/dash-integration-tests
#
# Uses percy.io for automated screenshot tests
# export PERCY_PROJECT=plotly/dash-integration-tests
# export PERCY_TOKEN=...
TIMEOUT = 20
class Tests(IntegrationTests):
def setUp(self):
pass
def wait_for_element_by_css_selector(self, selector):
return WebDriverWait(self.driver, TIMEOUT).until(
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
)
def wait_for_text_to_equal(self, selector, assertion_text):
def text_equal(driver):
text = driver.find_element_by_css_selector(selector).text
return text == assertion_text
WebDriverWait(self.driver, TIMEOUT).until(
text_equal
)
def snapshot(self, name):
if 'PERCY_PROJECT' in os.environ and 'PERCY_TOKEN' in os.environ:
python_version = sys.version.split(' ')[0]
print('Percy Snapshot {}'.format(python_version))
self.percy_runner.snapshot(name=name)
def create_upload_component_content_types_test(self, filename):
app = dash.Dash(__name__)
filepath = os.path.join(os.getcwd(), 'test', 'upload-assets', filename)
pre_style = {
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
}
app.layout = html.Div([
html.Div(filepath, id='waitfor'),
html.Div(
id='upload-div',
children=dcc.Upload(
id='upload',
children=html.Div([
'Drag and Drop or ',
html.A('Select a File')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center'
}
)
),
html.Div(id='output'),
html.Div(dt.DataTable(rows=[{}]), style={'display': 'none'})
])
@app.callback(Output('output', 'children'),
[Input('upload', 'contents')])
def update_output(contents):
if contents is not None:
content_type, content_string = contents.split(',')
if 'csv' in filepath:
df = pd.read_csv(io.StringIO(base64.b64decode(
content_string).decode('utf-8')))
return html.Div([
dt.DataTable(
rows=df.to_dict('records'),
columns=['city', 'country']),
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents, style=pre_style)
])
elif 'xls' in filepath:
df = pd.read_excel(io.BytesIO(base64.b64decode(
content_string)))
return html.Div([
dt.DataTable(
rows=df.to_dict('records'),
columns=['city', 'country']),
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents, style=pre_style)
])
elif 'image' in content_type:
return html.Div([
html.Img(src=contents),
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents, style=pre_style)
])
else:
return html.Div([
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents, style=pre_style)
])
self.startServer(app)
try:
self.wait_for_element_by_css_selector('#waitfor')
except Exception as e:
print(self.wait_for_element_by_css_selector(
'#_dash-app-content').get_attribute('innerHTML'))
raise e
upload_div = self.wait_for_element_by_css_selector(
'#upload-div input[type=file]')
upload_div.send_keys(filepath)
time.sleep(5)
self.snapshot(filename)
def test_upload_csv(self):
self.create_upload_component_content_types_test('utf8.csv')
def test_upload_xlsx(self):
self.create_upload_component_content_types_test('utf8.xlsx')
def test_upload_png(self):
self.create_upload_component_content_types_test('dash-logo-stripe.png')
def test_upload_svg(self):
self.create_upload_component_content_types_test('dash-logo-stripe.svg')
def test_upload_gallery(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='waitfor'),
html.Label('Empty'),
dcc.Upload(),
html.Label('Button'),
dcc.Upload(html.Button('Upload File')),
html.Label('Text'),
dcc.Upload('Upload File'),
html.Label('Link'),
dcc.Upload(html.A('Upload File')),
html.Label('Style'),
dcc.Upload([
'Drag and Drop or ',
html.A('Select a File')
], style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center'
})
])
self.startServer(app)
try:
self.wait_for_element_by_css_selector('#waitfor')
except Exception as e:
print(self.wait_for_element_by_css_selector(
'#_dash-app-content').get_attribute('innerHTML'))
raise e
self.snapshot('test_upload_gallery')
def test_gallery(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='waitfor'),
html.Label('Upload'),
dcc.Upload(),
html.Label('Horizontal Tabs'),
dcc.Tabs(id="tabs", children=[
dcc.Tab(label='Tab one', className='test', style={'border': '1px solid magenta'}, children=[
html.Div(['Test'])
]),
dcc.Tab(label='Tab two', children=[
html.Div([
html.H1("This is the content in tab 2"),
html.P("A graph here would be nice!")
])
], id='tab-one'),
dcc.Tab(label='Tab three', children=[
html.Div([
html.H1("This is the content in tab 3"),
])
]),
],
style={
'fontFamily': 'system-ui'
},
content_style={
'border': '1px solid #d6d6d6',
'padding': '44px'
},
parent_style={
'maxWidth': '1000px',
'margin': '0 auto'
}
),
html.Label('Vertical Tabs'),
dcc.Tabs(id="tabs1", vertical=True, children=[
dcc.Tab(label='Tab one', children=[
html.Div(['Test'])
]),
dcc.Tab(label='Tab two', children=[
html.Div([
html.H1("This is the content in tab 2"),
html.P("A graph here would be nice!")
])
]),
dcc.Tab(label='Tab three', children=[
html.Div([
html.H1("This is the content in tab 3"),
])
]),
]
),
html.Label('Dropdown'),
dcc.Dropdown(
options=[
{'label': 'New York City', 'value': 'NYC'},
{'label': u'Montréal', 'value': 'MTL'},
{'label': 'San Francisco', 'value': 'SF'},
{'label': u'北京', 'value': u'北京'}
],
value='MTL',
id='dropdown'
),
html.Label('Multi-Select Dropdown'),
dcc.Dropdown(
options=[
{'label': 'New York City', 'value': 'NYC'},
{'label': u'Montréal', 'value': 'MTL'},
{'label': 'San Francisco', 'value': 'SF'},
{'label': u'北京', 'value': u'北京'}
],
value=['MTL', 'SF'],
multi=True
),
html.Label('Radio Items'),
dcc.RadioItems(
options=[
{'label': 'New York City', 'value': 'NYC'},
{'label': u'Montréal', 'value': 'MTL'},
{'label': 'San Francisco', 'value': 'SF'},
{'label': u'北京', 'value': u'北京'}
],
value='MTL'
),
html.Label('Checkboxes'),
dcc.Checklist(
options=[
{'label': 'New York City', 'value': 'NYC'},
{'label': u'Montréal', 'value': 'MTL'},
{'label': 'San Francisco', 'value': 'SF'},
{'label': u'北京', 'value': u'北京'}
],
values=['MTL', 'SF']
),
html.Label('Text Input'),
dcc.Input(value='', placeholder='type here', type='text',
id='textinput'),
html.Label('Disabled Text Input'),
dcc.Input(value='disabled', type='text',
id='disabled-textinput', disabled=True),
html.Label('Slider'),
dcc.Slider(
min=0,
max=9,
marks={i: 'Label {}'.format(i) if i == 1 else str(i)
for i in range(1, 6)},
value=5,
),
html.Label('Graph'),
dcc.Graph(
id='graph',
figure={
'data': [{
'x': [1, 2, 3],
'y': [4, 1, 4]
}],
'layout': {
'title': u'北京'
}
}
),
html.Div([
html.Label('DatePickerSingle'),
dcc.DatePickerSingle(
id='date-picker-single',
date=datetime(1997, 5, 10)
),
html.Div([
html.Label('DatePickerSingle - empty input'),
dcc.DatePickerSingle(),
], id='dt-single-no-date-value'
),
html.Div([
html.Label('DatePickerSingle - initial visible month (May 97)'),
dcc.DatePickerSingle(
initial_visible_month=datetime(1997, 5, 10)
),
], id='dt-single-no-date-value-init-month'
),
]),
html.Div([
html.Label('DatePickerRange'),
dcc.DatePickerRange(
id='date-picker-range',
start_date=datetime(1997, 5, 3),
end_date_placeholder_text='Select a date!'
),
html.Div([
html.Label('DatePickerRange - empty input'),
dcc.DatePickerRange(
start_date_placeholder_text='Start date',
end_date_placeholder_text='End date'
),
], id='dt-range-no-date-values'
),
html.Div([
html.Label('DatePickerRange - initial visible month (May 97)'),
dcc.DatePickerRange(
start_date_placeholder_text='Start date',
end_date_placeholder_text='End date',
initial_visible_month=datetime(1997, 5, 10)
),
], id='dt-range-no-date-values-init-month'
),
]),
html.Label('TextArea'),
dcc.Textarea(
placeholder='Enter a value... 北京',
style={'width': '100%'}
),
html.Label('Markdown'),
dcc.Markdown('''
#### Dash and Markdown
Dash supports [Markdown](http://commonmark.org/help).
Markdown is a simple way to write and format text.
It includes a syntax for things like **bold text** and *italics*,
[links](http://commonmark.org/help), inline `code` snippets, lists,
quotes, and more.
北京
'''.replace(' ', '')),
dcc.Markdown(['# Line one', '## Line two']),
dcc.Markdown(),
dcc.SyntaxHighlighter(dedent('''import python
print(3)'''), language='python'),
dcc.SyntaxHighlighter([
'import python',
'print(3)'
], language='python'),
dcc.SyntaxHighlighter()
])
self.startServer(app)
self.wait_for_element_by_css_selector('#waitfor')
self.snapshot('gallery')
self.driver.find_element_by_css_selector(
'#dropdown .Select-input input'
).send_keys(u'北')
self.snapshot('gallery - chinese character')
text_input = self.driver.find_element_by_id('textinput')
disabled_text_input = self.driver.find_element_by_id(
'disabled-textinput')
text_input.send_keys('HODOR')
# It seems selenium errors when send(ing)_keys on a disabled element.
# In case this changes we try anyway and catch the particular
# exception. In any case Percy will snapshot the disabled input style
# so we are not totally dependent on the send_keys behaviour for
# testing disabled state.
try:
disabled_text_input.send_keys('RODOH')
except InvalidElementStateException:
pass
self.snapshot('gallery - text input')
# DatePickerSingle and DatePickerRange test
# for issue with datepicker when date value is `None`
dt_input_1 = self.driver.find_element_by_css_selector(
'#dt-single-no-date-value #date'
)
dt_input_1.click()
self.snapshot('gallery - DatePickerSingle\'s datepicker '
'when no date value and no initial month specified')
dt_input_1.send_keys("1997-05-03")
dt_input_2 = self.driver.find_element_by_css_selector(
'#dt-single-no-date-value-init-month #date'
)
dt_input_2.click()
self.snapshot('gallery - DatePickerSingle\'s datepicker '
'when no date value, but initial month is specified')
dt_input_2.send_keys("1997-05-03")
dt_input_3 = self.driver.find_element_by_css_selector(
'#dt-range-no-date-values #endDate'
)
dt_input_3.click()
self.snapshot('gallery - DatePickerRange\'s datepicker '
'when neither start date nor end date '
'nor initial month is specified')
dt_input_3.send_keys("1997-05-03")
dt_input_4 = self.driver.find_element_by_css_selector(
'#dt-range-no-date-values-init-month #endDate'
)
dt_input_4.click()
self.snapshot('gallery - DatePickerRange\'s datepicker '
'when neither start date nor end date is specified, '
'but initial month is')
dt_input_4.send_keys("1997-05-03")
def test_tabs_in_vertical_mode(self):
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Tabs(id="tabs", value='tab-3', children=[
dcc.Tab(label='Tab one', value='tab-1', id='tab-1', children=[
html.Div('Tab One Content')
]),
dcc.Tab(label='Tab two', value='tab-2', id='tab-2', children=[
html.Div('Tab Two Content')
]),
dcc.Tab(label='Tab three', value='tab-3', id='tab-3', children=[
html.Div('Tab Three Content')
]),
], vertical=True),
html.Div(id='tabs-content')
])
self.startServer(app=app)
self.wait_for_text_to_equal('#tab-3', 'Tab three')
self.snapshot('Tabs - vertical mode')
def test_tabs_without_children(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Dash Tabs component demo'),
dcc.Tabs(id="tabs", value='tab-2', children=[
dcc.Tab(label='Tab one', value='tab-1', id='tab-1'),
dcc.Tab(label='Tab two', value='tab-2', id='tab-2'),
]),
html.Div(id='tabs-content')
])
@app.callback(dash.dependencies.Output('tabs-content', 'children'),
[dash.dependencies.Input('tabs', 'value')])
def render_content(tab):
if tab == 'tab-1':
return html.Div([
html.H3('Test content 1')
], id='test-tab-1')
elif tab == 'tab-2':
return html.Div([
html.H3('Test content 2')
], id='test-tab-2')
self.startServer(app=app)
self.wait_for_text_to_equal('#tabs-content', 'Test content 2')
self.snapshot('initial tab - tab 2')
selected_tab = self.wait_for_element_by_css_selector('#tab-1')
selected_tab.click()
time.sleep(1)
self.wait_for_text_to_equal('#tabs-content', 'Test content 1')
def test_tabs_with_children_undefined(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Dash Tabs component demo'),
dcc.Tabs(id="tabs", value='tab-1'),
html.Div(id='tabs-content')
])
self.startServer(app=app)
self.snapshot('Tabs component with children undefined')
def test_tabs_render_without_selected(self):
app = dash.Dash(__name__)
data = [
{'id': 'one', 'value': 1},
{'id': 'two', 'value': 2},
]
menu = html.Div([
html.Div('one', id='one'),
html.Div('two', id='two')
])
tabs_one = html.Div([
dcc.Tabs([
dcc.Tab(dcc.Graph(id='graph-one'), label='tab-one-one'),
])
], id='tabs-one', style={'display': 'none'})
tabs_two = html.Div([
dcc.Tabs([
dcc.Tab(dcc.Graph(id='graph-two'), label='tab-two-one'),
])
], id='tabs-two', style={'display': 'none'})
app.layout = html.Div([
menu,
tabs_one,
tabs_two
])
for i in ('one', 'two'):
@app.callback(Output('tabs-{}'.format(i), 'style'),
[Input(i, 'n_clicks')])
def on_click(n_clicks):
if n_clicks is None:
raise PreventUpdate
if n_clicks % 2 == 1:
return {'display': 'block'}
return {'display': 'none'}
@app.callback(Output('graph-{}'.format(i), 'figure'),
[Input(i, 'n_clicks')])
def on_click(n_clicks):
if n_clicks is None:
raise PreventUpdate
return {
'data': [
{
'x': [1, 2, 3, 4],
'y': [4, 3, 2, 1]
}
]
}
self.startServer(app=app)
button_one = self.wait_for_element_by_css_selector('#one')
button_two = self.wait_for_element_by_css_selector('#two')
button_one.click()
# wait for tabs to be loaded after clicking
WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "#graph-one .main-svg"))
)
time.sleep(1)
self.snapshot("Tabs 1 rendered ")
button_two.click()
# wait for tabs to be loaded after clicking
WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "#graph-two .main-svg"))
)
time.sleep(1)
self.snapshot("Tabs 2 rendered ")
def test_tabs_without_value(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Dash Tabs component demo'),
dcc.Tabs(id="tabs-without-value", children=[
dcc.Tab(label='Tab One', value='tab-1'),
dcc.Tab(label='Tab Two', value='tab-2'),
]),
html.Div(id='tabs-content')
])
@app.callback(Output('tabs-content', 'children'),
[Input('tabs-without-value', 'value')])
def render_content(tab):
if tab == 'tab-1':
return html.H3('Default selected Tab content 1')
elif tab == 'tab-2':
return html.H3('Tab content 2')
self.startServer(app=app)
self.wait_for_text_to_equal('#tabs-content', 'Default selected Tab content 1')
self.snapshot('Tab 1 should be selected by default')
def test_graph_does_not_resize_in_tabs(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Dash Tabs component demo'),
dcc.Tabs(id="tabs-example", value='tab-1-example', children=[
dcc.Tab(label='Tab One', value='tab-1-example', id='tab-1'),
dcc.Tab(label='Tab Two', value='tab-2-example', id='tab-2'),
]),
html.Div(id='tabs-content-example')
])
@app.callback(Output('tabs-content-example', 'children'),
[Input('tabs-example', 'value')])
def render_content(tab):
if tab == 'tab-1-example':
return html.Div([
html.H3('Tab content 1'),
dcc.Graph(
id='graph-1-tabs',
figure={
'data': [{
'x': [1, 2, 3],
'y': [3, 1, 2],
'type': 'bar'
}]
}
)
])
elif tab == 'tab-2-example':
return html.Div([
html.H3('Tab content 2'),
dcc.Graph(
id='graph-2-tabs',
figure={
'data': [{
'x': [1, 2, 3],
'y': [5, 10, 6],
'type': 'bar'
}]
}
)
])
self.startServer(app=app)
tab_one = self.wait_for_element_by_css_selector('#tab-1')
tab_two = self.wait_for_element_by_css_selector('#tab-2')
WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.ID, "tab-2"))
)
self.snapshot("Tabs with Graph - initial (graph should not resize)")
tab_two.click()
# wait for Graph's internal svg to be loaded after clicking
WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "#graph-2-tabs .main-svg"))
)
self.snapshot("Tabs with Graph - clicked tab 2 (graph should not resize)")
WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.ID, "tab-1"))
)
tab_one.click()
# wait for Graph to be loaded after clicking
WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "#graph-1-tabs .main-svg"))
)
self.snapshot("Tabs with Graph - clicked tab 1 (graph should not resize)")
def test_location_link(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='waitfor'),
dcc.Location(id='test-location', refresh=False),
dcc.Link(
html.Button('I am a clickable button'),
id='test-link',
href='/test/pathname'),
dcc.Link(
html.Button('I am a clickable hash button'),
id='test-link-hash',
href='#test'),
dcc.Link(
html.Button('I am a clickable search button'),
id='test-link-search',
href='?testQuery=testValue',
refresh=False),
html.Button('I am a magic button that updates pathname',
id='test-button'),
html.A('link to click', href='/test/pathname/a', id='test-a'),
html.A('link to click', href='#test-hash', id='test-a-hash'),
html.A('link to click', href='?queryA=valueA', id='test-a-query'),
html.Div(id='test-pathname', children=[]),
html.Div(id='test-hash', children=[]),
html.Div(id='test-search', children=[]),
])
@app.callback(
output=Output(component_id='test-pathname',
component_property='children'),
inputs=[Input(component_id='test-location', component_property='pathname')])
def update_location_on_page(pathname):
return pathname
@app.callback(
output=Output(component_id='test-hash',
component_property='children'),
inputs=[Input(component_id='test-location', component_property='hash')])
def update_location_on_page(hash_val):
if hash_val is None:
return ''
return hash_val
@app.callback(
output=Output(component_id='test-search',
component_property='children'),
inputs=[Input(component_id='test-location', component_property='search')])
def update_location_on_page(search):
if search is None:
return ''
return search
@app.callback(
output=Output(component_id='test-location',
component_property='pathname'),
inputs=[Input(component_id='test-button',
component_property='n_clicks')],
state=[State(component_id='test-location', component_property='pathname')])
def update_pathname(n_clicks, current_pathname):
if n_clicks is not None:
return '/new/pathname'
return current_pathname
self.startServer(app=app)
time.sleep(1)
self.snapshot('link -- location')
# Check that link updates pathname
self.wait_for_element_by_css_selector('#test-link').click()
self.assertEqual(
self.driver.current_url.replace('http://localhost:8050', ''),
'/test/pathname')
self.wait_for_text_to_equal('#test-pathname', '/test/pathname')
# Check that hash is updated in the Location
self.wait_for_element_by_css_selector('#test-link-hash').click()
self.wait_for_text_to_equal('#test-pathname', '/test/pathname')
self.wait_for_text_to_equal('#test-hash', '#test')
self.snapshot('link -- /test/pathname#test')
# Check that search is updated in the Location -- note that this goes through href and therefore wipes the hash
self.wait_for_element_by_css_selector('#test-link-search').click()
self.wait_for_text_to_equal('#test-search', '?testQuery=testValue')
self.wait_for_text_to_equal('#test-hash', '')
self.snapshot('link -- /test/pathname?testQuery=testValue')
# Check that pathname is updated through a Button click via props
self.wait_for_element_by_css_selector('#test-button').click()
self.wait_for_text_to_equal('#test-pathname', '/new/pathname')
self.wait_for_text_to_equal('#test-search', '?testQuery=testValue')
self.snapshot('link -- /new/pathname?testQuery=testValue')
# Check that pathname is updated through an a tag click via props
self.wait_for_element_by_css_selector('#test-a').click()
try:
self.wait_for_element_by_css_selector('#waitfor')
except Exception as e:
print(self.wait_for_element_by_css_selector(
'#_dash-app-content').get_attribute('innerHTML'))
raise e
self.wait_for_text_to_equal('#test-pathname', '/test/pathname/a')
self.wait_for_text_to_equal('#test-search', '')
self.wait_for_text_to_equal('#test-hash', '')
self.snapshot('link -- /test/pathname/a')
# Check that hash is updated through an a tag click via props
self.wait_for_element_by_css_selector('#test-a-hash').click()
self.wait_for_text_to_equal('#test-pathname', '/test/pathname/a')
self.wait_for_text_to_equal('#test-search', '')
self.wait_for_text_to_equal('#test-hash', '#test-hash')
self.snapshot('link -- /test/pathname/a#test-hash')
# Check that hash is updated through an a tag click via props
self.wait_for_element_by_css_selector('#test-a-query').click()
self.wait_for_element_by_css_selector('#waitfor')
self.wait_for_text_to_equal('#test-pathname', '/test/pathname/a')
self.wait_for_text_to_equal('#test-search', '?queryA=valueA')
self.wait_for_text_to_equal('#test-hash', '')
self.snapshot('link -- /test/pathname/a?queryA=valueA')
def test_link_scroll(self):
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Location(id='test-url', refresh=False),
html.Div(id='push-to-bottom', children=[], style={
'display': 'block',
'height': '200vh'
}),
html.Div(id='page-content'),
dcc.Link('Test link', href='/test-link', id='test-link')
])
call_count = Value('i', 0)
@app.callback(Output('page-content', 'children'),
[Input('test-url', 'pathname')])
def display_page(pathname):
call_count.value = call_count.value + 1
return 'You are on page {}'.format(pathname)
self.startServer(app=app)
time.sleep(2)
# callback is called twice when defined
self.assertEqual(
call_count.value,
2
)
# test if link correctly scrolls back to top of page
test_link = self.wait_for_element_by_css_selector('#test-link')
test_link.send_keys(Keys.NULL)
test_link.click()
time.sleep(2)
# test link still fires update on Location
page_content = self.wait_for_element_by_css_selector('#page-content')
self.assertNotEqual(page_content.text, 'You are on page /')
self.wait_for_text_to_equal(
'#page-content', 'You are on page /test-link')
# test if rendered Link's <a> tag has a href attribute
link_href = test_link.get_attribute("href")
self.assertEqual(link_href, 'http://localhost:8050/test-link')
# test if callback is only fired once (offset of 2)
self.assertEqual(
call_count.value,
3
)
def test_candlestick(self):
app = dash.Dash(__name__)
app.layout = html.Div([
html.Button(
id='button',
children='Update Candlestick',
n_clicks=0
),
dcc.Graph(id='graph')
])
@app.callback(Output('graph', 'figure'), [Input('button', 'n_clicks')])
def update_graph(n_clicks):
return {
'data': [{
'open': [1] * 5,
'high': [3] * 5,
'low': [0] * 5,
'close': [2] * 5,
'x': [n_clicks] * 5,
'type': 'candlestick'
}]
}
self.startServer(app=app)
button = self.wait_for_element_by_css_selector('#button')
self.snapshot('candlestick - initial')
button.click()
time.sleep(1)
self.snapshot('candlestick - 1 click')
button.click()
time.sleep(1)
self.snapshot('candlestick - 2 click')
def test_graphs_with_different_figures(self):
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2],
'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5],
'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
),
dcc.Graph(
id='example-graph-2',
figure={
'data': [
{'x': [20, 24, 33], 'y': [5, 2, 3],
'type': 'bar', 'name': 'SF'},
{'x': [11, 22, 33], 'y': [22, 44, 55],
'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
),
])
self.startServer(app=app)
self.snapshot('2 graphs with different figures')
def test_graphs_without_ids(self):