Path: blob/main/tests/selenium/test_heat_map_selenium.py
1601 views
import base641import io2import os34from PIL import Image56import folium7from folium.plugins.heat_map import HeatMap8from folium.utilities import temp_html_filepath91011def test_heat_map_with_weights(driver):12"""Verify that HeatMap uses weights in data correctly.1314This test will fail in non-headless mode because window size will be different.1516"""17figure_width, figure_height = 800, 60018m = folium.Map(19(0.5, 0.5),20zoom_start=8,21tiles=None,22width=figure_width,23height=figure_height,24)25HeatMap(26# make four dots with different weights: 1, 1, 1.5 and 2.27data=[28(0, 0, 1.5),29(0, 1, 1),30(1, 0, 1),31(1, 1, 2),32],33radius=70,34blur=50,35).add_to(m)36html = m.get_root().render()37with temp_html_filepath(html) as filepath:38driver.get_file(filepath)39assert driver.wait_until(".folium-map")40driver.verify_js_logs()41canvas = driver.wait_until("canvas.leaflet-heatmap-layer")42assert canvas43# get the canvas as a PNG base64 string44canvas_base64 = driver.execute_script(45"return arguments[0].toDataURL('image/png').substring(21);", canvas46)47screenshot_bytes = base64.b64decode(canvas_base64)48screenshot = Image.open(io.BytesIO(screenshot_bytes))49# window size is not reliable, so crop to a smaller fixed size50screenshot = screenshot.crop((0, 0, figure_width, figure_height))51path = os.path.dirname(__file__)52with open(os.path.join(path, "test_heat_map_selenium_screenshot.png"), "rb") as f:53screenshot_expected = Image.open(f)54if list(screenshot.getdata()) != list(screenshot_expected.getdata()):55assert False, "screenshot is not as expected"565758