|
1 | 1 | import json
|
2 | 2 | import logging
|
3 | 3 |
|
| 4 | +from selenium.webdriver import Firefox |
4 | 5 | from selenium.webdriver.common.keys import Keys
|
5 | 6 | from selenium.webdriver.support import expected_conditions as EC
|
6 | 7 |
|
|
11 | 12 | class FindToolbar(BasePage):
|
12 | 13 | URL_TEMPLATE = ""
|
13 | 14 |
|
| 15 | + def __init__(self, driver: Firefox, **kwargs): |
| 16 | + super().__init__(driver, **kwargs) |
| 17 | + self.panel_ui = PanelUi(self.driver) |
| 18 | + self.match_dict = {} |
| 19 | + |
| 20 | + @BasePage.context_chrome |
14 | 21 | def open(self) -> BasePage:
|
15 | 22 | """Use PanelUi to open the Find Toolbar, wait for element to load"""
|
16 |
| - panel_ui = PanelUi(self.driver) |
17 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
18 |
| - panel_ui.open_panel_menu() |
19 |
| - panel_ui.select_panel_setting("find-in-page") |
20 |
| - self.wait_for_page_to_load() |
| 23 | + self.panel_ui.open_panel_menu() |
| 24 | + self.panel_ui.select_panel_setting("find-in-page") |
| 25 | + self.wait_for_page_to_load() |
21 | 26 | return self
|
22 | 27 |
|
| 28 | + @BasePage.context_chrome |
23 | 29 | def open_with_key_combo(self) -> BasePage:
|
24 | 30 | """Use Cmd/Ctrl + F to open the Find Toolbar, wait for load"""
|
25 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
26 |
| - if self.sys_platform() == "Darwin": |
27 |
| - mod_key = Keys.COMMAND |
28 |
| - else: |
29 |
| - mod_key = Keys.CONTROL |
30 |
| - self.perform_key_combo(mod_key, "f") |
31 |
| - self.wait_for_page_to_load() |
| 31 | + if self.sys_platform() == "Darwin": |
| 32 | + mod_key = Keys.COMMAND |
| 33 | + else: |
| 34 | + mod_key = Keys.CONTROL |
| 35 | + self.perform_key_combo(mod_key, "f") |
| 36 | + self.wait_for_page_to_load() |
32 | 37 | return self
|
33 | 38 |
|
| 39 | + @BasePage.context_chrome |
34 | 40 | def find(self, term: str) -> BasePage:
|
35 | 41 | """Use the Find Toolbar to search"""
|
36 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
37 |
| - findbar = self.get_element("find-toolbar-input") |
38 |
| - findbar.click() |
39 |
| - findbar.clear() |
40 |
| - findbar.send_keys(term + Keys.ENTER) |
| 42 | + find_bar = self.get_element("find-toolbar-input") |
| 43 | + find_bar.click() |
| 44 | + find_bar.clear() |
| 45 | + find_bar.send_keys(term + Keys.ENTER) |
| 46 | + if find_bar.get_property("status") != "notfound": |
| 47 | + self.match_dict = self.get_match_args() |
41 | 48 | return self
|
42 | 49 |
|
| 50 | + @BasePage.context_chrome |
43 | 51 | def get_match_args(self) -> dict:
|
44 | 52 | """Return the status of the find session"""
|
45 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
46 |
| - matches = self.get_element("matches-label") |
47 |
| - self.expect( |
48 |
| - EC.text_to_be_present_in_element_attribute( |
49 |
| - self.get_selector("matches-label"), "data-l10n-args", ":" |
50 |
| - ) |
| 53 | + self.expect( |
| 54 | + EC.text_to_be_present_in_element_attribute( |
| 55 | + self.get_selector("matches-label"), "data-l10n-args", ":" |
51 | 56 | )
|
52 |
| - logging.info(matches.get_attribute("outerHTML")) |
53 |
| - match_status_str = matches.get_attribute("data-l10n-args") |
54 |
| - return json.loads(match_status_str) |
| 57 | + ) |
| 58 | + matches = self.get_element("matches-label") |
| 59 | + logging.info(matches.get_attribute("outerHTML")) |
| 60 | + match_status_str = matches.get_attribute("data-l10n-args") |
| 61 | + self.match_dict = json.loads(match_status_str) |
| 62 | + return self.match_dict |
55 | 63 |
|
| 64 | + @BasePage.context_chrome |
56 | 65 | def next_match(self) -> BasePage:
|
57 | 66 | """Click the Next Match button"""
|
58 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
59 |
| - self.get_element("next-match-button").click() |
| 67 | + self.get_element("next-match-button").click() |
| 68 | + if self.match_dict["current"] < self.match_dict["total"]: |
| 69 | + self.match_dict["current"] += 1 |
| 70 | + else: |
| 71 | + self.match_dict["current"] = 1 |
60 | 72 | return self
|
61 | 73 |
|
| 74 | + @BasePage.context_chrome |
62 | 75 | def previous_match(self) -> BasePage:
|
63 | 76 | """Click the Previous Match button"""
|
64 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
65 |
| - self.get_element("previous-match-button").click() |
| 77 | + self.get_element("previous-match-button").click() |
| 78 | + if self.match_dict["current"] > 1: |
| 79 | + self.match_dict["current"] -= 1 |
| 80 | + else: |
| 81 | + self.match_dict["current"] = self.match_dict["total"] |
66 | 82 | return self
|
67 | 83 |
|
| 84 | + @BasePage.context_chrome |
68 | 85 | def rewind_to_first_match(self) -> BasePage:
|
69 | 86 | """Go back to match 1 of n"""
|
70 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
71 |
| - position = self.get_match_args()["current"] |
72 |
| - total = self.get_match_args()["total"] |
73 |
| - while position != 1: |
74 |
| - if position < total // 2: |
75 |
| - self.previous_match() |
76 |
| - else: |
77 |
| - self.next_match() |
78 |
| - position = self.get_match_args()["current"] |
| 87 | + while self.match_dict["current"] != 1: |
| 88 | + self.previous_match() |
79 | 89 | return self
|
80 | 90 |
|
| 91 | + @BasePage.context_chrome |
81 | 92 | def navigate_matches_by_keys(self, backwards=False) -> BasePage:
|
82 | 93 | """Use F3 and Shift+F3 to navigate matches"""
|
83 |
| - with self.driver.context(self.driver.CONTEXT_CHROME): |
84 |
| - if backwards: |
85 |
| - self.perform_key_combo(Keys.SHIFT, Keys.F3) |
86 |
| - else: |
87 |
| - logging.info(f"sending {Keys.F3.encode()}") |
88 |
| - self.actions.send_keys(Keys.F3).perform() |
89 |
| - return self |
| 94 | + if backwards: |
| 95 | + self.perform_key_combo(Keys.SHIFT, Keys.F3) |
| 96 | + else: |
| 97 | + logging.info(f"sending {Keys.F3.encode()}") |
| 98 | + self.actions.send_keys(Keys.F3).perform() |
| 99 | + return self |
| 100 | + |
| 101 | + @BasePage.context_chrome |
| 102 | + def navigate_matches_n_times(self, n: int): |
| 103 | + for _ in range(n): |
| 104 | + self.next_match() |
0 commit comments