diff --git a/modules/browser_object_panel_ui.py b/modules/browser_object_panel_ui.py index 4b9d30c8b..711509fb9 100644 --- a/modules/browser_object_panel_ui.py +++ b/modules/browser_object_panel_ui.py @@ -1,5 +1,6 @@ +import random from time import sleep -from typing import List +from typing import List, Optional, Tuple from pypom import Region from selenium.common.exceptions import NoSuchElementException, TimeoutException @@ -154,6 +155,31 @@ def open_private_window(self) -> BasePage: self.get_element("panel-ui-new-private-window").click() return self + @BasePage.context_chrome + def redirect_to_about_logins_page(self) -> BasePage: + """ + Opens the about:logins page by clicking the Password option in Hamburger Menu" + """ + self.open_panel_menu() + self.get_element("password-button").click() + return self + + @BasePage.context_chrome + def reopen_recently_closed_tabs(self) -> BasePage: + """Reopen all recently closed tabs""" + self.open_panel_menu() + self.click_on("panel-ui-history") + + self.click_on("panel-ui-history-recently-closed") + if self.sys_platform() == "Linux": + sleep(2) + + self.click_on("panel-ui-history-recently-closed-reopen-tabs") + + return self + + # History + @BasePage.context_chrome def open_history_menu(self) -> BasePage: """ @@ -163,19 +189,28 @@ def open_history_menu(self) -> BasePage: self.click_on("panel-ui-history") return self - def select_clear_history_option(self, option: str) -> BasePage: + @BasePage.context_chrome + def open_clear_history_dialog(self) -> BasePage: """ - Selects the clear history option, assumes the history panel is open. + Opens the clear history dialog and switches to iframe context, assuming the history panel is opened """ - with self.driver.context(self.driver.CONTEXT_CHROME): - self.get_element("clear-recent-history").click() - iframe = self.get_element("iframe") - BrowserActions(self.driver).switch_to_iframe_context(iframe) + self.click_on("clear-recent-history") - with self.driver.context(self.driver.CONTEXT_CONTENT): - dropdown_root = self.get_element("clear-history-dropdown") - dropdown = Dropdown(page=self, root=dropdown_root, require_shadow=False) - dropdown.select_option(option) + # Switch to iframe + self.element_visible("iframe") + iframe = self.get_element("iframe") + BrowserActions(self.driver).switch_to_iframe_context(iframe) + return self + + @BasePage.context_content + def select_history_time_range_option(self, option: str) -> BasePage: + """ + Selects time range option (assumes already in iframe context) + """ + dropdown_root = self.get_element("clear-history-dropdown") + dropdown = Dropdown(page=self, root=dropdown_root, require_shadow=False) + dropdown.select_option(option) + return self def get_all_history(self) -> List[WebElement]: """ @@ -186,14 +221,62 @@ def get_all_history(self) -> List[WebElement]: return history_items @BasePage.context_chrome - def redirect_to_about_logins_page(self) -> BasePage: + def verify_most_recent_history_item(self, expected_value: str) -> BasePage: """ - Opens the about:logins page by clicking the Password option in Hamburger Menu" + Verify that the specified value is the most recent item in the history menu. + Argument: + Expected_value (str): The expected value of the most recent history entry """ - self.open_panel_menu() - self.get_element("password-button").click() + recent_history_items = self.get_elements("recent-history-content") + actual_value = recent_history_items[0].get_attribute("value") + assert actual_value == expected_value return self + @BasePage.context_chrome + def get_random_history_entry(self) -> Optional[Tuple[str, str]]: + """ + Retrieve a random browser history entry from the Panel UI. + + This method ensures the History submenu is open, fetches all available + history items, selects one at random, extracts its URL and title, and + returns them after validating both are usable. + """ + items = self.get_elements("bookmark-item") + + if not items: + return None + + item = random.choice(items) + raw_url = item.get_attribute("image") + label = item.get_attribute("label") + + trimmed_url = self._extract_url_from_history(raw_url) + assert trimmed_url and label, "History item has missing URL or label." + return trimmed_url, label + + def _extract_url_from_history(self, raw_url: str) -> str: + """ + Extract a valid HTTP(S) URL from a raw history image attribute. This method locates the first occurrence of + "http" and returns the substring from there. + Argument: + raw_url (str): The raw string value from the 'image' attribute of a history entry. + """ + if not raw_url: + return "" + if "http" in raw_url: + return raw_url[raw_url.find("http") :] + return raw_url.strip() + + @BasePage.context_chrome + def confirm_history_clear(self): + """ + Confirm that the history is empty + """ + self.open_history_menu() + self.expect_element_attribute_contains( + "recent-history-content", "value", "(Empty)" + ) + # Bookmarks section @BasePage.context_chrome @@ -268,39 +351,3 @@ def get_bookmark_tags(self, tags: List[str]) -> List[str]: ) for tag in tags ] - - @BasePage.context_chrome - def clear_recent_history(self, execute=True) -> BasePage: - """Clears recent history. Case of execute=True may not be complete""" - self.open_panel_menu() - self.get_element("panel-ui-history").click() - - self.element_exists("clear-recent-history") - self.element_visible("clear-recent-history") - self.element_clickable("clear-recent-history") - if execute: - self.click("clear_recent_history") - - return self - - @BasePage.context_chrome - def confirm_history_clear(self): - """Confirm that the history is empty""" - self.open_history_menu() - self.expect_element_attribute_contains( - "recent-history-content", "value", "(Empty)" - ) - - @BasePage.context_chrome - def reopen_recently_closed_tabs(self) -> BasePage: - """Reopen all recently closed tabs""" - self.open_panel_menu() - self.click_on("panel-ui-history") - - self.click_on("panel-ui-history-recently-closed") - if self.sys_platform() == "Linux": - sleep(2) - - self.click_on("panel-ui-history-recently-closed-reopen-tabs") - - return self diff --git a/tests/bookmarks_and_history/test_clear_all_history.py b/tests/bookmarks_and_history/test_clear_all_history.py index 6f1b1df4f..464b57a99 100644 --- a/tests/bookmarks_and_history/test_clear_all_history.py +++ b/tests/bookmarks_and_history/test_clear_all_history.py @@ -19,12 +19,19 @@ def test_clear_all_history(driver: Firefox): """ C172045: Verify that the user can Clear all the History """ - gen_page = GenericPage(driver) - panel_ui = PanelUi(driver) - panel_ui.open() - panel_ui.open_history_menu() - - panel_ui.select_clear_history_option("Everything") - - gen_page.click_on("clear-history-button") - panel_ui.confirm_history_clear() + # Instantiate objects + page = GenericPage(driver) + panel = PanelUi(driver) + + # Open Clear History dialog + panel.open_history_menu() + panel.open_clear_history_dialog() + + # Select the option to clear all the history + panel.select_history_time_range_option("Everything") + # A method in panel BOM with selectors moved accordingly would make more sense, I'll come to this later, + # there are some context switching + iframe entanglements, couldn't make it work so far + page.click_on("clear-history-button") + + # Verify all the history is deleted + panel.confirm_history_clear() diff --git a/tests/bookmarks_and_history/test_clear_recent_history_displayed.py b/tests/bookmarks_and_history/test_clear_recent_history_displayed.py index 894ca4c04..cd370613b 100644 --- a/tests/bookmarks_and_history/test_clear_recent_history_displayed.py +++ b/tests/bookmarks_and_history/test_clear_recent_history_displayed.py @@ -13,7 +13,9 @@ def test_clear_recent_history_displayed(driver: Firefox): """ C172043: Clear recent history panel displayed """ - panel_ui = PanelUi(driver) - panel_ui.open() + # Instantiate object + panel = PanelUi(driver) - panel_ui.clear_recent_history(execute=False) + # Open Clear recent history dialog + panel.open_history_menu() + panel.open_clear_history_dialog() diff --git a/tests/bookmarks_and_history/test_open_websites_from_history.py b/tests/bookmarks_and_history/test_open_websites_from_history.py index 89693b6eb..d118a2ea1 100644 --- a/tests/bookmarks_and_history/test_open_websites_from_history.py +++ b/tests/bookmarks_and_history/test_open_websites_from_history.py @@ -1,4 +1,4 @@ -import random +import logging import pytest from selenium.webdriver import Firefox @@ -17,35 +17,27 @@ def use_profile(): return "theme_change" -def trim_url(url: str) -> str: - colon_index = url.find(":") - if colon_index != -1: - return url[colon_index + 1 :].strip() - else: - return "" - - def test_open_websites_from_history(driver: Firefox): """ - C118807: Verify that the user can open websites from the Toolbar History submenu + C118807: Verify that the user can open any random website from Hamburger Menu, History section """ - panel_ui = PanelUi(driver) - panel_ui.open() + # Instantiate object + panel = PanelUi(driver) - panel_ui.open_history_menu() + # Open History section from Hamburger Menu and get a random entry from browser history + panel.open_history_menu() + result = panel.get_random_history_entry() - with driver.context(driver.CONTEXT_CHROME): - history_items = panel_ui.get_all_history() - if len(history_items) == 0: - assert False, "There is no history." + # Skip test if no history entries are available + if result is None: + logging.info("Test skipped: No history available") + return - rand_index = random.randint(0, len(history_items) - 1) - url_to_visit = history_items[rand_index].get_attribute("image") - website_label = history_items[rand_index].get_attribute("label") + # Extract URL and page title from the selected history entry + url, label = result - trimmed_url = trim_url(url_to_visit) - page = GenericPage(driver, url=trimmed_url) + # Navigate to the selected page and verify it loads correctly + page = GenericPage(driver, url=url) page.open() - - page.url_contains(trimmed_url) - page.title_contains(website_label) + page.url_contains(url) + page.title_contains(label) diff --git a/tests/bookmarks_and_history/test_opened_website_in_new_tab_present_in_hamburger_history_menu.py b/tests/bookmarks_and_history/test_opened_website_in_new_tab_present_in_hamburger_history_menu.py new file mode 100644 index 000000000..abe3fe8ba --- /dev/null +++ b/tests/bookmarks_and_history/test_opened_website_in_new_tab_present_in_hamburger_history_menu.py @@ -0,0 +1,38 @@ +import pytest +from selenium.webdriver import Firefox + +from modules.browser_object import PanelUi, TabBar +from modules.page_object import GenericPage + + +@pytest.fixture() +def test_case(): + return "118802" + + +@pytest.fixture() +def use_profile(): + return "theme_change" + + +WIKIPEDIA_URL = "https://www.wikipedia.org/" + + +def test_the_website_opened_in_new_tab_is_present_in_history_menu(driver: Firefox): + """ + C118802 - Verify that the website opened in new tab is displayed in Hamburger Menu, History section, on top of the + list + """ + # Instantiate objects + tabs = TabBar(driver) + page = GenericPage(driver, url=WIKIPEDIA_URL) + panel = PanelUi(driver) + + # Open a desired webpage in a new tab + tabs.open_web_page_in_new_tab(page, 2) + page.url_contains("wikipedia") + + # Verify the opened webpage from last step is present in the Hamburger Menu, History section and is on top of the + # list as the most recent website visited + panel.open_history_menu() + panel.verify_most_recent_history_item("Wikipedia") diff --git a/tests/bookmarks_and_history/test_opened_website_in_new_tab_present_in_toolbar_history.py b/tests/bookmarks_and_history/test_opened_website_in_new_tab_present_in_toolbar_history.py deleted file mode 100644 index 986f80bb1..000000000 --- a/tests/bookmarks_and_history/test_opened_website_in_new_tab_present_in_toolbar_history.py +++ /dev/null @@ -1,46 +0,0 @@ -import pytest -from selenium.webdriver import Firefox - -from modules.browser_object import PanelUi, TabBar -from modules.page_object_generics import GenericPage - - -@pytest.fixture() -def test_case(): - return "118802" - - -FACEBOOK_URL = "https://www.facebook.com/" -AMAZON_URL = "https://www.amazon.com/" -YOUTUBE_URL = "https://www.youtube.com/" - -WEBSITES = [FACEBOOK_URL, AMAZON_URL] - - -def test_the_website_opened_in_new_tab_is_present_in_history_menu(driver: Firefox): - """ - C118802 - Verify that the website opened in new tab is displayed in the Toolbar History submenu on top of the - list - """ - - for url in WEBSITES: - GenericPage(driver, url=url).open() - - tabs = TabBar(driver) - - tabs.new_tab_by_button() - tabs.wait_for_num_tabs(2) - tabs.switch_to_new_tab() - - page = GenericPage(driver, url=YOUTUBE_URL) - page.open() - page.url_contains("youtube") - - panel_ui = PanelUi(driver) - panel_ui.open() - panel_ui.open_history_menu() - - # Verify YouTube is present in the history menu and is on top of the list as the most recent website visited - panel_ui.expect_element_attribute_contains( - "recent-history-content", "value", "YouTube" - ) diff --git a/tests/bookmarks_and_history/test_opened_website_in_new_window_present_in_hamburger_history_menu.py b/tests/bookmarks_and_history/test_opened_website_in_new_window_present_in_hamburger_history_menu.py new file mode 100644 index 000000000..c1139f16c --- /dev/null +++ b/tests/bookmarks_and_history/test_opened_website_in_new_window_present_in_hamburger_history_menu.py @@ -0,0 +1,38 @@ +import pytest +from selenium.webdriver import Firefox + +from modules.browser_object_panel_ui import PanelUi +from modules.page_object import GenericPage + + +@pytest.fixture() +def test_case(): + return "118805" + + +@pytest.fixture() +def use_profile(): + return "theme_change" + + +WIKIPEDIA_URL = "https://www.wikipedia.org/" + + +def test_the_website_opened_in_new_window_is_present_in_history_menu(driver: Firefox): + """ + C118805 - Verify that the website opened in new window is displayed in Hamburger Menu, History section, on top of the + list + """ + # Instantiate objects + page = GenericPage(driver, url=WIKIPEDIA_URL) + panel = PanelUi(driver) + + # Open a desired webpage in a new window + panel.open_and_switch_to_new_window("window") + page.open() + page.url_contains("wikipedia") + + # Verify the opened webpage from last step is present in the Hamburger Menu, History section and is on top of the + # list as the most recent website visited + panel.open_history_menu() + panel.verify_most_recent_history_item("Wikipedia") diff --git a/tests/bookmarks_and_history/test_opened_website_in_new_window_present_in_toolbar_history.py b/tests/bookmarks_and_history/test_opened_website_in_new_window_present_in_toolbar_history.py deleted file mode 100644 index 76a3f2370..000000000 --- a/tests/bookmarks_and_history/test_opened_website_in_new_window_present_in_toolbar_history.py +++ /dev/null @@ -1,41 +0,0 @@ -import pytest -from selenium.webdriver import Firefox - -from modules.browser_object_panel_ui import PanelUi -from modules.page_object_generics import GenericPage - - -@pytest.fixture() -def test_case(): - return "118805" - - -FACEBOOK_URL = "https://www.facebook.com/" -AMAZON_URL = "https://www.amazon.com/" -YOUTUBE_URL = "https://www.youtube.com/" - -WEBSITES = [FACEBOOK_URL, AMAZON_URL] - - -def test_the_website_opened_in_new_window_is_present_in_history_menu(driver: Firefox): - """ - C118805 - Verify that the website opened in new window is displayed in the Toolbar History submenu on top of the - list - """ - - for url in WEBSITES: - GenericPage(driver, url=url).open() - - panel_ui = PanelUi(driver) - panel_ui.open_and_switch_to_new_window("window") - - page = GenericPage(driver, url=YOUTUBE_URL) - page.open() - page.url_contains("youtube") - - panel_ui.open_history_menu() - - # Verify YouTube is present in the history menu and is on top of the list as the most recent website visited - panel_ui.expect_element_attribute_contains( - "recent-history-content", "value", "YouTube" - ) diff --git a/tests/bookmarks_and_history/test_opened_website_present_in_hamburger_history_menu.py b/tests/bookmarks_and_history/test_opened_website_present_in_hamburger_history_menu.py new file mode 100644 index 000000000..bd75c0d41 --- /dev/null +++ b/tests/bookmarks_and_history/test_opened_website_present_in_hamburger_history_menu.py @@ -0,0 +1,36 @@ +import pytest +from selenium.webdriver import Firefox + +from modules.browser_object_panel_ui import PanelUi +from modules.page_object import GenericPage + + +@pytest.fixture() +def test_case(): + return "118800" + + +@pytest.fixture() +def use_profile(): + return "theme_change" + + +WIKIPEDIA_URL = "https://www.wikipedia.org/" + + +def test_the_most_recent_website_is_present_in_history_menu(driver: Firefox): + """ + C118800 - Verify that the most recently opened website is displayed in Hamburger Menu, History section, on top of the + list + """ + # Instantiate objects + page = GenericPage(driver, url=WIKIPEDIA_URL) + panel = PanelUi(driver) + + # Open the desired webpage + page.open() + + # Verify the opened webpage from last step is present in the Hamburger Menu, History section and is on top of the + # list as the most recent website visited + panel.open_history_menu() + panel.verify_most_recent_history_item("Wikipedia") diff --git a/tests/bookmarks_and_history/test_opened_website_present_in_toolbar_history.py b/tests/bookmarks_and_history/test_opened_website_present_in_toolbar_history.py deleted file mode 100644 index feaa2eff4..000000000 --- a/tests/bookmarks_and_history/test_opened_website_present_in_toolbar_history.py +++ /dev/null @@ -1,38 +0,0 @@ -import pytest -from selenium.webdriver import Firefox - -from modules.browser_object_panel_ui import PanelUi -from modules.page_object_generics import GenericPage - - -@pytest.fixture() -def test_case(): - return "118800" - - -FACEBOOK_URL = "https://www.facebook.com/" -AMAZON_URL = "https://www.amazon.com/" -YOUTUBE_URL = "https://www.youtube.com/" - -WEBSITES = [FACEBOOK_URL, AMAZON_URL, YOUTUBE_URL] - - -def test_the_most_recent_website_is_present_in_history_menu(driver: Firefox): - """ - C118800 - Verify that the most recently opened website is displayed in the Toolbar History submenu on top of the - list - """ - - for url in WEBSITES: - GenericPage(driver, url=url).open() - - panel_ui = PanelUi(driver) - panel_ui.open() - panel_ui.open_history_menu() - - # Verify YouTube is present in the history menu and is on top of the list as the most recent website visited - with driver.context(driver.CONTEXT_CHROME): - recent_history_elements = panel_ui.get_elements("recent-history-content") - assert recent_history_elements[0].get_attribute("value") == "YouTube", ( - "YouTube is not the first item in the recent history." - ) diff --git a/tests/bookmarks_and_history/test_private_window_website_not_in_history.py b/tests/bookmarks_and_history/test_private_window_website_not_in_history.py index 88f5bc1a3..ddf8d4b03 100644 --- a/tests/bookmarks_and_history/test_private_window_website_not_in_history.py +++ b/tests/bookmarks_and_history/test_private_window_website_not_in_history.py @@ -15,20 +15,16 @@ def test_case(): def test_opened_website_in_private_window_not_captured_in_history_list(driver: Firefox): """ - C118806 - Verify that opened websites in a New Private Window will not be displayed in the Hamburger submenu history + C118806 - Verify that opened websites in a New Private Window will not be displayed in the Hamburger History submenu """ + # Instantiate objects + panel = PanelUi(driver) + page = GenericPage(driver, url=YOUTUBE_URL) - panel_ui = PanelUi(driver) - panel_ui.open_and_switch_to_new_window("private") + # Open the desired webpage in a new Private window + panel.open_and_switch_to_new_window("private") + page.open() - GenericPage(driver, url=YOUTUBE_URL).open() - - panel_ui.open_history_menu() - - with driver.context(driver.CONTEXT_CHROME): - empty_label = panel_ui.get_element("recent-history-content").get_attribute( - "value" - ) - assert empty_label == "(Empty)", ( - f"Expected history to be empty, but found '{empty_label}'" - ) + # Verify that the webpage opened in Private window is not listed in History + panel.open_history_menu() + panel.confirm_history_clear()