Skip to content

Commit 8d8f563

Browse files
committed
Extended Panel UI methods and tests
1 parent 73c5661 commit 8d8f563

File tree

3 files changed

+169
-71
lines changed

3 files changed

+169
-71
lines changed

foxpuppet/windows/browser/panel_ui/panel_ui.py

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,80 @@ def open_panel_menu(self) -> None:
5050
with self.selenium.context(self.selenium.CONTEXT_CHROME):
5151
self.selenium.find_element(*PanelUILocators.PANEL_UI_BUTTON).click()
5252

53+
def open_new_tab(self) -> None:
54+
"""
55+
Opens a new tab using the Panel UI menu.
56+
"""
57+
self.open_panel_menu()
58+
with self.selenium.context(self.selenium.CONTEXT_CHROME):
59+
self.selenium.find_element(*PanelUILocators.NEW_TAB).click()
60+
61+
def open_new_window(self) -> None:
62+
"""
63+
Opens a new window using the Panel UI menu.
64+
"""
65+
self.open_panel_menu()
66+
with self.selenium.context(self.selenium.CONTEXT_CHROME):
67+
self.selenium.find_element(*PanelUILocators.NEW_WINDOW).click()
68+
5369
def open_private_window(self) -> None:
5470
"""
5571
Opens a new window in private browsing mode using the Panel UI menu.
5672
"""
57-
# self.open_panel_menu()
73+
self.open_panel_menu()
5874
with self.selenium.context(self.selenium.CONTEXT_CHROME):
5975
self.selenium.find_element(*PanelUILocators.PRIVATE_WINDOW).click()
6076

6177
def open_history_menu(self) -> None:
6278
"""
6379
Opens the History in Panel UI Menu
6480
"""
65-
# self.open_panel_menu()
81+
self.open_panel_menu()
6682
with self.selenium.context(self.selenium.CONTEXT_CHROME):
6783
self.selenium.find_element(*PanelUILocators.HISTORY).click()
6884

69-
def wait_for_num_windows_or_tabs(self, expected_count: int) -> None:
85+
def wait_for_num_windows_or_tabs(self, expected_count: int) -> bool:
7086
"""
7187
Waits until the number of open browser windows or tabs matches the expected value.
7288
7389
Args:
7490
expected_count (int): The expected number of windows or tabs.
7591
"""
7692
self.wait.until(
77-
lambda _: len(self.driver.window_handles) == expected_count,
78-
f"Expected {expected_count} windows or tabs, but found {len(self.driver.window_handles)}",
93+
lambda _: len(self.selenium.window_handles) == expected_count,
94+
f"Expected {expected_count} windows or tabs, but found {len(self.selenium.window_handles)}",
7995
)
96+
return True
8097

8198
def switch_to_new_window_or_tab(self) -> None:
8299
"""Get list of all window handles, switch to the newly opened tab/window"""
83100
handles = self.selenium.window_handles
84101
self.selenium.switch_to.window(handles[-1])
85102

103+
def awesome_bar(self, links: list) -> list:
104+
"""Check if the provided links are present in the awesome bar's suggestion links."""
105+
urls = []
106+
with self.selenium.context(self.selenium.CONTEXT_CHROME):
107+
for link in links:
108+
awesome_bar = self.selenium.find_element(*PanelUILocators.INPUT_FIELD)
109+
awesome_bar.clear()
110+
awesome_bar.send_keys(link)
111+
112+
self.wait.until(
113+
lambda _: self.selenium.find_elements(*PanelUILocators.SEARCH_RESULTS)
114+
)
115+
116+
search_results = self.selenium.find_elements(
117+
*PanelUILocators.SEARCH_RESULT_ITEMS
118+
)
86119

87-
class History(PanelUI):
88-
"""Handles interactions with Firefox History."""
120+
for result in search_results:
121+
url_span = result.find_element(*PanelUILocators.SEARCH_RESULT_ITEM)
122+
if url_span.text in link:
123+
if len(url_span.text) != 0:
124+
urls.append(link)
125+
break
126+
return urls
89127

