Skip to content

Commit 6f90491

Browse files
authored
Merge pull request #1655 from seleniumbase/add-methods-for-url-asserts
Add methods for URL asserts
2 parents a808b28 + 1fdd8f5 commit 6f90491

File tree

18 files changed

+330
-3
lines changed

18 files changed

+330
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
<h1>SeleniumBase</h1>
1111

12-
<h3 align="center"><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.io/cdn/img/sb_logo_10t.png" alt="SeleniumBase" title="SeleniumBase" width="280" /></a></h3>
12+
<h3 align="center"><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.io/cdn/img/sb_logo_10t.png" alt="SeleniumBase" title="SeleniumBase" width="266" /></a></h3>
1313

14-
<h3 align="center">A Powerful Browser Testing Framework for Python.</h3>
14+
<h3 align="center">Create reliable end-to-end browser tests with Python.</h3>
1515

1616
<p align="center"><a href="https://pypi.python.org/pypi/seleniumbase" target="_blank"><img src="https://img.shields.io/pypi/v/seleniumbase.svg?color=3399EE" alt="PyPI version" /></a> <a href="https://github.com/seleniumbase/SeleniumBase/releases" target="_blank"><img src="https://img.shields.io/github/v/release/seleniumbase/SeleniumBase.svg?color=22AAEE" alt="GitHub version" /></a> <a href="https://seleniumbase.io"><img src="https://img.shields.io/badge/docs-seleniumbase.io-11BBAA.svg" alt="SeleniumBase Docs" /></a> <a href="https://github.com/seleniumbase/SeleniumBase/actions" target="_blank"><img src="https://github.com/seleniumbase/SeleniumBase/workflows/CI%20build/badge.svg" alt="SeleniumBase GitHub Actions" /></a> <a href="https://gitter.im/seleniumbase/SeleniumBase" target="_blank"><img src="https://badges.gitter.im/seleniumbase/SeleniumBase.svg" alt="SeleniumBase" /></a></p>
1717

examples/test_url_asserts.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from seleniumbase import BaseCase
2+
BaseCase.main(__name__, __file__)
3+
4+
5+
class URLTestClass(BaseCase):
6+
def test_url_asserts(self):
7+
self.open("https://seleniumbase.io/")
8+
self.assert_url("https://seleniumbase.io/")
9+
self.assert_title_contains("SeleniumBase")
10+
self.js_click('nav a:contains("Coffee Cart")')
11+
self.assert_url_contains("/coffee")
12+
self.assert_title("Coffee Cart")

help_docs/method_summary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ self.assert_title(title)
467467

468468
self.assert_title_contains(substring)
469469

470+
self.assert_url(url)
471+
472+
self.assert_url_contains(substring)
473+
470474
self.assert_no_js_errors(exclude=[])
471475

472476
self.inspect_html()

sbase/steps.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,19 @@ def accept_alert(context):
650650
def dismiss_alert(context):
651651
sb = context.sb
652652
sb.dismiss_alert()
653+
654+
655+
@step("Assert URL '{url}'")
656+
@step('Assert URL "{url}"')
657+
def assert_url(context, url):
658+
sb = context.sb
659+
url = normalize_text(url)
660+
sb.assert_url(url)
661+
662+
663+
@step("Assert URL contains '{substring}'")
664+
@step('Assert URL contains "{substring}"')
665+
def assert_url_contains(context, substring):
666+
sb = context.sb
667+
substring = normalize_text(substring)
668+
sb.assert_url_contains(substring)

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.11.1"
2+
__version__ = "4.11.2"

seleniumbase/behave/behave_helper.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ def generate_gherkin(srt_actions):
259259
sb_actions.append('%s "%s"' % (method, action[1]))
260260
else:
261261
sb_actions.append("%s '%s'" % (method, action[1]))
262+
elif action[0] == "a_url":
263+
method = "Assert URL"
264+
if '"' not in action[1]:
265+
sb_actions.append('%s "%s"' % (method, action[1]))
266+
else:
267+
sb_actions.append("%s '%s'" % (method, action[1]))
268+
elif action[0] == "a_u_c":
269+
method = "Assert URL contains"
270+
if '"' not in action[1]:
271+
sb_actions.append('%s "%s"' % (method, action[1]))
272+
else:
273+
sb_actions.append("%s '%s'" % (method, action[1]))
262274
elif action[0] == "as_df":
263275
method = "Assert downloaded file"
264276
if '"' not in action[1]:

