Skip to content

Commit 70a083a

Browse files
committed
Refactor link_text methods
1 parent 004010d commit 70a083a

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ self.find_link_text(link_text, timeout=None)
781781
# self.wait_for_link_text_visible(link_text, timeout=None)
782782

783783
self.assert_link_text(link_text, timeout=None)
784+
# Duplicates:
785+
# self.assert_link(link_text, timeout=None)
784786

785787
############
786788

seleniumbase/common/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
""" SeleniumBase Exceptions
2+
LinkTextNotFoundException => Called when expected link text is not visible.
23
NoSuchFileException => Called when self.assert_downloaded_file(...) fails.
34
NoSuchOptionException => Called when select_option_by_*() lacks the option.
45
NotConnectedException => Called when Internet is not reachable when needed.
56
NotUsingChromeException => Used by Chrome-only methods if not using Chrome.
67
NotUsingChromiumException => Used by Chromium-only methods if not Chromium.
78
OutOfScopeException => Used by BaseCase methods when setUp() is skipped.
89
ProxyConnectionException => Called when the proxy connection failed.
9-
TextNotVisibleException => Called when expected text fails to appear.
10+
TextNotVisibleException => Called when the expected text is not visible.
1011
TimeLimitExceededException => Called when exceeding "--time-limit=SECONDS".
1112
TimeoutException => Called when some timeout limit has been exceeded.
1213
VisualException => Called when there's a Visual Diff Assertion Failure.
1314
"""
1415

1516

17+
class LinkTextNotFoundException(Exception):
18+
pass
19+
20+
1621
class NoSuchFileException(Exception):
1722
pass
1823

seleniumbase/fixtures/base_case.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,9 +1450,8 @@ def get_partial_link_text_attribute(
14501450
else:
14511451
return None
14521452
if hard_fail:
1453-
raise Exception(
1454-
"Partial Link text {%s} was not found!" % link_text
1455-
)
1453+
msg = "Partial Link text {%s} was not found!" % link_text
1454+
page_actions.timeout_exception("LinkTextNotFoundException", msg)
14561455
else:
14571456
return None
14581457

@@ -8200,6 +8199,15 @@ def wait_for_element_not_present(
82008199
original_selector=original_selector,
82018200
)
82028201

8202+
def assert_link(self, link_text, timeout=None):
8203+
"""Same as self.assert_link_text()"""
8204+
self.__check_scope()
8205+
if not timeout:
8206+
timeout = settings.SMALL_TIMEOUT
8207+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
8208+
timeout = self.__get_new_timeout(timeout)
8209+
self.assert_link_text(link_text, timeout=timeout)
8210+
82038211
def assert_element_not_present(
82048212
self, selector, by="css selector", timeout=None
82058213
):
@@ -9009,11 +9017,11 @@ def wait_for_link_text_present(self, link_text, timeout=None):
90099017
if now_ms >= stop_ms:
90109018
break
90119019
time.sleep(0.2)
9012-
message = "Link text {%s} was not present after %s seconds!" % (
9020+
message = "Link text {%s} was not found after %s seconds!" % (
90139021
link_text,
90149022
timeout,
90159023
)
9016-
page_actions.timeout_exception("NoSuchElementException", message)
9024+
page_actions.timeout_exception("LinkTextNotFoundException", message)
90179025

90189026
def wait_for_partial_link_text_present(self, link_text, timeout=None):
90199027
self.__check_scope()
@@ -9035,10 +9043,10 @@ def wait_for_partial_link_text_present(self, link_text, timeout=None):
90359043
break
90369044
time.sleep(0.2)
90379045
message = (
9038-
"Partial Link text {%s} was not present after %s seconds!"
9046+
"Partial Link text {%s} was not found after %s seconds!"
90399047
"" % (link_text, timeout)
90409048
)
9041-
page_actions.timeout_exception("NoSuchElementException", message)
9049+
page_actions.timeout_exception("LinkTextNotFoundException", message)
90429050

90439051
def wait_for_link_text_visible(self, link_text, timeout=None):
90449052
self.__check_scope()
@@ -13223,7 +13231,7 @@ def __wait_for_shadow_text_visible(self, text, selector, timeout):
1322313231
text,
1322413232
selector,
1322513233
)
13226-
page_actions.timeout_exception("ElementNotVisibleException", msg)
13234+
page_actions.timeout_exception("TextNotVisibleException", msg)
1322713235
return True
1322813236

1322913237
def __wait_for_exact_shadow_text_visible(self, text, selector, timeout):
@@ -13242,7 +13250,7 @@ def __wait_for_exact_shadow_text_visible(self, text, selector, timeout):
1324213250
"" % (text, selector)
1324313251
)
1324413252
page_actions.timeout_exception(
13245-
"ElementNotVisibleException", msg
13253+
"TextNotVisibleException", msg
1324613254
)
1324713255
return True
1324813256
except Exception:

