-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update link * update link, discard max_val * Update test_heat_map.py * Update url after rebase * Rename temp html file utility function * temp_html_filepath accept str and bytes * Refactor selenium tests * Small additional refactor of selenium fixture * Add HeatMap with weights selenium test * Set window size at driver load * Print screenshot if test fails * export canvas, not full screenshot * Use JS resource from folium master
- Loading branch information
Showing
8 changed files
with
118 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
import pytest | ||
from selenium.webdriver import Chrome, ChromeOptions | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support.ui import WebDriverWait | ||
from selenium.webdriver.support.expected_conditions import visibility_of_element_located | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def driver(): | ||
"""Pytest fixture that yields a Selenium WebDriver instance""" | ||
driver = DriverFolium() | ||
try: | ||
yield driver | ||
finally: | ||
driver.quit() | ||
|
||
|
||
class DriverFolium(Chrome): | ||
"""Selenium WebDriver wrapper that adds folium test specific features.""" | ||
|
||
def __init__(self): | ||
options = ChromeOptions() | ||
options.add_argument('--no-sandbox') | ||
options.add_argument('--disable-dev-shm-usage') | ||
options.add_argument('--disable-gpu') | ||
options.add_argument('--headless') | ||
options.add_argument("--window-size=1024,768") | ||
super().__init__(options=options) | ||
|
||
def get_file(self, filepath): | ||
self.clean_window() | ||
super().get('file://' + filepath) | ||
|
||
def clean_window(self): | ||
"""Make sure we have a fresh window (without restarting the browser).""" | ||
# open new tab | ||
self.execute_script('window.open();') | ||
# close old tab | ||
self.close() | ||
# switch to new tab | ||
self.switch_to.window(self.window_handles[0]) | ||
|
||
def verify_js_logs(self): | ||
"""Raise an error if there are errors in the browser JS console.""" | ||
logs = self.get_log('browser') | ||
for log in logs: | ||
if log['level'] == 'SEVERE': | ||
msg = ' '.join(log['message'].split()[2:]) | ||
raise RuntimeError('Javascript error: "{}".'.format(msg)) | ||
|
||
def wait_until(self, css_selector, timeout=10): | ||
"""Wait for and return the element(s) selected by css_selector.""" | ||
wait = WebDriverWait(self, timeout=timeout) | ||
is_visible = visibility_of_element_located((By.CSS_SELECTOR, css_selector)) | ||
return wait.until(is_visible) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import base64 | ||
import os | ||
|
||
import folium | ||
from folium.plugins.heat_map import HeatMap | ||
from folium.utilities import temp_html_filepath | ||
|
||
|
||
def test_heat_map_with_weights(driver): | ||
"""Verify that HeatMap uses weights in data correctly. | ||
This test will fail in non-headless mode because window size will be different. | ||
""" | ||
m = folium.Map((0.5, 0.5), zoom_start=8, tiles=None) | ||
HeatMap( | ||
# make four dots with different weights: 1, 1, 1.5 and 2. | ||
data=[ | ||
(0, 0, 1.5), | ||
(0, 1, 1), | ||
(1, 0, 1), | ||
(1, 1, 2), | ||
], | ||
radius=70, | ||
blur=50, | ||
).add_to(m) | ||
html = m.get_root().render() | ||
with temp_html_filepath(html) as filepath: | ||
driver.get_file(filepath) | ||
assert driver.wait_until('.folium-map') | ||
driver.verify_js_logs() | ||
canvas = driver.wait_until('canvas.leaflet-heatmap-layer') | ||
assert canvas | ||
# get the canvas as a PNG base64 string | ||
canvas_base64 = driver.execute_script( | ||
"return arguments[0].toDataURL('image/png').substring(21);", canvas) | ||
screenshot = base64.b64decode(canvas_base64) | ||
path = os.path.dirname(__file__) | ||
with open(os.path.join(path, 'test_heat_map_selenium_screenshot.png'), 'rb') as f: | ||
screenshot_expected = f.read() | ||
if hash(screenshot) != hash(screenshot_expected): | ||
print(screenshot) | ||
assert False, 'screenshot is not as expected' |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters