Skip to content

Commit

Permalink
feat: add PageExplorer.current_url
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Feb 6, 2025
1 parent d6517d1 commit 8201cd7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/page_explorer/page_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ def close_browser(self, wait: int = 0, poll: float = 0.5) -> None:
break
sleep(poll)

@property
def current_url(self) -> str | None:
"""Gets the URL of the current path.
Args:
None
Returns:
The URL if it is available otherwise None.
"""
with suppress(HTTPError, WebDriverException):
return self._driver.current_url
return None

# pylint: disable=too-many-branches
def explore(
self,
Expand Down Expand Up @@ -281,10 +295,9 @@ def is_connected(self) -> bool:
Returns:
True if a page is open and connection is active otherwise False.
"""
try:
with suppress(HTTPError, WebDriverException):
return isinstance(self._driver.title, str)
except (HTTPError, WebDriverException):
LOG.debug("connection has closed")
LOG.debug("connection has closed")
return False

def shutdown(self) -> None:
Expand Down
18 changes: 18 additions & 0 deletions src/page_explorer/test_page_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ def test_page_explorer_is_connected(mocker, title_effect, result):
assert exp.is_connected() == result


@mark.parametrize(
"url_effect, result",
(
# connected
(("foo",), "foo"),
# not connected
((WebDriverException("test"),), None),
),
)
def test_page_explorer_current_url(mocker, url_effect, result):
"""test PageExplorer.current_url"""
driver = mocker.patch("page_explorer.page_explorer.FirefoxDriver", autospec=True)
type(driver.return_value).current_url = mocker.PropertyMock(side_effect=url_effect)

with PageExplorer("bin", 1234) as exp:
assert exp.current_url == result


@mark.parametrize(
"title_calls, title_effect, script_effect",
(
Expand Down

0 comments on commit 8201cd7

Please sign in to comment.