Path: blob/main/tests/selenium/test_selenium.py
1601 views
import base641import glob2import os3import subprocess4from html.parser import HTMLParser5from urllib.parse import unquote67import nbconvert8import pytest9from selenium.common.exceptions import UnexpectedAlertPresentException1011from folium.utilities import temp_html_filepath121314def find_notebooks():15"""Return a list of filenames of the example notebooks."""16path = os.path.dirname(__file__)17pattern = os.path.join(path, "..", "..", "docs", "**", "*.md")18files = glob.glob(pattern, recursive=True)19if files:20return files21else:22raise OSError("Could not find the notebooks")232425@pytest.mark.parametrize("filepath", find_notebooks())26def test_notebook(filepath, driver):27if "WmsTimeDimension" in filepath:28pytest.xfail("WmsTimeDimension.ipynb external resource makes this test flaky")29for filepath_html in get_notebook_html(filepath):30driver.get_file(filepath_html)31try:32assert driver.wait_until(".folium-map")33except UnexpectedAlertPresentException:34# in Plugins.ipynb we get an alert about geolocation permission35# for some reason it cannot be closed or avoided, so just ignore it36print("skipping", filepath_html, "because of alert")37continue38driver.verify_js_logs()394041def get_notebook_html(filepath_notebook):42"""Convert markdown to notebook to html files, remove them when done."""43subprocess.run(44[45"jupytext",46"--to",47"notebook",48"--execute",49filepath_notebook,50]51)52filepath_notebook = filepath_notebook.replace(".md", ".ipynb")5354html_exporter = nbconvert.HTMLExporter()55body, _ = html_exporter.from_filename(filepath_notebook)5657parser = IframeParser()58parser.feed(body)59iframes = parser.iframes6061for iframe in iframes:62with temp_html_filepath(iframe) as filepath_html:63yield filepath_html646566class IframeParser(HTMLParser):67"""Extract the iframes from an html page."""6869def __init__(self):70super().__init__()71self.iframes = []7273def handle_starttag(self, tag, attrs):74if tag == "iframe":75attrs = dict(attrs)76if "srcdoc" in attrs:77html_bytes = attrs["srcdoc"].encode()78elif "data-html" in attrs: # legacy79data_html = attrs["data-html"]80if "%" in data_html[:20]:81# newest branca version: data-html is percent-encoded82html_bytes = unquote(data_html).encode()83else:84# legacy branca version: data-html is base64 encoded85html_bytes = base64.b64decode(data_html)86else: # legacy87src = attrs["src"]88html_base64 = src.split(",")[-1]89html_bytes = base64.b64decode(html_base64)90self.iframes.append(html_bytes)919293