seleniumbase/fixtures/base_case.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,6 +4315,8 @@ def __process_recorded_actions(self):
43154315
ext_actions.append("as_lt")
43164316
ext_actions.append("as_ti")
43174317
ext_actions.append("as_tc")
4318+
ext_actions.append("a_url")
4319+
ext_actions.append("a_u_c")
43184320
ext_actions.append("as_df")
43194321
ext_actions.append("do_fi")
43204322
ext_actions.append("as_at")
@@ -4669,6 +4671,18 @@ def __process_recorded_actions(self):
46694671
sb_actions.append('self.%s("%s")' % (method, action[1]))
46704672
else:
46714673
sb_actions.append("self.%s('%s')" % (method, action[1]))
4674+
elif action[0] == "a_url":
4675+
method = "assert_url"
4676+
if '"' not in action[1]:
4677+
sb_actions.append('self.%s("%s")' % (method, action[1]))
4678+
else:
4679+
sb_actions.append("self.%s('%s')" % (method, action[1]))
4680+
elif action[0] == "a_u_c":
4681+
method = "assert_url_contains"
4682+
if '"' not in action[1]:
4683+
sb_actions.append('self.%s("%s")' % (method, action[1]))
4684+
else:
4685+
sb_actions.append("self.%s('%s')" % (method, action[1]))
46724686
elif action[0] == "as_df":
46734687
method = "assert_downloaded_file"
46744688
if '"' not in action[1]:
@@ -6724,6 +6738,10 @@ def assert_title_contains(self, substring):
67246738
self.assertIn(expected, actual, error % (expected, actual))
67256739
if self.demo_mode and not self.recorder_mode:
67266740
a_t = "ASSERT TITLE CONTAINS"
6741+
if self._language != "English":
6742+
from seleniumbase.fixtures.words import SD
6743+
6744+
a_t = SD.translate_assert_title_contains(self._language)
67276745
messenger_post = "<b>%s</b>: {%s}" % (a_t, expected)
67286746
self.__highlight_with_assert_success(messenger_post, "html")
67296747
if self.recorder_mode:
@@ -6737,6 +6755,85 @@ def assert_title_contains(self, substring):
67376755
self.__extra_actions.append(action)
67386756
return True
67396757

