77
88import nbconvert
99import pytest
10- from selenium .webdriver import Chrome , ChromeOptions
1110from selenium .common .exceptions import UnexpectedAlertPresentException
12- from selenium .webdriver .common .by import By
13- from selenium .webdriver .support .ui import WebDriverWait
14- from selenium .webdriver .support .expected_conditions import visibility_of_element_located
1511
16-
17- def create_driver ():
18- """Create a Selenium WebDriver instance."""
19- options = ChromeOptions ()
20- options .add_argument ('--no-sandbox' )
21- options .add_argument ('--disable-dev-shm-usage' )
22- options .add_argument ('--disable-gpu' )
23- options .add_argument ('--headless' )
24- driver = Chrome (options = options )
25- return driver
26-
27-
28- @pytest .fixture (scope = 'module' )
29- def driver ():
30- """Pytest fixture that yields a Selenium WebDriver instance"""
31- driver = create_driver ()
32- try :
33- yield driver
34- finally :
35- driver .quit ()
36-
37-
38- def clean_window (driver ):
39- # open new tab
40- driver .execute_script ('window.open();' )
41- # close old tab
42- driver .close ()
43- # switch to new tab
44- driver .switch_to .window (driver .window_handles [0 ])
12+ from folium .utilities import temp_html_filepath
4513
4614
4715def find_notebooks ():
@@ -59,22 +27,15 @@ def find_notebooks():
5927@pytest .mark .parametrize ('filepath' , find_notebooks ())
6028def test_notebook (filepath , driver ):
6129 for filepath_html in get_notebook_html (filepath ):
62- clean_window (driver )
63- driver .get ('file://' + filepath_html )
64- wait = WebDriverWait (driver , timeout = 10 )
65- map_is_visible = visibility_of_element_located ((By .CSS_SELECTOR , '.folium-map' ))
30+ driver .get_file (filepath_html )
6631 try :
67- assert wait . until ( map_is_visible )
32+ assert driver . wait_until ( '.folium-map' )
6833 except UnexpectedAlertPresentException :
6934 # in Plugins.ipynb we get an alert about geolocation permission
7035 # for some reason it cannot be closed or avoided, so just ignore it
7136 print ('skipping' , filepath_html , 'because of alert' )
7237 continue
73- logs = driver .get_log ('browser' )
74- for log in logs :
75- if log ['level' ] == 'SEVERE' :
76- msg = ' ' .join (log ['message' ].split ()[2 :])
77- raise RuntimeError ('Javascript error: "{}".' .format (msg ))
38+ driver .verify_js_logs ()
7839
7940
8041def get_notebook_html (filepath_notebook , execute = True ):
@@ -93,15 +54,9 @@ def get_notebook_html(filepath_notebook, execute=True):
9354 parser .feed (body )
9455 iframes = parser .iframes
9556
96- for i , iframe in enumerate (iframes ):
97- filepath_html = filepath_notebook .replace ('.ipynb' , '.{}.html' .format (i ))
98- filepath_html = os .path .abspath (filepath_html )
99- with open (filepath_html , 'wb' ) as f :
100- f .write (iframe )
101- try :
57+ for iframe in iframes :
58+ with temp_html_filepath (iframe ) as filepath_html :
10259 yield filepath_html
103- finally :
104- os .remove (filepath_html )
10560
10661
10762class IframeParser (HTMLParser ):
0 commit comments