Source code for capybara.node.document_matchers
from capybara.exceptions import ExpectationNotMet
from capybara.queries.title_query import TitleQuery
[docs]class DocumentMatchersMixin(object):
[docs] def assert_title(self, title, **kwargs):
"""
Asserts that the page has the given title.
Args:
title (str | RegexObject): The string or regex that the title should match.
**kwargs: Arbitrary keyword arguments for :class:`TitleQuery`.
Returns:
True
Raises:
ExpectationNotMet: If the assertion hasn't succeeded during the wait time.
"""
query = TitleQuery(title, **kwargs)
@self.synchronize(wait=query.wait)
def assert_title():
if not query.resolves_for(self):
raise ExpectationNotMet(query.failure_message)
return True
return assert_title()
[docs] def assert_no_title(self, title, **kwargs):
"""
Asserts that the page doesn't have the given title.
Args:
title (str | RegexObject): The string that the title should include.
**kwargs: Arbitrary keyword arguments for :class:`TitleQuery`.
Returns:
True
Raises:
ExpectationNotMet: If the assertion hasn't succeeded during the wait time.
"""
query = TitleQuery(title, **kwargs)
@self.synchronize(wait=query.wait)
def assert_no_title():
if query.resolves_for(self):
raise ExpectationNotMet(query.negative_failure_message)
return True
return assert_no_title()
[docs] def has_title(self, title, **kwargs):
"""
Checks if the page has the given title.
Args:
title (str | RegexObject): The string or regex that the title should match.
**kwargs: Arbitrary keyword arguments for :class:`TitleQuery`.
Returns:
bool: Whether it matches.
"""
try:
self.assert_title(title, **kwargs)
return True
except ExpectationNotMet:
return False
[docs] def has_no_title(self, title, **kwargs):
"""
Checks if the page doesn't have the given title.
Args:
title (str | RegexObject): The string that the title should include.
**kwargs: Arbitrary keyword arguments for :class:`TitleQuery`.
Returns:
bool: Whether it doesn't match.
"""
try:
self.assert_no_title(title, **kwargs)
return True
except ExpectationNotMet:
return False