Selenium WebDriver wrapper that adds folium test specific features.
| 16 | |
| 17 | |
| 18 | class DriverFolium(Chrome): |
| 19 | """Selenium WebDriver wrapper that adds folium test specific features.""" |
| 20 | |
| 21 | def __init__(self): |
| 22 | options = ChromeOptions() |
| 23 | options.add_argument("--no-sandbox") |
| 24 | options.add_argument("--disable-dev-shm-usage") |
| 25 | options.add_argument("--disable-gpu") |
| 26 | options.add_argument("--headless") |
| 27 | options.add_argument("--window-size=1024,768") |
| 28 | super().__init__(options=options) |
| 29 | |
| 30 | def get_file(self, filepath): |
| 31 | self.clean_window() |
| 32 | super().get("file://" + filepath) |
| 33 | |
| 34 | def clean_window(self): |
| 35 | """Make sure we have a fresh window (without restarting the browser).""" |
| 36 | # open new tab |
| 37 | self.execute_script("window.open();") |
| 38 | # close old tab |
| 39 | self.close() |
| 40 | # switch to new tab |
| 41 | self.switch_to.window(self.window_handles[0]) |
| 42 | |
| 43 | def verify_js_logs(self): |
| 44 | """Raise an error if there are errors in the browser JS console.""" |
| 45 | logs = self.get_log("browser") |
| 46 | for log in logs: |
| 47 | if log["level"] == "SEVERE": |
| 48 | msg = " ".join(log["message"].split()[2:]) |
| 49 | raise RuntimeError(f'Javascript error: "{msg}".') |
| 50 | |
| 51 | def wait_until(self, css_selector, timeout=10): |
| 52 | """Wait for and return the element(s) selected by css_selector.""" |
| 53 | wait = WebDriverWait(self, timeout=timeout) |
| 54 | is_visible = visibility_of_element_located((By.CSS_SELECTOR, css_selector)) |
| 55 | return wait.until(is_visible) |