Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/selenium/test_heat_map_selenium.py
1601 views
1
import base64
2
import io
3
import os
4
5
from PIL import Image
6
7
import folium
8
from folium.plugins.heat_map import HeatMap
9
from folium.utilities import temp_html_filepath
10
11
12
def test_heat_map_with_weights(driver):
13
"""Verify that HeatMap uses weights in data correctly.
14
15
This test will fail in non-headless mode because window size will be different.
16
17
"""
18
figure_width, figure_height = 800, 600
19
m = folium.Map(
20
(0.5, 0.5),
21
zoom_start=8,
22
tiles=None,
23
width=figure_width,
24
height=figure_height,
25
)
26
HeatMap(
27
# make four dots with different weights: 1, 1, 1.5 and 2.
28
data=[
29
(0, 0, 1.5),
30
(0, 1, 1),
31
(1, 0, 1),
32
(1, 1, 2),
33
],
34
radius=70,
35
blur=50,
36
).add_to(m)
37
html = m.get_root().render()
38
with temp_html_filepath(html) as filepath:
39
driver.get_file(filepath)
40
assert driver.wait_until(".folium-map")
41
driver.verify_js_logs()
42
canvas = driver.wait_until("canvas.leaflet-heatmap-layer")
43
assert canvas
44
# get the canvas as a PNG base64 string
45
canvas_base64 = driver.execute_script(
46
"return arguments[0].toDataURL('image/png').substring(21);", canvas
47
)
48
screenshot_bytes = base64.b64decode(canvas_base64)
49
screenshot = Image.open(io.BytesIO(screenshot_bytes))
50
# window size is not reliable, so crop to a smaller fixed size
51
screenshot = screenshot.crop((0, 0, figure_width, figure_height))
52
path = os.path.dirname(__file__)
53
with open(os.path.join(path, "test_heat_map_selenium_screenshot.png"), "rb") as f:
54
screenshot_expected = Image.open(f)
55
if list(screenshot.getdata()) != list(screenshot_expected.getdata()):
56
assert False, "screenshot is not as expected"
57
58