11import time
22import pytest
33
4- from selenium .webdriver .common .by import By
5-
64import dash_html_components as html
75import dash_core_components as dcc
86
1210from dash .exceptions import PreventUpdate
1311
1412
15- def findSyncPlotlyJs (scripts ):
16- for script in scripts :
17- if "dash_core_components/plotly" in script .get_attribute ("src" ):
18- return script
13+ def get_script_sources (dash_duo ):
14+ return [s .get_attribute ("src" ) for s in dash_duo .find_elements ("script" )]
15+
16+
17+ def hasSyncPlotlyJs (dash_duo ):
18+ return any ("dash_core_components/plotly" in s for s in get_script_sources (dash_duo ))
19+
20+
21+ def hasAsyncPlotlyJs (dash_duo ):
22+ return any (
23+ "dash_core_components/async-plotlyjs" in s for s in get_script_sources (dash_duo )
24+ )
1925
2026
21- def findAsyncPlotlyJs (scripts ):
22- for script in scripts :
23- if "dash_core_components/async-plotlyjs" in script .get_attribute ("src" ):
24- return script
27+ def hasWindowPlotly (dash_duo ):
28+ return dash_duo .driver .execute_script ("return !!window.Plotly" )
2529
2630
2731@pytest .mark .parametrize ("is_eager" , [True , False ])
28- def test_scripts (dash_duo , is_eager ):
32+ def test_scri001_scripts (dash_duo , is_eager ):
2933 app = Dash (__name__ , eager_loading = is_eager )
3034 app .layout = html .Div ([dcc .Graph (id = "output" , figure = {"data" : [{"y" : [3 , 1 , 2 ]}]})])
3135
@@ -37,16 +41,18 @@ def test_scripts(dash_duo, is_eager):
3741 dev_tools_hot_reload = False ,
3842 )
3943
40- # Give time for the async dependency to be requested (if any)
41- time . sleep ( 2 )
44+ # Wait for the graph to appear
45+ dash_duo . find_element ( ".js-plotly-plot" )
4246
43- scripts = dash_duo . driver . find_elements ( By . CSS_SELECTOR , "script" )
47+ assert hasSyncPlotlyJs ( dash_duo ) is is_eager
4448
45- assert (findSyncPlotlyJs (scripts ) is None ) is not is_eager
46- assert (findAsyncPlotlyJs (scripts ) is None ) is is_eager
49+ # Webpack 5 deletes the script tag immediately after evaluating it
50+ # https://github.com/plotly/dash/pull/1685#issuecomment-877199466
51+ assert hasAsyncPlotlyJs (dash_duo ) is False
52+ assert hasWindowPlotly (dash_duo ) is True
4753
4854
49- def test_scripts_on_request (dash_duo ):
55+ def test_scri002_scripts_on_request (dash_duo ):
5056 app = Dash (__name__ , eager_loading = False )
5157 app .layout = html .Div (id = "div" , children = [html .Button (id = "btn" )])
5258
@@ -68,15 +74,16 @@ def load_chart(n_clicks):
6874 # Give time for the async dependency to be requested (if any)
6975 time .sleep (2 )
7076
71- scripts = dash_duo . driver . find_elements ( By . CSS_SELECTOR , "script" )
72- assert findSyncPlotlyJs ( scripts ) is None
73- assert findAsyncPlotlyJs ( scripts ) is None
77+ assert hasSyncPlotlyJs ( dash_duo ) is False
78+ assert hasAsyncPlotlyJs ( dash_duo ) is False
79+ assert hasWindowPlotly ( dash_duo ) is False
7480
7581 dash_duo .find_element ("#btn" ).click ()
7682
77- # Give time for the async dependency to be requested (if any)
78- time . sleep ( 2 )
83+ # Wait for the graph to appear
84+ dash_duo . find_element ( ".js-plotly-plot" )
7985
80- scripts = dash_duo .driver .find_elements (By .CSS_SELECTOR , "script" )
81- assert findSyncPlotlyJs (scripts ) is None
82- assert findAsyncPlotlyJs (scripts ) is not None
86+ assert hasSyncPlotlyJs (dash_duo ) is False
87+ # Again, webpack 5 deletes the script tag immediately after evaluating it
88+ assert hasAsyncPlotlyJs (dash_duo ) is False
89+ assert hasWindowPlotly (dash_duo ) is True
0 commit comments