|
1 | 1 | import logging |
2 | 2 | import re |
| 3 | +import time |
3 | 4 | from typing import Literal |
4 | 5 |
|
5 | 6 | from selenium.common.exceptions import StaleElementReferenceException, TimeoutException |
@@ -291,6 +292,52 @@ def open_tracker_panel(self) -> BasePage: |
291 | 292 | self.get_element("shield-icon").click() |
292 | 293 | return self |
293 | 294 |
|
| 295 | + def search_and_check_if_suggestions_are_present( |
| 296 | + self, text, search_mode: str = "awesome", min_suggestions=0 |
| 297 | + ): |
| 298 | + """ |
| 299 | + Search in the given address bar and check if suggestions are present. |
| 300 | +
|
| 301 | + Args: |
| 302 | + text (str): Text to search for in the suggestions. |
| 303 | + search_mode(str): Search mode to use. Can be 'awesome' or 'search'. Defaults to 'awesome'. |
| 304 | + min_suggestions (int): Minimum number of suggestions to collect. |
| 305 | + """ |
| 306 | + if search_mode == "awesome": |
| 307 | + self.clear_awesome_bar() |
| 308 | + self.type_in_awesome_bar(text) |
| 309 | + time.sleep(0.5) |
| 310 | + return self.awesome_bar_has_suggestions() |
| 311 | + elif search_mode == "search": |
| 312 | + self.set_search_bar() |
| 313 | + self.type_in_search_bar(text) |
| 314 | + return self.search_bar_has_suggestions(min_suggestions) |
| 315 | + else: |
| 316 | + raise ValueError("search_mode must be either 'awesome' or 'search'") |
| 317 | + |
| 318 | + def awesome_bar_has_suggestions(self) -> bool: |
| 319 | + """Check if the awesome bar has any suggestions.""" |
| 320 | + self.wait_for_suggestions_present(1) |
| 321 | + suggestions = self.get_all_children("results-dropdown") |
| 322 | + return len(suggestions) > 2 |
| 323 | + |
| 324 | + @BasePage.context_chrome |
| 325 | + def search_bar_has_suggestions(self, min_suggestions: int = 0) -> bool: |
| 326 | + """Check if the legacy search bar has suggestions. if a style has max-height: 0px, then no suggestions are present.""" |
| 327 | + suggestion_container = self.get_element( |
| 328 | + "legacy-search-mode-suggestion-container" |
| 329 | + ) |
| 330 | + if min_suggestions > 2: |
| 331 | + return ( |
| 332 | + suggestion_container.find_element(By.XPATH, "./*[1]").tag_name |
| 333 | + == "richlistitem" |
| 334 | + ) |
| 335 | + else: |
| 336 | + has_children = self.driver.execute_script( |
| 337 | + "return arguments[0].children.length > 0;", suggestion_container |
| 338 | + ) |
| 339 | + return has_children |
| 340 | + |
294 | 341 | def wait_for_suggestions_present(self, at_least: int = 1): |
295 | 342 | """Wait until the suggestion list has at least one visible item.""" |
296 | 343 | self.set_chrome_context() |
|
0 commit comments