Skip to content

Commit d77c10d

Browse files
committed
helper method to extract domain
1 parent cab136b commit d77c10d

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

modules/util.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from os import remove
88
from random import shuffle
99
from typing import List, Literal, Union
10+
from urllib.parse import urlparse, urlunparse
1011

1112
from faker import Faker
1213
from faker.providers import internet, misc
@@ -370,21 +371,15 @@ def assert_json_value(self, json_data, jsonpath_expr, expected_value):
370371
f"Expected {expected_value}, but got {match[0].value}",
371372
)
372373

373-
def trim_url_to_last_slash(self, url: str):
374+
def get_domain_from_url(self, url: str) -> str:
374375
"""
375-
Given a URL, it will create a copy and trim it to the first slash. It does not include the last / after trimming.
376-
"""
377-
# Find the position of the last slash
378-
last_slash_index = url.rfind("/")
379-
380-
# Slice the string up to and including the last slash
381-
if last_slash_index != -1:
382-
trimmed_url = url[:last_slash_index]
383-
else:
384-
logging.warn("Couldn't find the last instance of a /.")
385-
trimmed_url = url
376+
Given a URL, it will extract the domain of the URL.
386377
387-
return trimmed_url
378+
For example, "https://www.example.com/path/to/page?query=123#fragment" will product "https://www.example.com"
379+
"""
380+
parsed_url = urlparse(url)
381+
domain_parsed_url = parsed_url._replace(path="")
382+
return urlunparse(domain_parsed_url)
388383

389384

390385
class BrowserActions:

tests/security_and_privacy/test_notifications_change_set.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_notifications_allow(
5656

5757
website_item = about_prefs.get_element(
5858
"permissions-notifications-popup-websites-item",
59-
labels=[util.trim_url_to_last_slash(NOTIFICATIONS_SITE)],
59+
labels=[util.get_domain_from_url(NOTIFICATIONS_SITE)],
6060
)
6161

6262
notification_website_status = about_prefs.get_element(

0 commit comments

Comments
 (0)