Skip to content

Commit

Permalink
Extended Panel UI methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Temidayo32 committed Feb 4, 2025
1 parent 73c5661 commit 8d8f563
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 71 deletions.
133 changes: 98 additions & 35 deletions foxpuppet/windows/browser/panel_ui/panel_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,80 @@ def open_panel_menu(self) -> None:
with self.selenium.context(self.selenium.CONTEXT_CHROME):
self.selenium.find_element(*PanelUILocators.PANEL_UI_BUTTON).click()

def open_new_tab(self) -> None:
"""
Opens a new tab using the Panel UI menu.
"""
self.open_panel_menu()
with self.selenium.context(self.selenium.CONTEXT_CHROME):
self.selenium.find_element(*PanelUILocators.NEW_TAB).click()

def open_new_window(self) -> None:
"""
Opens a new window using the Panel UI menu.
"""
self.open_panel_menu()
with self.selenium.context(self.selenium.CONTEXT_CHROME):
self.selenium.find_element(*PanelUILocators.NEW_WINDOW).click()

def open_private_window(self) -> None:
"""
Opens a new window in private browsing mode using the Panel UI menu.
"""
# self.open_panel_menu()
self.open_panel_menu()
with self.selenium.context(self.selenium.CONTEXT_CHROME):
self.selenium.find_element(*PanelUILocators.PRIVATE_WINDOW).click()

def open_history_menu(self) -> None:
"""
Opens the History in Panel UI Menu
"""
# self.open_panel_menu()
self.open_panel_menu()
with self.selenium.context(self.selenium.CONTEXT_CHROME):
self.selenium.find_element(*PanelUILocators.HISTORY).click()

def wait_for_num_windows_or_tabs(self, expected_count: int) -> None:
def wait_for_num_windows_or_tabs(self, expected_count: int) -> bool:
"""
Waits until the number of open browser windows or tabs matches the expected value.
Args:
expected_count (int): The expected number of windows or tabs.
"""
self.wait.until(
lambda _: len(self.driver.window_handles) == expected_count,
f"Expected {expected_count} windows or tabs, but found {len(self.driver.window_handles)}",
lambda _: len(self.selenium.window_handles) == expected_count,
f"Expected {expected_count} windows or tabs, but found {len(self.selenium.window_handles)}",
)
return True

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

def awesome_bar(self, links: list) -> list:
"""Check if the provided links are present in the awesome bar's suggestion links."""
urls = []
with self.selenium.context(self.selenium.CONTEXT_CHROME):
for link in links:
awesome_bar = self.selenium.find_element(*PanelUILocators.INPUT_FIELD)
awesome_bar.clear()
awesome_bar.send_keys(link)

self.wait.until(
lambda _: self.selenium.find_elements(*PanelUILocators.SEARCH_RESULTS)
)

search_results = self.selenium.find_elements(
*PanelUILocators.SEARCH_RESULT_ITEMS
)

class History(PanelUI):
"""Handles interactions with Firefox History."""
for result in search_results:
url_span = result.find_element(*PanelUILocators.SEARCH_RESULT_ITEM)
if url_span.text in link:
if len(url_span.text) != 0:
urls.append(link)
break
return urls