6758+
def assert_url(self, url):
6759+
"""Asserts that the web page URL matches the expected URL."""
6760+
self.wait_for_ready_state_complete()
6761+
expected = url.strip()
6762+
actual = self.get_current_url().strip()
6763+
error = "Expected URL [%s] does not match the actual URL [%s]!"
6764+
try:
6765+
self.assertEqual(expected, actual, error % (expected, actual))
6766+
except Exception:
6767+
self.wait_for_ready_state_complete()
6768+
time.sleep(2)
6769+
actual = self.get_current_url().strip()
6770+
try:
6771+
self.assertEqual(expected, actual, error % (expected, actual))
6772+
except Exception:
6773+
self.wait_for_ready_state_complete()
6774+
time.sleep(2)
6775+
actual = self.get_current_url().strip()
6776+
self.assertEqual(expected, actual, error % (expected, actual))
6777+
if self.demo_mode and not self.recorder_mode:
6778+
a_u = "ASSERT URL"
6779+
if self._language != "English":
6780+
from seleniumbase.fixtures.words import SD
6781+
6782+
a_u = SD.translate_assert_url(self._language)
6783+
messenger_post = "<b>%s</b>: {%s}" % (a_u, expected)
6784+
self.__highlight_with_assert_success(messenger_post, "html")
6785+
if self.recorder_mode:
6786+
url = self.get_current_url()
6787+
if url and len(url) > 0:
6788+
if ("http:") in url or ("https:") in url or ("file:") in url:
6789+
if self.get_session_storage_item("pause_recorder") == "no":
6790+
time_stamp = self.execute_script("return Date.now();")
6791+
origin = self.get_origin()
6792+
action = ["a_url", expected, origin, time_stamp]
6793+
self.__extra_actions.append(action)
6794+
return True
6795+
6796+
def assert_url_contains(self, substring):
6797+
"""Asserts that the URL substring appears in the full URL."""
6798+
self.wait_for_ready_state_complete()
6799+
expected = substring.strip()
6800+
actual = self.get_current_url().strip()
6801+
error = (
6802+
"Expected URL substring [%s] does not appear "
6803+
"in the full URL [%s]!"
6804+
)
6805+
try:
6806+
self.assertIn(expected, actual, error % (expected, actual))
6807+
except Exception:
6808+
self.wait_for_ready_state_complete()
6809+
time.sleep(2)
6810+
actual = self.get_current_url().strip()
6811+
try:
6812+
self.assertIn(expected, actual, error % (expected, actual))
6813+
except Exception:
6814+
self.wait_for_ready_state_complete()
6815+
time.sleep(2)
6816+
actual = self.get_current_url().strip()
6817+
self.assertIn(expected, actual, error % (expected, actual))
6818+
if self.demo_mode and not self.recorder_mode:
6819+
a_u = "ASSERT URL CONTAINS"
6820+
if self._language != "English":
6821+
from seleniumbase.fixtures.words import SD
6822+
6823+
a_u = SD.translate_assert_url_contains(self._language)
6824+
messenger_post = "<b>%s</b>: {%s}" % (a_u, expected)
6825+
self.__highlight_with_assert_success(messenger_post, "html")
6826+
if self.recorder_mode:
6827+
url = self.get_current_url()
6828+
if url and len(url) > 0:
6829+
if ("http:") in url or ("https:") in url or ("file:") in url:
6830+
if self.get_session_storage_item("pause_recorder") == "no":
6831+
time_stamp = self.execute_script("return Date.now();")
6832+
origin = self.get_origin()
6833+
action = ["a_u_c", expected, origin, time_stamp]
6834+
self.__extra_actions.append(action)
6835+
return True
6836+
67406837
def assert_no_js_errors(self, exclude=[]):
67416838
"""Asserts current URL has no "SEVERE"-level JavaScript errors.
67426839
Works ONLY on Chromium browsers (Chrome or Edge).

seleniumbase/fixtures/words.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,48 @@ def translate_assert_title(language):
100100
words["Spanish"] = "VERIFICAR TÍTULO"
101101
return words[language]
102102

103+
def translate_assert_title_contains(language):
104+
words = {}
105+
words["English"] = "ASSERT TITLE CONTAINS"
106+
words["Chinese"] = "断言标题包含"
107+
words["Dutch"] = "CONTROLEREN TITEL BEVAT"
108+
words["French"] = "VÉRIFIER TITRE CONTIENT"
109+
words["Italian"] = "VERIFICARE TITOLO CONTIENE"
110+
words["Japanese"] = "タイトル部分文字列を確認する"
111+
words["Korean"] = "제목 부분 확인"
112+
words["Portuguese"] = "VERIFICAR TÍTULO CONTÉM"
113+
words["Russian"] = "ПОДТВЕРДИТЬ НАЗВАНИЕ СОДЕРЖИТ"
114+
words["Spanish"] = "VERIFICAR TÍTULO CONTIENE"
115+
return words[language]
116+
117+
def translate_assert_url(language):
118+
words = {}
119+
words["English"] = "ASSERT URL"
120+
words["Chinese"] = "断言 URL"
121+
words["Dutch"] = "CONTROLEREN URL"
122+
words["French"] = "VÉRIFIER URL"
123+
words["Italian"] = "VERIFICARE URL"
124+
words["Japanese"] = "URL を確認する"
125+
words["Korean"] = "URL 확인"
126+
words["Portuguese"] = "VERIFICAR URL"
127+
words["Russian"] = "ПОДТВЕРДИТЬ URL"
128+
words["Spanish"] = "VERIFICAR URL"
129+
return words[language]
130+
131+
def translate_assert_url_contains(language):
132+
words = {}
133+
words["English"] = "ASSERT URL CONTAINS"
134+
words["Chinese"] = "断言 URL 包含"
135+
words["Dutch"] = "CONTROLEREN URL BEVAT"
136+
words["French"] = "VÉRIFIER URL CONTIENT"
137+
words["Italian"] = "VERIFICARE URL CONTIENE"
138+
words["Japanese"] = "URL を確認する"
139+
words["Korean"] = "URL 확인"
140+
words["Portuguese"] = "VERIFICAR URL CONTÉM"
141+
words["Russian"] = "ПОДТВЕРДИТЬ URL СОДЕРЖИТ"
142+
words["Spanish"] = "VERIFICAR URL CONTIENE"
143+
return words[language]
144+
103145
def translate_assert_no_404_errors(language):
104146
words = {}
105147
words["English"] = "ASSERT NO 404 ERRORS"

seleniumbase/translate/chinese.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,22 @@ def 断言属性(self, *args, **kwargs):
101101
# assert_attribute(selector, attribute, value)
102102
return self.assert_attribute(*args, **kwargs)
103103

104+
def 断言URL(self, *args, **kwargs):
105+
# assert_url(url)
106+
return self.assert_url(*args, **kwargs)
107+
108+
def 断言URL包含(self, *args, **kwargs):
109+
# assert_url_contains(substring)
110+
return self.assert_url_contains(*args, **kwargs)
111+
104112
def 断言标题(self, *args, **kwargs):
105113
# assert_title(title)
106114
return self.assert_title(*args, **kwargs)
107115

116+
def 断言标题包含(self, *args, **kwargs):
117+
# assert_title_contains(substring)
118+
return self.assert_title_contains(*args, **kwargs)
119+
108120
def 获取标题(self, *args, **kwargs):
109121
# get_title()
110122
return self.get_title(*args, **kwargs)

seleniumbase/translate/dutch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,22 @@ def controleren_attribuut(self, *args, **kwargs):
101101
# assert_attribute(selector, attribute, value)
102102
return self.assert_attribute(*args, **kwargs)
103103

104+
def controleren_url(self, *args, **kwargs):
105+
# assert_url(url)
106+
return self.assert_url(*args, **kwargs)
107+
108+
def controleren_url_bevat(self, *args, **kwargs):
109+
# assert_url_contains(substring)
110+
return self.assert_url_contains(*args, **kwargs)
111+
104112
def controleren_titel(self, *args, **kwargs):
105113
# assert_title(title)
106114
return self.assert_title(*args, **kwargs)
107115

116+
def controleren_titel_bevat(self, *args, **kwargs):
117+
# assert_title_contains(substring)
118+
return self.assert_title_contains(*args, **kwargs)
119+
108120
def titel_ophalen(self, *args, **kwargs):
109121
# get_title()
110122
return self.get_title(*args, **kwargs)

0 commit comments

Comments
 (0)