Skip to content

Commit 1122251

Browse files
committed
Add open website from history test
1 parent 6bfca23 commit 1122251

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

modules/browser_object_panel_ui.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import random
12
from time import sleep
2-
from typing import List
3+
from typing import List, Tuple
34

5+
import pytest
46
from pypom import Region
57
from selenium.common.exceptions import NoSuchElementException, TimeoutException
68
from selenium.webdriver.remote.webelement import WebElement
@@ -191,6 +193,48 @@ def get_all_history(self) -> List[WebElement]:
191193
history_items = self.get_elements("bookmark-item")
192194
return history_items
193195

196+
@BasePage.context_chrome
197+
def get_random_history_entry(self) -> Tuple[str, str]:
198+
"""
199+
Retrieve a random browser history entry from the Panel UI.
200+
201+
This method ensures the History submenu is open, fetches all available
202+
history items, selects one at random, extracts its URL and title, and
203+
returns them after validating both are usable.
204+
"""
205+
items = self.get_elements("bookmark-item")
206+
207+
if not items:
208+
pytest.skip("No history available to test.")
209+
210+
item = random.choice(items)
211+
raw_url = item.get_attribute("image")
212+
label = item.get_attribute("label")
213+
214+
trimmed_url = self._extract_url_from_history(raw_url)
215+
assert trimmed_url and label, "History item has missing URL or label."
216+
217+
return trimmed_url, label
218+
219+
def _extract_url_from_history(self, raw_url: str) -> str:
220+
"""
221+
Extract a valid HTTP(S) URL from a raw history image attribute.
222+
223+
Firefox stores history URLs using special schemes like:
224+
'moz-anno:favicon:https://example.com'
225+
This method locates the first occurrence of "http" and returns the substring from there.
226+
227+
Args:
228+
raw_url (str): The raw string value from the 'image' attribute of a history entry.
229+
"""
230+
if not raw_url:
231+
return ""
232+
233+
if "http" in raw_url:
234+
return raw_url[raw_url.find("http") :]
235+
236+
return raw_url.strip()
237+
194238
@BasePage.context_chrome
195239
def redirect_to_about_logins_page(self) -> BasePage:
196240
"""
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import random
2-
31
import pytest
42
from selenium.webdriver import Firefox
53

@@ -17,35 +15,20 @@ def use_profile():
1715
return "theme_change"
1816

1917

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-
2818
def test_open_websites_from_history(driver: Firefox):
2919
"""
30-
C118807: Verify that the user can open websites from the Toolbar History submenu
20+
C118807: Verify that the user can open any random website from Hamburger Menu, History section
3121
"""
32-
panel_ui = PanelUi(driver)
33-
panel_ui.open()
3422

35-
panel_ui.open_history_menu()
23+
# Instantiate objects
24+
panel = PanelUi(driver)
25+
panel.open_history_menu()
3626

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."
27+
# Retrieve a random item from history and its label
28+
url, label = panel.get_random_history_entry()
4129

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")
45-
46-
trimmed_url = trim_url(url_to_visit)
47-
page = GenericPage(driver, url=trimmed_url)
30+
# Open the corresponding page and verify URL and title match
31+
page = GenericPage(driver, url=url)
4832
page.open()
49-
50-
page.url_contains(trimmed_url)
51-
page.title_contains(website_label)
33+
page.url_contains(url)
34+
page.title_contains(label)

0 commit comments

Comments
 (0)