def is_present(self, link: str) -> bool:
"""
Expand Down Expand Up @@ -132,9 +170,23 @@ def clear_history(self):
)
self.selenium.switch_to.default_content()

def verify_links_in_awesome_bar(self, links: list, new_window: bool = False) -> list:
"""
Verifies that the provided links appear in the awesome bar.
Args:
links `list`: A list of links to be verified.
class PrivateWindow(PanelUI):
"""Handles interactions with Firefox Private Window."""
Returns:
list: A list of links that were not found in the awesome bar.
"""
if new_window:
self.open_new_window()
else:
self.open_new_tab()
self.switch_to_new_window_or_tab()
for link in links:
self.selenium.get(link)
return self.awesome_bar(links)

def verify_private_browsing_links_not_in_awesome_bar(self, links: list) -> list:
"""
Expand All @@ -145,36 +197,43 @@ def verify_private_browsing_links_not_in_awesome_bar(self, links: list) -> list:
Returns:
list: A list of links that appeared in the awesome bar during private browsing.
"""
invalid_links = []
initial_window_handle = self.driver.current_window_handle
# self.open_private_window()
initial_window_handle = self.selenium.current_window_handle
self.open_private_window()
self.switch_to_new_window_or_tab()

for link in links:
self.selenium.get(link)

self.driver.switch_to.window(initial_window_handle)

with self.selenium.context(self.selenium.CONTEXT_CHROME):
for link in links:
awesome_bar = self.selenium.find_element(*PanelUILocators.INPUT_FIELD)
awesome_bar.clear()
awesome_bar.send_keys(link)

self.wait.until(
lambda _: self.selenium.find_elements(*PanelUILocators.SEARCH_RESULTS)
)
self.selenium.switch_to.window(initial_window_handle)
return self.awesome_bar(links)

search_results = self.selenium.find_elements(
*PanelUILocators.SEARCH_RESULTS
)
if any(link in result.text for result in search_results):
invalid_links.append(link)
def verify_links_in_history(self, links: list, new_window: bool = False) -> list:
"""
Verifies that the provided links appear in the history.
Args:
links `list`: A list of links to be verified.
return invalid_links
Returns:
list: A list of links that were not found in the history.
"""
if new_window:
self.open_new_window()
else:
self.open_new_tab()
self.switch_to_new_window_or_tab()
for link in links:
self.selenium.get(link)
self.open_panel_menu()
self.open_history_menu()
urls = []
for link in links:
if not self.is_present(link):
urls.append(link)
return urls

def verify_private_browsing_links_not_in_history(
self, links: list, history: History
self,
links: list,
) -> list:
"""
Verifies that the provided links visited in private browsing session do not appear in the history.
Expand All @@ -186,8 +245,8 @@ def verify_private_browsing_links_not_in_history(
list: A list of links that were found in the history during the private browsing session.
"""
invalid_links = []
initial_window_handle = self.driver.current_window_handle
# self.open_private_window()
initial_window_handle = self.selenium.current_window_handle
self.open_private_window()
self.switch_to_new_window_or_tab()

for link in links:
Expand All @@ -197,15 +256,18 @@ def verify_private_browsing_links_not_in_history(
self.open_panel_menu()
self.open_history_menu()
for link in links:
if history.is_present(link):
if self.is_present(link):
invalid_links.append(link)
return invalid_links


class PanelUILocators:
PANEL_UI_BUTTON = (By.ID, "PanelUI-menu-button")
NEW_TAB = (By.ID, "appMenu-new-tab-button2")
NEW_WINDOW = (By.ID, "appMenu-new-window-button2")
PRIVATE_WINDOW = (By.ID, "appMenu-new-private-window-button2")
HISTORY = (By.ID, "appMenu-history-button")
INPUT_FIELD = (By.ID, "urlbar-input")
CLEAR_RECENT_HISTORY = (By.ID, "appMenuClearRecentHistory")
CLEAR_RECENT_HISTORY_BUTTON = (By.CSS_SELECTOR, "button[dlgtype='accept']")
CLEAR_HISTORY_EVERYTHING = (By.CSS_SELECTOR, "menuitem[value='0']")
Expand All @@ -216,10 +278,11 @@ class PanelUILocators:
"#appMenu_historyMenu toolbarbutton.subviewbutton",
)
HISTORY_IFRAME = (By.CSS_SELECTOR, "browser.dialogFrame")
SEARCH_RESULTS = (By.ID, "urlbar-results")
SEARCH_RESULT_ITEMS = (By.CSS_SELECTOR, "div.urlbarView-row[role='presentation']")
SEARCH_RESULT_ITEM = (By.CSS_SELECTOR, "span.urlbarView-url")


PANEL_ITEMS = {
"PanelUI-menu-button": PanelUI,
"appMenu-history-button": History,
"appMenu-new-private-window-button2": PrivateWindow,
}
20 changes: 1 addition & 19 deletions foxpuppet/windows/browser/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class BrowserWindow(BaseWindow):
"""Representation of a browser window."""

_bookmark_locator = (By.ID, "main-window") # editBookmarkPanelTemplate
_bookmark_locator = (By.ID, "main-window")
_file_menu_button_locator = (By.ID, "file-menu")
_file_menu_private_window_locator = (By.ID, "menu_newPrivateWindow")
_file_menu_new_window_button_locator = (By.ID, "menu_newNavigator")
Expand All @@ -33,10 +33,6 @@ class BrowserWindow(BaseWindow):
By.CSS_SELECTOR,
"#appMenu-notification-popup popupnotification",
)
_app_menu_panel_ui_locator = (
By.CSS_SELECTOR,
"#appMenu-mainView .panel-subview-body toolbarbutton",
)
_tab_browser_locator = (By.ID, "tabbrowser-tabs")

@property
Expand Down Expand Up @@ -97,20 +93,6 @@ def panel_ui(self) -> PanelUI | Any:
panel_root = PanelUI.create(self, root)
except NoSuchElementException:
pass

try:
panel_items = self.selenium.find_elements(
*self._app_menu_panel_ui_locator
)
for item in panel_items:
_id = item.get_property("id")
from foxpuppet.windows.browser.panel_ui.panel_ui import PANEL_ITEMS

if _id in PANEL_ITEMS and item.is_displayed():
panel_root = PANEL_ITEMS[_id].create(self, item) # type: ignore
except StopIteration:
pass

return panel_root

def wait_for_notification(
Expand Down
87 changes: 70 additions & 17 deletions tests/test_panel_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,95 @@
import pytest
from selenium.webdriver.remote.webdriver import WebDriver
from foxpuppet.windows import BrowserWindow
from foxpuppet.windows.browser.panel_ui.panel_ui import PanelUI, History
from foxpuppet.windows.browser.panel_ui.panel_ui import PanelUI


links = [
"https://www.mozilla.org/en-US/?v=a",
"https://www.youtube.com",
"https://www.facebook.com/",
]


@pytest.fixture
def history(browser: BrowserWindow) -> History | None:
"""Create History panel object.
def panel_ui(browser: BrowserWindow) -> PanelUI | None:
"""Create Panel UI object.
Args:
browser: BrowserWindow instance
Returns:
:py:class:`History`: FoxPuppet History panel object
:py:class:`PanelUI`: FoxPuppet Panel UI object
"""
panel_ui = browser.wait_for_panel_ui(PanelUI)
if panel_ui is not None:
panel_ui.open_panel_menu()
return browser.wait_for_panel_ui(History)
return browser.wait_for_panel_ui(PanelUI)


def test_open_new_tab(panel_ui: PanelUI) -> None:
"""Test opening a new tab using the Panel UI."""
panel_ui.open_new_tab()
assert panel_ui.wait_for_num_windows_or_tabs(2)


def test_open_new_window(panel_ui: PanelUI) -> None:
"""Test opening a new window using the Panel UI."""
panel_ui.open_new_window()
assert panel_ui.wait_for_num_windows_or_tabs(2)


def test_verify_links_open_in_new_tab_in_awesome_bar(panel_ui: PanelUI) -> None:
"""Test that links opened in new tab are present in the awesome bar."""
link = ["https://www.mozilla.org/en-US/?v=a"]
urls = panel_ui.verify_links_in_awesome_bar(link)
assert len(urls) == 1


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

def test_url_is_present_in_history(history: History, selenium: WebDriver) -> None:

def test_verify_links_open_in_new_window_in_history(panel_ui: PanelUI) -> None:
"""Test that links opened in new window are present in browser history."""
urls = panel_ui.verify_links_in_history(links, new_window=True)
assert len(urls) == 3


def test_verify_links_open_in_new_window_in_awesome_bar(panel_ui: PanelUI) -> None:
"""Test that links opened in new window are present in the awesome bar."""
link = ["https://www.mozilla.org/en-US/?v=a"]
urls = panel_ui.verify_links_in_awesome_bar(link, new_window=True)
assert len(urls) == 1


def test_url_is_present_in_history(panel_ui: PanelUI, selenium: WebDriver) -> None:
"""Test that visited URL appears in browser history."""
url = "https://www.mozilla.org/en-US/?v=a"
selenium.get(url)
history.open_history_menu()
assert history.is_present(url)
panel_ui.open_history_menu()
assert panel_ui.is_present(url)


def test_clear_recent_history(history: History, selenium: WebDriver) -> None:
def test_clear_recent_history(panel_ui: PanelUI, selenium: WebDriver) -> None:
"""Test clearing browser history removes visited URLs."""
url = "https://www.mozilla.org/en-US/?v=a"
selenium.get(url)
history.open_history_menu()
history.clear_history()
panel_ui.open_history_menu()
panel_ui.clear_history()
import time

time.sleep(1)
history.open_panel_menu()
history.open_history_menu()
assert not history.is_present(url)
panel_ui.open_history_menu()
assert not panel_ui.is_present(url)


def test_verify_private_browsing_links_not_in_history(panel_ui: PanelUI) -> None:
"""Test that links opened in private window are not present in browser history."""
invalid_links = panel_ui.verify_private_browsing_links_not_in_history(links)
assert not invalid_links


def test_verify_private_browsing_links_not_in_awesome_bar(panel_ui: PanelUI) -> None:
"""Test that links opened in private window are not present in the awesome bar."""
invalid_links = panel_ui.verify_private_browsing_links_not_in_awesome_bar(links)
assert not invalid_links

0 comments on commit 8d8f563

Please sign in to comment.