90128
def is_present(self, link: str) -> bool:
91129
"""
@@ -132,9 +170,23 @@ def clear_history(self):
132170
)
133171
self.selenium.switch_to.default_content()
134172

173+
def verify_links_in_awesome_bar(self, links: list, new_window: bool = False) -> list:
174+
"""
175+
Verifies that the provided links appear in the awesome bar.
176+
Args:
177+
links `list`: A list of links to be verified.
135178
136-
class PrivateWindow(PanelUI):
137-
"""Handles interactions with Firefox Private Window."""
179+
Returns:
180+
list: A list of links that were not found in the awesome bar.
181+
"""
182+
if new_window:
183+
self.open_new_window()
184+
else:
185+
self.open_new_tab()
186+
self.switch_to_new_window_or_tab()
187+
for link in links:
188+
self.selenium.get(link)
189+
return self.awesome_bar(links)
138190

139191
def verify_private_browsing_links_not_in_awesome_bar(self, links: list) -> list:
140192
"""
@@ -145,36 +197,43 @@ def verify_private_browsing_links_not_in_awesome_bar(self, links: list) -> list:
145197
Returns:
146198
list: A list of links that appeared in the awesome bar during private browsing.
147199
"""
148-
invalid_links = []
149-
initial_window_handle = self.driver.current_window_handle
150-
# self.open_private_window()
200+
initial_window_handle = self.selenium.current_window_handle
201+
self.open_private_window()
151202
self.switch_to_new_window_or_tab()
152203

153204
for link in links:
154205
self.selenium.get(link)
155206

156-
self.driver.switch_to.window(initial_window_handle)
157-
158-
with self.selenium.context(self.selenium.CONTEXT_CHROME):
159-
for link in links:
160-
awesome_bar = self.selenium.find_element(*PanelUILocators.INPUT_FIELD)
161-
awesome_bar.clear()
162-
awesome_bar.send_keys(link)
163-
164-
self.wait.until(
165-
lambda _: self.selenium.find_elements(*PanelUILocators.SEARCH_RESULTS)
166-
)
207+
self.selenium.switch_to.window(initial_window_handle)
208+
return self.awesome_bar(links)
167209

168-
search_results = self.selenium.find_elements(
169-
*PanelUILocators.SEARCH_RESULTS
170-
)
171-
if any(link in result.text for result in search_results):
172-
invalid_links.append(link)
210+
def verify_links_in_history(self, links: list, new_window: bool = False) -> list:
211+
"""
212+
Verifies that the provided links appear in the history.
213+
Args:
214+
links `list`: A list of links to be verified.
173215
174-
return invalid_links
216+
Returns:
217+
list: A list of links that were not found in the history.
218+
"""
219+
if new_window:
220+
self.open_new_window()
221+
else:
222+
self.open_new_tab()
223+
self.switch_to_new_window_or_tab()
224+
for link in links:
225+
self.selenium.get(link)
226+
self.open_panel_menu()
227+
self.open_history_menu()
228+
urls = []
229+
for link in links:
230+
if not self.is_present(link):
231+
urls.append(link)
232+
return urls
175233

176234
def verify_private_browsing_links_not_in_history(
177-
self, links: list, history: History
235+
self,
236+
links: list,
178237
) -> list:
179238
"""
180239
Verifies that the provided links visited in private browsing session do not appear in the history.
@@ -186,8 +245,8 @@ def verify_private_browsing_links_not_in_history(
186245
list: A list of links that were found in the history during the private browsing session.
187246
"""
188247
invalid_links = []
189-
initial_window_handle = self.driver.current_window_handle
190-
# self.open_private_window()
248+
initial_window_handle = self.selenium.current_window_handle
249+
self.open_private_window()
191250
self.switch_to_new_window_or_tab()
192251

193252
for link in links:
@@ -197,15 +256,18 @@ def verify_private_browsing_links_not_in_history(
197256
self.open_panel_menu()
198257
self.open_history_menu()
199258
for link in links:
200-
if history.is_present(link):
259+
if self.is_present(link):
201260
invalid_links.append(link)
202261
return invalid_links
203262

204263

205264
class PanelUILocators:
206265
PANEL_UI_BUTTON = (By.ID, "PanelUI-menu-button")
266+
NEW_TAB = (By.ID, "appMenu-new-tab-button2")
267+
NEW_WINDOW = (By.ID, "appMenu-new-window-button2")
207268
PRIVATE_WINDOW = (By.ID, "appMenu-new-private-window-button2")
208269
HISTORY = (By.ID, "appMenu-history-button")
270+
INPUT_FIELD = (By.ID, "urlbar-input")
209271
CLEAR_RECENT_HISTORY = (By.ID, "appMenuClearRecentHistory")
210272
CLEAR_RECENT_HISTORY_BUTTON = (By.CSS_SELECTOR, "button[dlgtype='accept']")
211273
CLEAR_HISTORY_EVERYTHING = (By.CSS_SELECTOR, "menuitem[value='0']")
@@ -216,10 +278,11 @@ class PanelUILocators:
216278
"#appMenu_historyMenu toolbarbutton.subviewbutton",
217279
)
218280
HISTORY_IFRAME = (By.CSS_SELECTOR, "browser.dialogFrame")
281+
SEARCH_RESULTS = (By.ID, "urlbar-results")
282+
SEARCH_RESULT_ITEMS = (By.CSS_SELECTOR, "div.urlbarView-row[role='presentation']")
283+
SEARCH_RESULT_ITEM = (By.CSS_SELECTOR, "span.urlbarView-url")
219284

220285

221286
PANEL_ITEMS = {
222287
"PanelUI-menu-button": PanelUI,
223-
"appMenu-history-button": History,
224-
"appMenu-new-private-window-button2": PrivateWindow,
225288
}

foxpuppet/windows/browser/window.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class BrowserWindow(BaseWindow):
2323
"""Representation of a browser window."""
2424

25-
_bookmark_locator = (By.ID, "main-window") # editBookmarkPanelTemplate
25+
_bookmark_locator = (By.ID, "main-window")
2626
_file_menu_button_locator = (By.ID, "file-menu")
2727
_file_menu_private_window_locator = (By.ID, "menu_newPrivateWindow")
2828
_file_menu_new_window_button_locator = (By.ID, "menu_newNavigator")
@@ -33,10 +33,6 @@ class BrowserWindow(BaseWindow):
3333
By.CSS_SELECTOR,
3434
"#appMenu-notification-popup popupnotification",
3535
)
36-
_app_menu_panel_ui_locator = (
37-
By.CSS_SELECTOR,
38-
"#appMenu-mainView .panel-subview-body toolbarbutton",
39-
)
4036
_tab_browser_locator = (By.ID, "tabbrowser-tabs")
4137

4238
@property
@@ -97,20 +93,6 @@ def panel_ui(self) -> PanelUI | Any:
9793
panel_root = PanelUI.create(self, root)
9894
except NoSuchElementException:
9995
pass
100-
101-
try:
102-
panel_items = self.selenium.find_elements(
103-
*self._app_menu_panel_ui_locator
104-
)
105-
for item in panel_items:
106-
_id = item.get_property("id")
107-
from foxpuppet.windows.browser.panel_ui.panel_ui import PANEL_ITEMS
108-
109-
if _id in PANEL_ITEMS and item.is_displayed():
110-
panel_root = PANEL_ITEMS[_id].create(self, item) # type: ignore
111-
except StopIteration:
112-
pass
113-
11496
return panel_root
11597

11698
def wait_for_notification(

tests/test_panel_ui.py

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,95 @@
66
import pytest
77
from selenium.webdriver.remote.webdriver import WebDriver
88
from foxpuppet.windows import BrowserWindow
9-
from foxpuppet.windows.browser.panel_ui.panel_ui import PanelUI, History
9+
from foxpuppet.windows.browser.panel_ui.panel_ui import PanelUI
10+
11+
12+
links = [
13+
"https://www.mozilla.org/en-US/?v=a",
14+
"https://www.youtube.com",
15+
"https://www.facebook.com/",
16+
]
1017

1118

1219
@pytest.fixture
13-
def history(browser: BrowserWindow) -> History | None:
14-
"""Create History panel object.
20+
def panel_ui(browser: BrowserWindow) -> PanelUI | None:
21+
"""Create Panel UI object.
1522
1623
Args:
1724
browser: BrowserWindow instance
1825
1926
Returns:
20-
:py:class:`History`: FoxPuppet History panel object
27+
:py:class:`PanelUI`: FoxPuppet Panel UI object
2128
"""
22-
panel_ui = browser.wait_for_panel_ui(PanelUI)
23-
if panel_ui is not None:
24-
panel_ui.open_panel_menu()
25-
return browser.wait_for_panel_ui(History)
29+
return browser.wait_for_panel_ui(PanelUI)
30+
31+
32+
def test_open_new_tab(panel_ui: PanelUI) -> None:
33+
"""Test opening a new tab using the Panel UI."""
34+
panel_ui.open_new_tab()
35+
assert panel_ui.wait_for_num_windows_or_tabs(2)
36+
37+
38+
def test_open_new_window(panel_ui: PanelUI) -> None:
39+
"""Test opening a new window using the Panel UI."""
40+
panel_ui.open_new_window()
41+
assert panel_ui.wait_for_num_windows_or_tabs(2)
42+
43+
44+
def test_verify_links_open_in_new_tab_in_awesome_bar(panel_ui: PanelUI) -> None:
45+
"""Test that links opened in new tab are present in the awesome bar."""
46+
link = ["https://www.mozilla.org/en-US/?v=a"]
47+
urls = panel_ui.verify_links_in_awesome_bar(link)
48+
assert len(urls) == 1
49+
2650

51+
def test_verify_links_open_in_new_tab_in_history(panel_ui: PanelUI) -> None:
52+
"""Test that links opened in new tab are present in browser history."""
53+
urls = panel_ui.verify_links_in_history(links)
54+
assert len(urls) == 3
2755

28-
def test_url_is_present_in_history(history: History, selenium: WebDriver) -> None:
56+
57+
def test_verify_links_open_in_new_window_in_history(panel_ui: PanelUI) -> None:
58+
"""Test that links opened in new window are present in browser history."""
59+
urls = panel_ui.verify_links_in_history(links, new_window=True)
60+
assert len(urls) == 3
61+
62+
63+
def test_verify_links_open_in_new_window_in_awesome_bar(panel_ui: PanelUI) -> None:
64+
"""Test that links opened in new window are present in the awesome bar."""
65+
link = ["https://www.mozilla.org/en-US/?v=a"]
66+
urls = panel_ui.verify_links_in_awesome_bar(link, new_window=True)
67+
assert len(urls) == 1
68+
69+
70+
def test_url_is_present_in_history(panel_ui: PanelUI, selenium: WebDriver) -> None:
2971
"""Test that visited URL appears in browser history."""
3072
url = "https://www.mozilla.org/en-US/?v=a"
3173
selenium.get(url)
32-
history.open_history_menu()
33-
assert history.is_present(url)
74+
panel_ui.open_history_menu()
75+
assert panel_ui.is_present(url)
3476

3577

36-
def test_clear_recent_history(history: History, selenium: WebDriver) -> None:
78+
def test_clear_recent_history(panel_ui: PanelUI, selenium: WebDriver) -> None:
3779
"""Test clearing browser history removes visited URLs."""
3880
url = "https://www.mozilla.org/en-US/?v=a"
3981
selenium.get(url)
40-
history.open_history_menu()
41-
history.clear_history()
82+
panel_ui.open_history_menu()
83+
panel_ui.clear_history()
4284
import time
4385

4486
time.sleep(1)
45-
history.open_panel_menu()
46-
history.open_history_menu()
47-
assert not history.is_present(url)
87+
panel_ui.open_history_menu()
88+
assert not panel_ui.is_present(url)
89+
90+
91+
def test_verify_private_browsing_links_not_in_history(panel_ui: PanelUI) -> None:
92+
"""Test that links opened in private window are not present in browser history."""
93+
invalid_links = panel_ui.verify_private_browsing_links_not_in_history(links)
94+
assert not invalid_links
95+
96+
97+
def test_verify_private_browsing_links_not_in_awesome_bar(panel_ui: PanelUI) -> None:
98+
"""Test that links opened in private window are not present in the awesome bar."""
99+
invalid_links = panel_ui.verify_private_browsing_links_not_in_awesome_bar(links)
100+
assert not invalid_links

0 commit comments

Comments
 (0)