Skip to content

Commit b74e615

Browse files
authored
Merge pull request #795 from mozilla/anca/refactoring-bk-hist
Anca/ Refactoring History (I)
2 parents 4f51862 + 3111030 commit b74e615

11 files changed

+258
-227
lines changed

modules/browser_object_panel_ui.py

Lines changed: 98 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import random
12
from time import sleep
2-
from typing import List
3+
from typing import List, Optional, Tuple
34

45
from pypom import Region
56
from selenium.common.exceptions import NoSuchElementException, TimeoutException
@@ -154,6 +155,31 @@ def open_private_window(self) -> BasePage:
154155
self.get_element("panel-ui-new-private-window").click()
155156
return self
156157

158+
@BasePage.context_chrome
159+
def redirect_to_about_logins_page(self) -> BasePage:
160+
"""
161+
Opens the about:logins page by clicking the Password option in Hamburger Menu"
162+
"""
163+
self.open_panel_menu()
164+
self.get_element("password-button").click()
165+
return self
166+
167+
@BasePage.context_chrome
168+
def reopen_recently_closed_tabs(self) -> BasePage:
169+
"""Reopen all recently closed tabs"""
170+
self.open_panel_menu()
171+
self.click_on("panel-ui-history")
172+
173+
self.click_on("panel-ui-history-recently-closed")
174+
if self.sys_platform() == "Linux":
175+
sleep(2)
176+
177+
self.click_on("panel-ui-history-recently-closed-reopen-tabs")
178+
179+
return self
180+
181+
# History
182+
157183
@BasePage.context_chrome
158184
def open_history_menu(self) -> BasePage:
159185
"""
@@ -163,19 +189,28 @@ def open_history_menu(self) -> BasePage:
163189
self.click_on("panel-ui-history")
164190
return self
165191

166-
def select_clear_history_option(self, option: str) -> BasePage:
192+
@BasePage.context_chrome
193+
def open_clear_history_dialog(self) -> BasePage:
167194
"""
168-
Selects the clear history option, assumes the history panel is open.
195+
Opens the clear history dialog and switches to iframe context, assuming the history panel is opened
169196
"""
170-
with self.driver.context(self.driver.CONTEXT_CHROME):
171-
self.get_element("clear-recent-history").click()
172-
iframe = self.get_element("iframe")
173-
BrowserActions(self.driver).switch_to_iframe_context(iframe)
197+
self.click_on("clear-recent-history")
174198

175-
with self.driver.context(self.driver.CONTEXT_CONTENT):
176-
dropdown_root = self.get_element("clear-history-dropdown")
177-
dropdown = Dropdown(page=self, root=dropdown_root, require_shadow=False)
178-
dropdown.select_option(option)
199+
# Switch to iframe
200+
self.element_visible("iframe")
201+
iframe = self.get_element("iframe")
202+
BrowserActions(self.driver).switch_to_iframe_context(iframe)
203+
return self
204+
205+
@BasePage.context_content
206+
def select_history_time_range_option(self, option: str) -> BasePage:
207+
"""
208+
Selects time range option (assumes already in iframe context)
209+
"""
210+
dropdown_root = self.get_element("clear-history-dropdown")
211+
dropdown = Dropdown(page=self, root=dropdown_root, require_shadow=False)
212+
dropdown.select_option(option)
213+
return self
179214

180215
def get_all_history(self) -> List[WebElement]:
181216
"""
@@ -186,14 +221,62 @@ def get_all_history(self) -> List[WebElement]:
186221
return history_items
187222

188223
@BasePage.context_chrome
189-
def redirect_to_about_logins_page(self) -> BasePage:
224+
def verify_most_recent_history_item(self, expected_value: str) -> BasePage:
190225
"""
191-
Opens the about:logins page by clicking the Password option in Hamburger Menu"
226+
Verify that the specified value is the most recent item in the history menu.
227+
Argument:
228+
Expected_value (str): The expected value of the most recent history entry
192229
"""
193-
self.open_panel_menu()
194-
self.get_element("password-button").click()
230+
recent_history_items = self.get_elements("recent-history-content")
231+
actual_value = recent_history_items[0].get_attribute("value")
232+
assert actual_value == expected_value
195233
return self
196234

