Skip to content

Commit e085517

Browse files
committed
Add method: assert_title_contains(substring)
1 parent 900cb3e commit e085517

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ self.assert_attribute(
458458

459459
self.assert_title(title)
460460

461+
self.assert_title_contains(substring)
462+
461463
self.assert_no_js_errors()
462464

463465
self.inspect_html()

sbase/steps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,11 @@ def assert_text_not_visible(context, text):
604604
sb = context.sb
605605
text = normalize_text(text)
606606
sb.assert_text_not_visible(text)
607+
608+
609+
@step("Assert title contains '{substring}'")
610+
@step('Assert title contains "{substring}"')
611+
def assert_title_contains(context, substring):
612+
sb = context.sb
613+
substring = normalize_text(substring)
614+
sb.assert_title_contains(substring)

seleniumbase/behave/behave_helper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ def generate_gherkin(srt_actions):
244244
sb_actions.append('%s "%s"' % (method, action[1]))
245245
else:
246246
sb_actions.append("%s '%s'" % (method, action[1]))
247+
elif action[0] == "as_tc":
248+
method = "Assert title contains"
249+
if '"' not in action[1]:
250+
sb_actions.append('%s "%s"' % (method, action[1]))
251+
else:
252+
sb_actions.append("%s '%s'" % (method, action[1]))
247253
elif action[0] == "as_df":
248254
method = "Assert downloaded file"
249255
if '"' not in action[1]:

seleniumbase/fixtures/base_case.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,7 @@ def __process_recorded_actions(self):
40754075
ext_actions.append("hi_li")
40764076
ext_actions.append("as_lt")
40774077
ext_actions.append("as_ti")
4078+
ext_actions.append("as_tc")
40784079
ext_actions.append("as_df")
40794080
ext_actions.append("do_fi")
40804081
ext_actions.append("as_at")
@@ -4406,6 +4407,12 @@ def __process_recorded_actions(self):
44064407
sb_actions.append('self.%s("%s")' % (method, action[1]))
44074408
else:
44084409
sb_actions.append("self.%s('%s')" % (method, action[1]))
4410+
elif action[0] == "as_tc":
4411+
method = "assert_title_contains"
4412+
if '"' not in action[1]:
4413+
sb_actions.append('self.%s("%s")' % (method, action[1]))
4414+
else:
4415+
sb_actions.append("self.%s('%s')" % (method, action[1]))
44094416
elif action[0] == "as_df":
44104417
method = "assert_downloaded_file"
44114418
if '"' not in action[1]:
@@ -6354,7 +6361,50 @@ def assert_title(self, title):
63546361
from seleniumbase.fixtures.words import SD
63556362

63566363
a_t = SD.translate_assert_title(self._language)
6357-
messenger_post = "%s: {%s}" % (a_t, title)
6364+
messenger_post = "%s: {%s}" % (a_t, expected)
6365+
self.__highlight_with_assert_success(messenger_post, "html")
6366+
if self.recorder_mode:
6367+
url = self.get_current_url()
6368+
if url and len(url) > 0:
6369+
if ("http:") in url or ("https:") in url or ("file:") in url:
6370+
if self.get_session_storage_item("pause_recorder") == "no":
6371+
time_stamp = self.execute_script("return Date.now();")
6372+
origin = self.get_origin()
6373+
action = ["as_ti", expected, origin, time_stamp]
6374+
self.__extra_actions.append(action)
6375+
return True
6376+
6377+
def assert_title_contains(self, substring):
6378+
"""Asserts that the title substring appears in the web page title.
6379+
When a web page initially loads, the title starts as the URL,
6380+
but then the title switches over to the actual page title.
6381+
In Recorder Mode, this assertion is skipped because the Recorder
6382+
changes the page title to the selector of the hovered element.
6383+
"""
6384+
self.wait_for_ready_state_complete()
6385+
expected = substring.strip()
6386+
actual = self.get_page_title().strip()
6387+
error = (
6388+
"Expected title substring [%s] does not appear "
6389+
"in the actual page title [%s]!"
6390+
)
6391+
try:
6392+
if not self.recorder_mode:
6393+
self.assertIn(expected, actual, error % (expected, actual))
6394+
except Exception:
6395+
self.wait_for_ready_state_complete()
6396+
time.sleep(2)
6397+
actual = self.get_page_title().strip()
6398+
try:
6399+
self.assertIn(expected, actual, error % (expected, actual))
6400+
except Exception:
6401+
self.wait_for_ready_state_complete()
6402+
time.sleep(2)
6403+
actual = self.get_page_title().strip()
6404+
self.assertIn(expected, actual, error % (expected, actual))
6405+
if self.demo_mode and not self.recorder_mode:
6406+
a_t = "ASSERT TITLE CONTAINS"
6407+
messenger_post = "%s: {%s}" % (a_t, expected)
63586408
self.__highlight_with_assert_success(messenger_post, "html")
63596409
if self.recorder_mode:
63606410
url = self.get_current_url()
@@ -6363,7 +6413,7 @@ def assert_title(self, title):
63636413
if self.get_session_storage_item("pause_recorder") == "no":
63646414
time_stamp = self.execute_script("return Date.now();")
63656415
origin = self.get_origin()
6366-
action = ["as_ti", title, origin, time_stamp]
6416+
action = ["as_tc", expected, origin, time_stamp]
63676417
self.__extra_actions.append(action)
63686418
return True
63696419

0 commit comments

Comments
 (0)