Skip to content

Commit 4e389d1

Browse files
committed
Add methods for asserting exact text
1 parent 654e56b commit 4e389d1

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,19 @@ def wait_for_text_visible(self, text, selector="html", by=By.CSS_SELECTOR,
20682068
return page_actions.wait_for_text_visible(
20692069
self.driver, text, selector, by, timeout)
20702070

2071+
def wait_for_exact_text_visible(self, text, selector="html",
2072+
by=By.CSS_SELECTOR,
2073+
timeout=settings.LARGE_TIMEOUT):
2074+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
2075+
timeout = self.__get_new_timeout(timeout)
2076+
if page_utils.is_xpath_selector(selector):
2077+
by = By.XPATH
2078+
if page_utils.is_link_text_selector(selector):
2079+
selector = page_utils.get_link_text_from_selector(selector)
2080+
by = By.LINK_TEXT
2081+
return page_actions.wait_for_exact_text_visible(
2082+
self.driver, text, selector, by, timeout)
2083+
20712084
def wait_for_text(self, text, selector="html", by=By.CSS_SELECTOR,
20722085
timeout=settings.LARGE_TIMEOUT):
20732086
""" The shorter version of wait_for_text_visible() """
@@ -2111,6 +2124,29 @@ def assert_text(self, text, selector="html", by=By.CSS_SELECTOR,
21112124
self.__highlight_with_assert_success(messenger_post, selector, by)
21122125
return True
21132126

2127+
def assert_exact_text(self, text, selector="html", by=By.CSS_SELECTOR,
2128+
timeout=settings.SMALL_TIMEOUT):
2129+
""" Similar to assert_text(), but the text must be exact, rather than
2130+
exist as a subset of the full text.
2131+
(Extra whitespace at the beginning or the end doesn't count.)
2132+
Raises an exception if the element or the text is not found.
2133+
Returns True if successful. Default timeout = SMALL_TIMEOUT. """
2134+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
2135+
timeout = self.__get_new_timeout(timeout)
2136+
self.wait_for_exact_text_visible(
2137+
text, selector, by=by, timeout=timeout)
2138+
2139+
if self.demo_mode:
2140+
if page_utils.is_xpath_selector(selector):
2141+
by = By.XPATH
2142+
if page_utils.is_link_text_selector(selector):
2143+
selector = page_utils.get_link_text_from_selector(selector)
2144+
by = By.LINK_TEXT
2145+
messenger_post = ("ASSERT TEXT {%s} in %s: %s"
2146+
% (text, by, selector))
2147+
self.__highlight_with_assert_success(messenger_post, selector, by)
2148+
return True
2149+
21142150
# For backwards compatibility, earlier method names of the next
21152151
# four methods have remained even though they do the same thing,
21162152
# with the exception of assert_*, which won't return the element,

seleniumbase/fixtures/page_actions.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,49 @@ def wait_for_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
280280
(text, selector, timeout, plural))
281281

282282

283+
def wait_for_exact_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
284+
timeout=settings.LARGE_TIMEOUT):
285+
"""
286+
Searches for the specified element by the given selector. Returns the
287+
element object if the text matches exactly with the text in the element,
288+
and the text is visible.
289+
Raises an exception if the text or element do not appear
290+
in the specified timeout.
291+
@Params
292+
driver - the webdriver object (required)
293+
text - the exact text that is expected for the element (required)
294+
selector - the locator that is used (required)
295+
by - the method to search for the locator (Default: By.CSS_SELECTOR)
296+
timeout - the time to wait for elements in seconds
297+
@Returns
298+
A web element object that contains the text searched for
299+
"""
300+
301+
element = None
302+
start_ms = time.time() * 1000.0
303+
stop_ms = start_ms + (timeout * 1000.0)
304+
for x in range(int(timeout * 10)):
305+
try:
306+
element = driver.find_element(by=by, value=selector)
307+
if element.is_displayed() and text.strip() == element.text.strip():
308+
return element
309+
else:
310+
element = None
311+
raise Exception()
312+
except Exception:
313+
now_ms = time.time() * 1000.0
314+
if now_ms >= stop_ms:
315+
break
316+
time.sleep(0.1)
317+
plural = "s"
318+
if timeout == 1:
319+
plural = ""
320+
if not element:
321+
raise ElementNotVisibleException(
322+
"Expected exact text {%s} for {%s} was not visible "
323+
"after %s second%s!" % (text, selector, timeout, plural))
324+
325+
283326
def wait_for_element_absent(driver, selector, by=By.CSS_SELECTOR,
284327
timeout=settings.LARGE_TIMEOUT):
285328
"""

0 commit comments

Comments
 (0)