235+
@BasePage.context_chrome
236+
def get_random_history_entry(self) -> Optional[Tuple[str, str]]:
237+
"""
238+
Retrieve a random browser history entry from the Panel UI.
239+
240+
This method ensures the History submenu is open, fetches all available
241+
history items, selects one at random, extracts its URL and title, and
242+
returns them after validating both are usable.
243+
"""
244+
items = self.get_elements("bookmark-item")
245+
246+
if not items:
247+
return None
248+
249+
item = random.choice(items)
250+
raw_url = item.get_attribute("image")
251+
label = item.get_attribute("label")
252+
253+
trimmed_url = self._extract_url_from_history(raw_url)
254+
assert trimmed_url and label, "History item has missing URL or label."
255+
return trimmed_url, label
256+
257+
def _extract_url_from_history(self, raw_url: str) -> str:
258+
"""
259+
Extract a valid HTTP(S) URL from a raw history image attribute. This method locates the first occurrence of
260+
"http" and returns the substring from there.
261+
Argument:
262+
raw_url (str): The raw string value from the 'image' attribute of a history entry.
263+
"""
264+
if not raw_url:
265+
return ""
266+
if "http" in raw_url:
267+
return raw_url[raw_url.find("http") :]
268+
return raw_url.strip()
269+
270+
@BasePage.context_chrome
271+
def confirm_history_clear(self):
272+
"""
273+
Confirm that the history is empty
274+
"""
275+
self.open_history_menu()
276+
self.expect_element_attribute_contains(
277+
"recent-history-content", "value", "(Empty)"
278+
)
279+
197280
# Bookmarks section
198281

199282
@BasePage.context_chrome
@@ -268,39 +351,3 @@ def get_bookmark_tags(self, tags: List[str]) -> List[str]:
268351
)
269352
for tag in tags
270353
]
271-
272-
@BasePage.context_chrome
273-
def clear_recent_history(self, execute=True) -> BasePage:
274-
"""Clears recent history. Case of execute=True may not be complete"""
275-
self.open_panel_menu()
276-
self.get_element("panel-ui-history").click()
277-
278-
self.element_exists("clear-recent-history")
279-
self.element_visible("clear-recent-history")
280-
self.element_clickable("clear-recent-history")
281-
if execute:
282-
self.click("clear_recent_history")
283-
284-
return self
285-
286-
@BasePage.context_chrome
287-
def confirm_history_clear(self):
288-
"""Confirm that the history is empty"""
289-
self.open_history_menu()
290-
self.expect_element_attribute_contains(
291-
"recent-history-content", "value", "(Empty)"
292-
)
293-
294-
@BasePage.context_chrome
295-
def reopen_recently_closed_tabs(self) -> BasePage:
296-
"""Reopen all recently closed tabs"""
297-
self.open_panel_menu()
298-
self.click_on("panel-ui-history")
299-
300-
self.click_on("panel-ui-history-recently-closed")
301-
if self.sys_platform() == "Linux":
302-
sleep(2)
303-
304-
self.click_on("panel-ui-history-recently-closed-reopen-tabs")
305-
306-
return self

tests/bookmarks_and_history/test_clear_all_history.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ def test_clear_all_history(driver: Firefox):
1919
"""
2020
C172045: Verify that the user can Clear all the History
2121
"""
22-
gen_page = GenericPage(driver)
23-
panel_ui = PanelUi(driver)
24-
panel_ui.open()
25-
panel_ui.open_history_menu()
26-
27-
panel_ui.select_clear_history_option("Everything")
28-
29-
gen_page.click_on("clear-history-button")
30-
panel_ui.confirm_history_clear()
22+
# Instantiate objects
23+
page = GenericPage(driver)
24+
panel = PanelUi(driver)
25+
26+
# Open Clear History dialog
27+
panel.open_history_menu()
28+
panel.open_clear_history_dialog()
29+
30+
# Select the option to clear all the history
31+
panel.select_history_time_range_option("Everything")
32+
# A method in panel BOM with selectors moved accordingly would make more sense, I'll come to this later,
33+
# there are some context switching + iframe entanglements, couldn't make it work so far
34+
page.click_on("clear-history-button")
35+
36+
# Verify all the history is deleted
37+
panel.confirm_history_clear()

