| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- # Copyright (c) Microsoft Corporation.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- import collections.abc
- from contextlib import contextmanager
- from typing import Any, Iterator, List, Literal, Optional, Pattern, Sequence, Union
- from urllib.parse import urljoin
- from playwright._impl._api_structures import (
- AriaRole,
- ExpectedTextValue,
- FrameExpectOptions,
- FrameExpectResult,
- )
- from playwright._impl._connection import format_call_log
- from playwright._impl._errors import Error
- from playwright._impl._fetch import APIResponse
- from playwright._impl._helper import is_textual_mime_type
- from playwright._impl._js_handle import parse_value
- from playwright._impl._locator import Locator
- from playwright._impl._page import Page
- from playwright._impl._str_utils import escape_regex_flags
- _soft_errors: Optional[List[AssertionError]] = None
- @contextmanager
- def _soft_scope() -> Iterator[List[AssertionError]]:
- global _soft_errors
- assert _soft_errors is None, "nested soft assertion scopes are not supported"
- _soft_errors = []
- try:
- yield _soft_errors
- finally:
- _soft_errors = None
- def _record_soft_or_raise(error: AssertionError, is_soft: bool) -> None:
- __tracebackhide__ = True
- if is_soft:
- if _soft_errors is None:
- raise RuntimeError(
- "expect.soft(...) requires pytest-playwright>=0.7.3 "
- "(or pytest-playwright-asyncio>=0.7.3). Upgrade the plugin, "
- "or use a regular expect(...) assertion."
- )
- _soft_errors.append(error)
- return
- raise error
- class AssertionsBase:
- def __init__(
- self,
- locator: Locator,
- timeout: float = None,
- is_not: bool = False,
- message: Optional[str] = None,
- is_soft: bool = False,
- ) -> None:
- self._actual_locator = locator
- self._loop = locator._loop
- self._dispatcher_fiber = locator._dispatcher_fiber
- self._timeout = timeout
- self._is_not = is_not
- self._custom_message = message
- self._is_soft = is_soft
- async def _call_expect(
- self, expression: str, expect_options: FrameExpectOptions, title: Optional[str]
- ) -> FrameExpectResult:
- raise NotImplementedError(
- "_call_expect must be implemented in a derived class."
- )
- async def _expect_impl(
- self,
- expression: str,
- expect_options: FrameExpectOptions,
- expected: Any,
- message: str,
- title: str = None,
- ) -> None:
- __tracebackhide__ = True
- expect_options["isNot"] = self._is_not
- if expect_options.get("timeout") is None:
- expect_options["timeout"] = self._timeout or 5_000
- if expect_options["isNot"]:
- message = message.replace("expected to", "expected not to")
- if "useInnerText" in expect_options and expect_options["useInnerText"] is None:
- del expect_options["useInnerText"]
- result = await self._call_expect(expression, expect_options, title)
- if result["matches"] == self._is_not:
- received = result.get("received") or {}
- if isinstance(received, dict):
- if "value" in received and received["value"] is not None:
- actual = parse_value(received["value"])
- else:
- actual = received.get("ariaSnapshot")
- else:
- actual = received
- if self._custom_message:
- out_message = self._custom_message
- if expected is not None:
- out_message += f"\nExpected value: '{expected or '<None>'}'"
- else:
- out_message = (
- f"{message} '{expected}'" if expected is not None else f"{message}"
- )
- error_message = result.get("errorMessage")
- error_message = f"\n{error_message}" if error_message else ""
- _record_soft_or_raise(
- AssertionError(
- f"{out_message}\nActual value: {actual}{error_message} {format_call_log(result.get('log'))}"
- ),
- self._is_soft,
- )
- class PageAssertions(AssertionsBase):
- def __init__(
- self,
- page: Page,
- timeout: float = None,
- is_not: bool = False,
- message: Optional[str] = None,
- is_soft: bool = False,
- ) -> None:
- super().__init__(page.locator(":root"), timeout, is_not, message, is_soft)
- self._actual_page = page
- async def _call_expect(
- self, expression: str, expect_options: FrameExpectOptions, title: Optional[str]
- ) -> FrameExpectResult:
- __tracebackhide__ = True
- return await self._actual_page.main_frame._expect(
- None, expression, expect_options, title
- )
- @property
- def _not(self) -> "PageAssertions":
- return PageAssertions(
- self._actual_page,
- self._timeout,
- not self._is_not,
- self._custom_message,
- self._is_soft,
- )
- async def to_have_title(
- self, titleOrRegExp: Union[Pattern[str], str], timeout: float = None
- ) -> None:
- __tracebackhide__ = True
- expected_values = to_expected_text_values(
- [titleOrRegExp], normalize_white_space=True
- )
- await self._expect_impl(
- "to.have.title",
- FrameExpectOptions(expectedText=expected_values, timeout=timeout),
- titleOrRegExp,
- "Page title expected to be",
- 'Expect "to_have_title"',
- )
- async def not_to_have_title(
- self, titleOrRegExp: Union[Pattern[str], str], timeout: float = None
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_title(titleOrRegExp, timeout)
- async def to_have_url(
- self,
- urlOrRegExp: Union[str, Pattern[str]],
- timeout: float = None,
- ignoreCase: bool = None,
- ) -> None:
- __tracebackhide__ = True
- base_url = self._actual_page.context._base_url
- if isinstance(urlOrRegExp, str) and base_url:
- urlOrRegExp = urljoin(base_url, urlOrRegExp)
- expected_text = to_expected_text_values([urlOrRegExp], ignoreCase=ignoreCase)
- await self._expect_impl(
- "to.have.url",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- urlOrRegExp,
- "Page URL expected to be",
- 'Expect "to_have_url"',
- )
- async def not_to_have_url(
- self,
- urlOrRegExp: Union[Pattern[str], str],
- timeout: float = None,
- ignoreCase: bool = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_url(urlOrRegExp, timeout, ignoreCase)
- async def to_match_aria_snapshot(
- self, expected: str, timeout: float = None
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.match.aria",
- FrameExpectOptions(expectedValue=expected, timeout=timeout),
- expected,
- "Page expected to match Aria snapshot",
- 'Expect "to_match_aria_snapshot"',
- )
- async def not_to_match_aria_snapshot(
- self, expected: str, timeout: float = None
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_match_aria_snapshot(expected, timeout)
- class LocatorAssertions(AssertionsBase):
- def __init__(
- self,
- locator: Locator,
- timeout: float = None,
- is_not: bool = False,
- message: Optional[str] = None,
- is_soft: bool = False,
- ) -> None:
- super().__init__(locator, timeout, is_not, message, is_soft)
- self._actual_locator = locator
- async def _call_expect(
- self, expression: str, expect_options: FrameExpectOptions, title: Optional[str]
- ) -> FrameExpectResult:
- __tracebackhide__ = True
- return await self._actual_locator._expect(expression, expect_options, title)
- @property
- def _not(self) -> "LocatorAssertions":
- return LocatorAssertions(
- self._actual_locator,
- self._timeout,
- not self._is_not,
- self._custom_message,
- self._is_soft,
- )
- async def to_contain_text(
- self,
- expected: Union[
- Sequence[str],
- Sequence[Pattern[str]],
- Sequence[Union[Pattern[str], str]],
- Pattern[str],
- str,
- ],
- useInnerText: bool = None,
- timeout: float = None,
- ignoreCase: bool = None,
- ) -> None:
- __tracebackhide__ = True
- if isinstance(expected, collections.abc.Sequence) and not isinstance(
- expected, str
- ):
- expected_text = to_expected_text_values(
- expected,
- match_substring=True,
- normalize_white_space=True,
- ignoreCase=ignoreCase,
- )
- await self._expect_impl(
- "to.contain.text.array",
- FrameExpectOptions(
- expectedText=expected_text,
- useInnerText=useInnerText,
- timeout=timeout,
- ),
- expected,
- "Locator expected to contain text",
- 'Expect "to_contain_text"',
- )
- else:
- expected_text = to_expected_text_values(
- [expected],
- match_substring=True,
- normalize_white_space=True,
- ignoreCase=ignoreCase,
- )
- await self._expect_impl(
- "to.have.text",
- FrameExpectOptions(
- expectedText=expected_text,
- useInnerText=useInnerText,
- timeout=timeout,
- ),
- expected,
- "Locator expected to contain text",
- 'Expect "to_contain_text"',
- )
- async def not_to_contain_text(
- self,
- expected: Union[
- Sequence[str],
- Sequence[Pattern[str]],
- Sequence[Union[Pattern[str], str]],
- Pattern[str],
- str,
- ],
- useInnerText: bool = None,
- timeout: float = None,
- ignoreCase: bool = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_contain_text(expected, useInnerText, timeout, ignoreCase)
- async def to_have_attribute(
- self,
- name: str,
- value: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- expected_text = to_expected_text_values([value], ignoreCase=ignoreCase)
- await self._expect_impl(
- "to.have.attribute.value",
- FrameExpectOptions(
- expressionArg=name, expectedText=expected_text, timeout=timeout
- ),
- value,
- "Locator expected to have attribute",
- 'Expect "to_have_attribute"',
- )
- async def not_to_have_attribute(
- self,
- name: str,
- value: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_attribute(
- name, value, ignoreCase=ignoreCase, timeout=timeout
- )
- async def to_have_class(
- self,
- expected: Union[
- Sequence[str],
- Sequence[Pattern[str]],
- Sequence[Union[Pattern[str], str]],
- Pattern[str],
- str,
- ],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- if isinstance(expected, collections.abc.Sequence) and not isinstance(
- expected, str
- ):
- expected_text = to_expected_text_values(expected)
- await self._expect_impl(
- "to.have.class.array",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- expected,
- "Locator expected to have class",
- 'Expect "to_have_class"',
- )
- else:
- expected_text = to_expected_text_values([expected])
- await self._expect_impl(
- "to.have.class",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- expected,
- "Locator expected to have class",
- 'Expect "to_have_class"',
- )
- async def not_to_have_class(
- self,
- expected: Union[
- Sequence[str],
- Sequence[Pattern[str]],
- Sequence[Union[Pattern[str], str]],
- Pattern[str],
- str,
- ],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_class(expected, timeout)
- async def to_contain_class(
- self,
- expected: Union[
- Sequence[str],
- str,
- ],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- if isinstance(expected, collections.abc.Sequence) and not isinstance(
- expected, str
- ):
- expected_text = to_expected_text_values(expected)
- await self._expect_impl(
- "to.contain.class.array",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- expected,
- "Locator expected to contain class names",
- 'Expect "to_contain_class"',
- )
- else:
- expected_text = to_expected_text_values([expected])
- await self._expect_impl(
- "to.contain.class",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- expected,
- "Locator expected to contain class",
- 'Expect "to_contain_class"',
- )
- async def not_to_contain_class(
- self,
- expected: Union[
- Sequence[str],
- str,
- ],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_contain_class(expected, timeout)
- async def to_have_count(
- self,
- count: int,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.have.count",
- FrameExpectOptions(expectedNumber=count, timeout=timeout),
- count,
- "Locator expected to have count",
- 'Expect "to_have_count"',
- )
- async def not_to_have_count(
- self,
- count: int,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_count(count, timeout)
- async def to_have_css(
- self,
- name: str,
- value: Union[str, Pattern[str]],
- timeout: float = None,
- pseudo: Literal["after", "before"] = None,
- ) -> None:
- __tracebackhide__ = True
- expected_text = to_expected_text_values([value])
- await self._expect_impl(
- "to.have.css",
- FrameExpectOptions(
- expressionArg=name,
- expectedText=expected_text,
- timeout=timeout,
- pseudo=pseudo,
- ),
- value,
- "Locator expected to have CSS",
- 'Expect "to_have_css"',
- )
- async def not_to_have_css(
- self,
- name: str,
- value: Union[str, Pattern[str]],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_css(name, value, timeout)
- async def to_have_id(
- self,
- id: Union[str, Pattern[str]],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- expected_text = to_expected_text_values([id])
- await self._expect_impl(
- "to.have.id",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- id,
- "Locator expected to have ID",
- 'Expect "to_have_id"',
- )
- async def not_to_have_id(
- self,
- id: Union[str, Pattern[str]],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_id(id, timeout)
- async def to_have_js_property(
- self,
- name: str,
- value: Any,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.have.property",
- FrameExpectOptions(
- expressionArg=name, expectedValue=value, timeout=timeout
- ),
- value,
- "Locator expected to have JS Property",
- 'Expect "to_have_property"',
- )
- async def not_to_have_js_property(
- self,
- name: str,
- value: Any,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_js_property(name, value, timeout)
- async def to_have_value(
- self,
- value: Union[str, Pattern[str]],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- expected_text = to_expected_text_values([value])
- await self._expect_impl(
- "to.have.value",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- value,
- "Locator expected to have Value",
- 'Expect "to_have_value"',
- )
- async def not_to_have_value(
- self,
- value: Union[str, Pattern[str]],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_value(value, timeout)
- async def to_have_values(
- self,
- values: Union[
- Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
- ],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- expected_text = to_expected_text_values(values)
- await self._expect_impl(
- "to.have.values",
- FrameExpectOptions(expectedText=expected_text, timeout=timeout),
- values,
- "Locator expected to have Values",
- 'Expect "to_have_values"',
- )
- async def not_to_have_values(
- self,
- values: Union[
- Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
- ],
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_values(values, timeout)
- async def to_have_text(
- self,
- expected: Union[
- Sequence[str],
- Sequence[Pattern[str]],
- Sequence[Union[Pattern[str], str]],
- Pattern[str],
- str,
- ],
- useInnerText: bool = None,
- timeout: float = None,
- ignoreCase: bool = None,
- ) -> None:
- __tracebackhide__ = True
- if isinstance(expected, collections.abc.Sequence) and not isinstance(
- expected, str
- ):
- expected_text = to_expected_text_values(
- expected,
- normalize_white_space=True,
- ignoreCase=ignoreCase,
- )
- await self._expect_impl(
- "to.have.text.array",
- FrameExpectOptions(
- expectedText=expected_text,
- useInnerText=useInnerText,
- timeout=timeout,
- ),
- expected,
- "Locator expected to have text",
- 'Expect "to_have_text"',
- )
- else:
- expected_text = to_expected_text_values(
- [expected], normalize_white_space=True, ignoreCase=ignoreCase
- )
- await self._expect_impl(
- "to.have.text",
- FrameExpectOptions(
- expectedText=expected_text,
- useInnerText=useInnerText,
- timeout=timeout,
- ),
- expected,
- "Locator expected to have text",
- 'Expect "to_have_text"',
- )
- async def not_to_have_text(
- self,
- expected: Union[
- Sequence[str],
- Sequence[Pattern[str]],
- Sequence[Union[Pattern[str], str]],
- Pattern[str],
- str,
- ],
- useInnerText: bool = None,
- timeout: float = None,
- ignoreCase: bool = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_text(expected, useInnerText, timeout, ignoreCase)
- async def to_be_attached(
- self,
- attached: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- if attached is None:
- attached = True
- attached_string = "attached" if attached else "detached"
- await self._expect_impl(
- ("to.be.attached" if attached else "to.be.detached"),
- FrameExpectOptions(timeout=timeout),
- None,
- f"Locator expected to be {attached_string}",
- 'Expect "to_be_attached"',
- )
- async def to_be_checked(
- self,
- timeout: float = None,
- checked: bool = None,
- indeterminate: bool = None,
- ) -> None:
- __tracebackhide__ = True
- expected_value = {}
- if indeterminate is not None:
- expected_value["indeterminate"] = indeterminate
- if checked is not None:
- expected_value["checked"] = checked
- checked_string: str
- if indeterminate:
- checked_string = "indeterminate"
- else:
- checked_string = "unchecked" if checked is False else "checked"
- await self._expect_impl(
- "to.be.checked",
- FrameExpectOptions(timeout=timeout, expectedValue=expected_value),
- None,
- f"Locator expected to be {checked_string}",
- 'Expect "to_be_checked"',
- )
- async def not_to_be_attached(
- self,
- attached: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_attached(attached=attached, timeout=timeout)
- async def not_to_be_checked(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_checked(timeout)
- async def to_be_disabled(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.be.disabled",
- FrameExpectOptions(timeout=timeout),
- None,
- "Locator expected to be disabled",
- 'Expect "to_be_disabled"',
- )
- async def not_to_be_disabled(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_disabled(timeout)
- async def to_be_editable(
- self,
- editable: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- if editable is None:
- editable = True
- editable_string = "editable" if editable else "readonly"
- await self._expect_impl(
- "to.be.editable" if editable else "to.be.readonly",
- FrameExpectOptions(timeout=timeout),
- None,
- f"Locator expected to be {editable_string}",
- 'Expect "to_be_editable"',
- )
- async def not_to_be_editable(
- self,
- editable: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_editable(editable, timeout)
- async def to_be_empty(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.be.empty",
- FrameExpectOptions(timeout=timeout),
- None,
- "Locator expected to be empty",
- 'Expect "to_be_empty"',
- )
- async def not_to_be_empty(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_empty(timeout)
- async def to_be_enabled(
- self,
- enabled: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- if enabled is None:
- enabled = True
- enabled_string = "enabled" if enabled else "disabled"
- await self._expect_impl(
- "to.be.enabled" if enabled else "to.be.disabled",
- FrameExpectOptions(timeout=timeout),
- None,
- f"Locator expected to be {enabled_string}",
- 'Expect "to_be_enabled"',
- )
- async def not_to_be_enabled(
- self,
- enabled: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_enabled(enabled, timeout)
- async def to_be_hidden(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.be.hidden",
- FrameExpectOptions(timeout=timeout),
- None,
- "Locator expected to be hidden",
- 'Expect "to_be_hidden"',
- )
- async def not_to_be_hidden(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_hidden(timeout)
- async def to_be_visible(
- self,
- visible: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- if visible is None:
- visible = True
- visible_string = "visible" if visible else "hidden"
- await self._expect_impl(
- "to.be.visible" if visible else "to.be.hidden",
- FrameExpectOptions(timeout=timeout),
- None,
- f"Locator expected to be {visible_string}",
- 'Expect "to_be_visible"',
- )
- async def not_to_be_visible(
- self,
- visible: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_visible(visible, timeout)
- async def to_be_focused(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.be.focused",
- FrameExpectOptions(timeout=timeout),
- None,
- "Locator expected to be focused",
- 'Expect "to_be_focused"',
- )
- async def not_to_be_focused(
- self,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_focused(timeout)
- async def to_be_in_viewport(
- self,
- ratio: float = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.be.in.viewport",
- FrameExpectOptions(timeout=timeout, expectedNumber=ratio),
- None,
- "Locator expected to be in viewport",
- 'Expect "to_be_in_viewport"',
- )
- async def not_to_be_in_viewport(
- self, ratio: float = None, timeout: float = None
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_be_in_viewport(ratio=ratio, timeout=timeout)
- async def to_have_accessible_description(
- self,
- description: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- expected_values = to_expected_text_values(
- [description], ignoreCase=ignoreCase, normalize_white_space=True
- )
- await self._expect_impl(
- "to.have.accessible.description",
- FrameExpectOptions(expectedText=expected_values, timeout=timeout),
- None,
- "Locator expected to have accessible description",
- 'Expect "to_have_accessible_description"',
- )
- async def not_to_have_accessible_description(
- self,
- name: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_accessible_description(name, ignoreCase, timeout)
- async def to_have_accessible_name(
- self,
- name: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- expected_values = to_expected_text_values(
- [name], ignoreCase=ignoreCase, normalize_white_space=True
- )
- await self._expect_impl(
- "to.have.accessible.name",
- FrameExpectOptions(expectedText=expected_values, timeout=timeout),
- None,
- "Locator expected to have accessible name",
- 'Expect "to_have_accessible_name"',
- )
- async def not_to_have_accessible_name(
- self,
- name: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_accessible_name(name, ignoreCase, timeout)
- async def to_have_role(self, role: AriaRole, timeout: float = None) -> None:
- __tracebackhide__ = True
- if isinstance(role, Pattern):
- raise Error('"role" argument in to_have_role must be a string')
- expected_values = to_expected_text_values([role])
- await self._expect_impl(
- "to.have.role",
- FrameExpectOptions(expectedText=expected_values, timeout=timeout),
- None,
- "Locator expected to have accessible role",
- 'Expect "to_have_role"',
- )
- async def to_have_accessible_error_message(
- self,
- errorMessage: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- expected_values = to_expected_text_values(
- [errorMessage], ignoreCase=ignoreCase, normalize_white_space=True
- )
- await self._expect_impl(
- "to.have.accessible.error.message",
- FrameExpectOptions(expectedText=expected_values, timeout=timeout),
- None,
- "Locator expected to have accessible error message",
- 'Expect "to_have_accessible_error_message"',
- )
- async def not_to_have_accessible_error_message(
- self,
- errorMessage: Union[str, Pattern[str]],
- ignoreCase: bool = None,
- timeout: float = None,
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_have_accessible_error_message(
- errorMessage=errorMessage, ignoreCase=ignoreCase, timeout=timeout
- )
- async def not_to_have_role(self, role: AriaRole, timeout: float = None) -> None:
- __tracebackhide__ = True
- await self._not.to_have_role(role, timeout)
- async def to_match_aria_snapshot(
- self, expected: str, timeout: float = None
- ) -> None:
- __tracebackhide__ = True
- await self._expect_impl(
- "to.match.aria",
- FrameExpectOptions(expectedValue=expected, timeout=timeout),
- expected,
- "Locator expected to match Aria snapshot",
- 'Expect "to_match_aria_snapshot"',
- )
- async def not_to_match_aria_snapshot(
- self, expected: str, timeout: float = None
- ) -> None:
- __tracebackhide__ = True
- await self._not.to_match_aria_snapshot(expected, timeout)
- class APIResponseAssertions:
- def __init__(
- self,
- response: APIResponse,
- timeout: float = None,
- is_not: bool = False,
- message: Optional[str] = None,
- is_soft: bool = False,
- ) -> None:
- self._loop = response._loop
- self._dispatcher_fiber = response._dispatcher_fiber
- self._timeout = timeout
- self._is_not = is_not
- self._actual = response
- self._custom_message = message
- self._is_soft = is_soft
- @property
- def _not(self) -> "APIResponseAssertions":
- return APIResponseAssertions(
- self._actual,
- self._timeout,
- not self._is_not,
- self._custom_message,
- self._is_soft,
- )
- async def to_be_ok(
- self,
- ) -> None:
- __tracebackhide__ = True
- if self._is_not is not self._actual.ok:
- return
- message = f"Response status expected to be within [200..299] range, was '{self._actual.status}'"
- if self._is_not:
- message = message.replace("expected to", "expected not to")
- out_message = self._custom_message or message
- out_message += format_call_log(await self._actual._fetch_log())
- content_type = self._actual.headers.get("content-type")
- is_text_encoding = content_type and is_textual_mime_type(content_type)
- text = await self._actual.text() if is_text_encoding else None
- if text is not None:
- out_message += f"\n Response Text:\n{text[:1000]}"
- _record_soft_or_raise(AssertionError(out_message), self._is_soft)
- async def not_to_be_ok(self) -> None:
- __tracebackhide__ = True
- await self._not.to_be_ok()
- def expected_regex(
- pattern: Pattern[str],
- match_substring: bool,
- normalize_white_space: bool,
- ignoreCase: Optional[bool] = None,
- ) -> ExpectedTextValue:
- expected = ExpectedTextValue(
- regexSource=pattern.pattern,
- regexFlags=escape_regex_flags(pattern),
- matchSubstring=match_substring,
- normalizeWhiteSpace=normalize_white_space,
- ignoreCase=ignoreCase,
- )
- if expected["ignoreCase"] is None:
- del expected["ignoreCase"]
- return expected
- def to_expected_text_values(
- items: Union[
- Sequence[Pattern[str]], Sequence[str], Sequence[Union[str, Pattern[str]]]
- ],
- match_substring: bool = False,
- normalize_white_space: bool = False,
- ignoreCase: Optional[bool] = None,
- ) -> Sequence[ExpectedTextValue]:
- out: List[ExpectedTextValue] = []
- assert isinstance(items, (list, tuple))
- for item in items:
- if isinstance(item, str):
- o = ExpectedTextValue(
- string=item,
- matchSubstring=match_substring,
- normalizeWhiteSpace=normalize_white_space,
- ignoreCase=ignoreCase,
- )
- if o["ignoreCase"] is None:
- del o["ignoreCase"]
- out.append(o)
- elif isinstance(item, Pattern):
- out.append(
- expected_regex(item, match_substring, normalize_white_space, ignoreCase)
- )
- else:
- raise Error("value must be a string or regular expression")
- return out
|