seleniumbase/fixtures/page_actions.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from selenium.common.exceptions import NoSuchWindowException
2929
from selenium.common.exceptions import StaleElementReferenceException
3030
from selenium.webdriver.common.action_chains import ActionChains
31+
from seleniumbase.common.exceptions import LinkTextNotFoundException
3132
from seleniumbase.common.exceptions import TextNotVisibleException
3233
from seleniumbase.config import settings
3334
from seleniumbase.fixtures import shared_utils
@@ -468,12 +469,12 @@ def wait_for_element_visible(
468469
)
469470
timeout_exception(ElementNotVisibleException, message)
470471
elif not element and by == "link text":
471-
message = "Link text {%s} was not visible after %s second%s!" % (
472+
message = "Link text {%s} was not found after %s second%s!" % (
472473
selector,
473474
timeout,
474475
plural,
475476
)
476-
timeout_exception(ElementNotVisibleException, message)
477+
timeout_exception(LinkTextNotFoundException, message)
477478
else:
478479
return element
479480

@@ -870,12 +871,12 @@ def wait_for_element_clickable(
870871
)
871872
timeout_exception(ElementNotInteractableException, message)
872873
elif not element and by == "link text" and not is_visible:
873-
message = "Link text {%s} was not visible after %s second%s!" % (
874+
message = "Link text {%s} was not found after %s second%s!" % (
874875
selector,
875876
timeout,
876877
plural,
877878
)
878-
timeout_exception(ElementNotVisibleException, message)
879+
timeout_exception(LinkTextNotFoundException, message)
879880
elif not element and by == "link text" and is_visible:
880881
message = "Link text {%s} was not clickable after %s second%s!" % (
881882
selector,

seleniumbase/fixtures/shared_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def format_exc(exception, message):
7171
from selenium.common.exceptions import NoSuchElementException
7272
from selenium.common.exceptions import NoSuchFrameException
7373
from selenium.common.exceptions import NoSuchWindowException
74+
from seleniumbase.common.exceptions import LinkTextNotFoundException
7475
from seleniumbase.common.exceptions import NoSuchFileException
7576
from seleniumbase.common.exceptions import NoSuchOptionException
7677
from seleniumbase.common.exceptions import TextNotVisibleException
@@ -83,6 +84,10 @@ def format_exc(exception, message):
8384
exc = exceptions.ElementNotVisibleException
8485
elif exception == "ElementNotVisibleException":
8586
exc = exceptions.ElementNotVisibleException
87+
elif exception == LinkTextNotFoundException:
88+
exc = exceptions.LinkTextNotFoundException
89+
elif exception == "LinkTextNotFoundException":
90+
exc = exceptions.LinkTextNotFoundException
8691
elif exception == NoSuchElementException:
8792
exc = exceptions.NoSuchElementException
8893
elif exception == "NoSuchElementException":

0 commit comments

Comments
 (0)