tests/bookmarks_and_history/test_clear_recent_history_displayed.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def test_clear_recent_history_displayed(driver: Firefox):
1313
"""
1414
C172043: Clear recent history panel displayed
1515
"""
16-
panel_ui = PanelUi(driver)
17-
panel_ui.open()
16+
# Instantiate object
17+
panel = PanelUi(driver)
1818

19-
panel_ui.clear_recent_history(execute=False)
19+
# Open Clear recent history dialog
20+
panel.open_history_menu()
21+
panel.open_clear_history_dialog()
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import random
1+
import logging
22

33
import pytest
44
from selenium.webdriver import Firefox
@@ -17,35 +17,27 @@ def use_profile():
1717
return "theme_change"
1818

1919

20-
def trim_url(url: str) -> str:
21-
colon_index = url.find(":")
22-
if colon_index != -1:
23-
return url[colon_index + 1 :].strip()
24-
else:
25-
return ""
26-
27-
2820
def test_open_websites_from_history(driver: Firefox):
2921
"""
30-
C118807: Verify that the user can open websites from the Toolbar History submenu
22+
C118807: Verify that the user can open any random website from Hamburger Menu, History section
3123
"""
32-
panel_ui = PanelUi(driver)
33-
panel_ui.open()
24+
# Instantiate object
25+
panel = PanelUi(driver)
3426

35-
panel_ui.open_history_menu()
27+
# Open History section from Hamburger Menu and get a random entry from browser history
28+
panel.open_history_menu()
29+
result = panel.get_random_history_entry()
3630

37-
with driver.context(driver.CONTEXT_CHROME):
38-
history_items = panel_ui.get_all_history()
39-
if len(history_items) == 0:
40-
assert False, "There is no history."
31+
# Skip test if no history entries are available
32+
if result is None:
33+
logging.info("Test skipped: No history available")
34+
return
4135

42-
rand_index = random.randint(0, len(history_items) - 1)
43-
url_to_visit = history_items[rand_index].get_attribute("image")
44-
website_label = history_items[rand_index].get_attribute("label")
36+
# Extract URL and page title from the selected history entry
37+
url, label = result
4538

46-
trimmed_url = trim_url(url_to_visit)
47-
page = GenericPage(driver, url=trimmed_url)
39+
# Navigate to the selected page and verify it loads correctly
40+
page = GenericPage(driver, url=url)
4841
page.open()
49-
50-
page.url_contains(trimmed_url)
51-
page.title_contains(website_label)
42+
page.url_contains(url)
43+
page.title_contains(label)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
from selenium.webdriver import Firefox
3+
4+
from modules.browser_object import PanelUi, TabBar
5+
from modules.page_object import GenericPage
6+
7+
8+
@pytest.fixture()
9+
def test_case():
10+
return "118802"
11+
12+
13+
@pytest.fixture()
14+
def use_profile():
15+
return "theme_change"
16+
17+
18+
WIKIPEDIA_URL = "https://www.wikipedia.org/"
19+
20+
21+
def test_the_website_opened_in_new_tab_is_present_in_history_menu(driver: Firefox):
22+
"""
23+
C118802 - Verify that the website opened in new tab is displayed in Hamburger Menu, History section, on top of the
24+
list
25+
"""
26+
# Instantiate objects
27+
tabs = TabBar(driver)
28+
page = GenericPage(driver, url=WIKIPEDIA_URL)
29+
panel = PanelUi(driver)
30+
31+
# Open a desired webpage in a new tab
32+
tabs.open_web_page_in_new_tab(page, 2)
33+
page.url_contains("wikipedia")
34+
35+
# Verify the opened webpage from last step is present in the Hamburger Menu, History section and is on top of the
36+
# list as the most recent website visited
37+
panel.open_history_menu()
38+
panel.verify_most_recent_history_item("Wikipedia")

tests/bookmarks_and_history/test_opened_website_in_new_tab_present_in_toolbar_history.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)