_assertions.py 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. # Copyright (c) Microsoft Corporation.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import collections.abc
  15. from typing import Any, List, Optional, Pattern, Sequence, Union
  16. from urllib.parse import urljoin
  17. from playwright._impl._api_structures import (
  18. AriaRole,
  19. ExpectedTextValue,
  20. FrameExpectOptions,
  21. FrameExpectResult,
  22. )
  23. from playwright._impl._connection import format_call_log
  24. from playwright._impl._errors import Error
  25. from playwright._impl._fetch import APIResponse
  26. from playwright._impl._helper import is_textual_mime_type
  27. from playwright._impl._locator import Locator
  28. from playwright._impl._page import Page
  29. from playwright._impl._str_utils import escape_regex_flags
  30. class AssertionsBase:
  31. def __init__(
  32. self,
  33. locator: Locator,
  34. timeout: float = None,
  35. is_not: bool = False,
  36. message: Optional[str] = None,
  37. ) -> None:
  38. self._actual_locator = locator
  39. self._loop = locator._loop
  40. self._dispatcher_fiber = locator._dispatcher_fiber
  41. self._timeout = timeout
  42. self._is_not = is_not
  43. self._custom_message = message
  44. async def _call_expect(
  45. self, expression: str, expect_options: FrameExpectOptions, title: Optional[str]
  46. ) -> FrameExpectResult:
  47. raise NotImplementedError(
  48. "_call_expect must be implemented in a derived class."
  49. )
  50. async def _expect_impl(
  51. self,
  52. expression: str,
  53. expect_options: FrameExpectOptions,
  54. expected: Any,
  55. message: str,
  56. title: str = None,
  57. ) -> None:
  58. __tracebackhide__ = True
  59. expect_options["isNot"] = self._is_not
  60. if expect_options.get("timeout") is None:
  61. expect_options["timeout"] = self._timeout or 5_000
  62. if expect_options["isNot"]:
  63. message = message.replace("expected to", "expected not to")
  64. if "useInnerText" in expect_options and expect_options["useInnerText"] is None:
  65. del expect_options["useInnerText"]
  66. result = await self._call_expect(expression, expect_options, title)
  67. if result["matches"] == self._is_not:
  68. actual = result.get("received")
  69. if self._custom_message:
  70. out_message = self._custom_message
  71. if expected is not None:
  72. out_message += f"\nExpected value: '{expected or '<None>'}'"
  73. else:
  74. out_message = (
  75. f"{message} '{expected}'" if expected is not None else f"{message}"
  76. )
  77. error_message = result.get("errorMessage")
  78. error_message = f"\n{error_message}" if error_message else ""
  79. raise AssertionError(
  80. f"{out_message}\nActual value: {actual}{error_message} {format_call_log(result.get('log'))}"
  81. )
  82. class PageAssertions(AssertionsBase):
  83. def __init__(
  84. self,
  85. page: Page,
  86. timeout: float = None,
  87. is_not: bool = False,
  88. message: Optional[str] = None,
  89. ) -> None:
  90. super().__init__(page.locator(":root"), timeout, is_not, message)
  91. self._actual_page = page
  92. async def _call_expect(
  93. self, expression: str, expect_options: FrameExpectOptions, title: Optional[str]
  94. ) -> FrameExpectResult:
  95. __tracebackhide__ = True
  96. return await self._actual_page.main_frame._expect(
  97. None, expression, expect_options, title
  98. )
  99. @property
  100. def _not(self) -> "PageAssertions":
  101. return PageAssertions(
  102. self._actual_page, self._timeout, not self._is_not, self._custom_message
  103. )
  104. async def to_have_title(
  105. self, titleOrRegExp: Union[Pattern[str], str], timeout: float = None
  106. ) -> None:
  107. __tracebackhide__ = True
  108. expected_values = to_expected_text_values(
  109. [titleOrRegExp], normalize_white_space=True
  110. )
  111. await self._expect_impl(
  112. "to.have.title",
  113. FrameExpectOptions(expectedText=expected_values, timeout=timeout),
  114. titleOrRegExp,
  115. "Page title expected to be",
  116. 'Expect "to_have_title"',
  117. )
  118. async def not_to_have_title(
  119. self, titleOrRegExp: Union[Pattern[str], str], timeout: float = None
  120. ) -> None:
  121. __tracebackhide__ = True
  122. await self._not.to_have_title(titleOrRegExp, timeout)
  123. async def to_have_url(
  124. self,
  125. urlOrRegExp: Union[str, Pattern[str]],
  126. timeout: float = None,
  127. ignoreCase: bool = None,
  128. ) -> None:
  129. __tracebackhide__ = True
  130. base_url = self._actual_page.context._base_url
  131. if isinstance(urlOrRegExp, str) and base_url:
  132. urlOrRegExp = urljoin(base_url, urlOrRegExp)
  133. expected_text = to_expected_text_values([urlOrRegExp], ignoreCase=ignoreCase)
  134. await self._expect_impl(
  135. "to.have.url",
  136. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  137. urlOrRegExp,
  138. "Page URL expected to be",
  139. 'Expect "to_have_url"',
  140. )
  141. async def not_to_have_url(
  142. self,
  143. urlOrRegExp: Union[Pattern[str], str],
  144. timeout: float = None,
  145. ignoreCase: bool = None,
  146. ) -> None:
  147. __tracebackhide__ = True
  148. await self._not.to_have_url(urlOrRegExp, timeout, ignoreCase)
  149. class LocatorAssertions(AssertionsBase):
  150. def __init__(
  151. self,
  152. locator: Locator,
  153. timeout: float = None,
  154. is_not: bool = False,
  155. message: Optional[str] = None,
  156. ) -> None:
  157. super().__init__(locator, timeout, is_not, message)
  158. self._actual_locator = locator
  159. async def _call_expect(
  160. self, expression: str, expect_options: FrameExpectOptions, title: Optional[str]
  161. ) -> FrameExpectResult:
  162. __tracebackhide__ = True
  163. return await self._actual_locator._expect(expression, expect_options, title)
  164. @property
  165. def _not(self) -> "LocatorAssertions":
  166. return LocatorAssertions(
  167. self._actual_locator, self._timeout, not self._is_not, self._custom_message
  168. )
  169. async def to_contain_text(
  170. self,
  171. expected: Union[
  172. Sequence[str],
  173. Sequence[Pattern[str]],
  174. Sequence[Union[Pattern[str], str]],
  175. Pattern[str],
  176. str,
  177. ],
  178. useInnerText: bool = None,
  179. timeout: float = None,
  180. ignoreCase: bool = None,
  181. ) -> None:
  182. __tracebackhide__ = True
  183. if isinstance(expected, collections.abc.Sequence) and not isinstance(
  184. expected, str
  185. ):
  186. expected_text = to_expected_text_values(
  187. expected,
  188. match_substring=True,
  189. normalize_white_space=True,
  190. ignoreCase=ignoreCase,
  191. )
  192. await self._expect_impl(
  193. "to.contain.text.array",
  194. FrameExpectOptions(
  195. expectedText=expected_text,
  196. useInnerText=useInnerText,
  197. timeout=timeout,
  198. ),
  199. expected,
  200. "Locator expected to contain text",
  201. 'Expect "to_contain_text"',
  202. )
  203. else:
  204. expected_text = to_expected_text_values(
  205. [expected],
  206. match_substring=True,
  207. normalize_white_space=True,
  208. ignoreCase=ignoreCase,
  209. )
  210. await self._expect_impl(
  211. "to.have.text",
  212. FrameExpectOptions(
  213. expectedText=expected_text,
  214. useInnerText=useInnerText,
  215. timeout=timeout,
  216. ),
  217. expected,
  218. "Locator expected to contain text",
  219. 'Expect "to_contain_text"',
  220. )
  221. async def not_to_contain_text(
  222. self,
  223. expected: Union[
  224. Sequence[str],
  225. Sequence[Pattern[str]],
  226. Sequence[Union[Pattern[str], str]],
  227. Pattern[str],
  228. str,
  229. ],
  230. useInnerText: bool = None,
  231. timeout: float = None,
  232. ignoreCase: bool = None,
  233. ) -> None:
  234. __tracebackhide__ = True
  235. await self._not.to_contain_text(expected, useInnerText, timeout, ignoreCase)
  236. async def to_have_attribute(
  237. self,
  238. name: str,
  239. value: Union[str, Pattern[str]],
  240. ignoreCase: bool = None,
  241. timeout: float = None,
  242. ) -> None:
  243. __tracebackhide__ = True
  244. expected_text = to_expected_text_values([value], ignoreCase=ignoreCase)
  245. await self._expect_impl(
  246. "to.have.attribute.value",
  247. FrameExpectOptions(
  248. expressionArg=name, expectedText=expected_text, timeout=timeout
  249. ),
  250. value,
  251. "Locator expected to have attribute",
  252. 'Expect "to_have_attribute"',
  253. )
  254. async def not_to_have_attribute(
  255. self,
  256. name: str,
  257. value: Union[str, Pattern[str]],
  258. ignoreCase: bool = None,
  259. timeout: float = None,
  260. ) -> None:
  261. __tracebackhide__ = True
  262. await self._not.to_have_attribute(
  263. name, value, ignoreCase=ignoreCase, timeout=timeout
  264. )
  265. async def to_have_class(
  266. self,
  267. expected: Union[
  268. Sequence[str],
  269. Sequence[Pattern[str]],
  270. Sequence[Union[Pattern[str], str]],
  271. Pattern[str],
  272. str,
  273. ],
  274. timeout: float = None,
  275. ) -> None:
  276. __tracebackhide__ = True
  277. if isinstance(expected, collections.abc.Sequence) and not isinstance(
  278. expected, str
  279. ):
  280. expected_text = to_expected_text_values(expected)
  281. await self._expect_impl(
  282. "to.have.class.array",
  283. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  284. expected,
  285. "Locator expected to have class",
  286. 'Expect "to_have_class"',
  287. )
  288. else:
  289. expected_text = to_expected_text_values([expected])
  290. await self._expect_impl(
  291. "to.have.class",
  292. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  293. expected,
  294. "Locator expected to have class",
  295. 'Expect "to_have_class"',
  296. )
  297. async def not_to_have_class(
  298. self,
  299. expected: Union[
  300. Sequence[str],
  301. Sequence[Pattern[str]],
  302. Sequence[Union[Pattern[str], str]],
  303. Pattern[str],
  304. str,
  305. ],
  306. timeout: float = None,
  307. ) -> None:
  308. __tracebackhide__ = True
  309. await self._not.to_have_class(expected, timeout)
  310. async def to_contain_class(
  311. self,
  312. expected: Union[
  313. Sequence[str],
  314. str,
  315. ],
  316. timeout: float = None,
  317. ) -> None:
  318. __tracebackhide__ = True
  319. if isinstance(expected, collections.abc.Sequence) and not isinstance(
  320. expected, str
  321. ):
  322. expected_text = to_expected_text_values(expected)
  323. await self._expect_impl(
  324. "to.contain.class.array",
  325. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  326. expected,
  327. "Locator expected to contain class names",
  328. 'Expect "to_contain_class"',
  329. )
  330. else:
  331. expected_text = to_expected_text_values([expected])
  332. await self._expect_impl(
  333. "to.contain.class",
  334. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  335. expected,
  336. "Locator expected to contain class",
  337. 'Expect "to_contain_class"',
  338. )
  339. async def not_to_contain_class(
  340. self,
  341. expected: Union[
  342. Sequence[str],
  343. str,
  344. ],
  345. timeout: float = None,
  346. ) -> None:
  347. __tracebackhide__ = True
  348. await self._not.to_contain_class(expected, timeout)
  349. async def to_have_count(
  350. self,
  351. count: int,
  352. timeout: float = None,
  353. ) -> None:
  354. __tracebackhide__ = True
  355. await self._expect_impl(
  356. "to.have.count",
  357. FrameExpectOptions(expectedNumber=count, timeout=timeout),
  358. count,
  359. "Locator expected to have count",
  360. 'Expect "to_have_count"',
  361. )
  362. async def not_to_have_count(
  363. self,
  364. count: int,
  365. timeout: float = None,
  366. ) -> None:
  367. __tracebackhide__ = True
  368. await self._not.to_have_count(count, timeout)
  369. async def to_have_css(
  370. self,
  371. name: str,
  372. value: Union[str, Pattern[str]],
  373. timeout: float = None,
  374. ) -> None:
  375. __tracebackhide__ = True
  376. expected_text = to_expected_text_values([value])
  377. await self._expect_impl(
  378. "to.have.css",
  379. FrameExpectOptions(
  380. expressionArg=name, expectedText=expected_text, timeout=timeout
  381. ),
  382. value,
  383. "Locator expected to have CSS",
  384. 'Expect "to_have_css"',
  385. )
  386. async def not_to_have_css(
  387. self,
  388. name: str,
  389. value: Union[str, Pattern[str]],
  390. timeout: float = None,
  391. ) -> None:
  392. __tracebackhide__ = True
  393. await self._not.to_have_css(name, value, timeout)
  394. async def to_have_id(
  395. self,
  396. id: Union[str, Pattern[str]],
  397. timeout: float = None,
  398. ) -> None:
  399. __tracebackhide__ = True
  400. expected_text = to_expected_text_values([id])
  401. await self._expect_impl(
  402. "to.have.id",
  403. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  404. id,
  405. "Locator expected to have ID",
  406. 'Expect "to_have_id"',
  407. )
  408. async def not_to_have_id(
  409. self,
  410. id: Union[str, Pattern[str]],
  411. timeout: float = None,
  412. ) -> None:
  413. __tracebackhide__ = True
  414. await self._not.to_have_id(id, timeout)
  415. async def to_have_js_property(
  416. self,
  417. name: str,
  418. value: Any,
  419. timeout: float = None,
  420. ) -> None:
  421. __tracebackhide__ = True
  422. await self._expect_impl(
  423. "to.have.property",
  424. FrameExpectOptions(
  425. expressionArg=name, expectedValue=value, timeout=timeout
  426. ),
  427. value,
  428. "Locator expected to have JS Property",
  429. 'Expect "to_have_property"',
  430. )
  431. async def not_to_have_js_property(
  432. self,
  433. name: str,
  434. value: Any,
  435. timeout: float = None,
  436. ) -> None:
  437. __tracebackhide__ = True
  438. await self._not.to_have_js_property(name, value, timeout)
  439. async def to_have_value(
  440. self,
  441. value: Union[str, Pattern[str]],
  442. timeout: float = None,
  443. ) -> None:
  444. __tracebackhide__ = True
  445. expected_text = to_expected_text_values([value])
  446. await self._expect_impl(
  447. "to.have.value",
  448. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  449. value,
  450. "Locator expected to have Value",
  451. 'Expect "to_have_value"',
  452. )
  453. async def not_to_have_value(
  454. self,
  455. value: Union[str, Pattern[str]],
  456. timeout: float = None,
  457. ) -> None:
  458. __tracebackhide__ = True
  459. await self._not.to_have_value(value, timeout)
  460. async def to_have_values(
  461. self,
  462. values: Union[
  463. Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
  464. ],
  465. timeout: float = None,
  466. ) -> None:
  467. __tracebackhide__ = True
  468. expected_text = to_expected_text_values(values)
  469. await self._expect_impl(
  470. "to.have.values",
  471. FrameExpectOptions(expectedText=expected_text, timeout=timeout),
  472. values,
  473. "Locator expected to have Values",
  474. 'Expect "to_have_values"',
  475. )
  476. async def not_to_have_values(
  477. self,
  478. values: Union[
  479. Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
  480. ],
  481. timeout: float = None,
  482. ) -> None:
  483. __tracebackhide__ = True
  484. await self._not.to_have_values(values, timeout)
  485. async def to_have_text(
  486. self,
  487. expected: Union[
  488. Sequence[str],
  489. Sequence[Pattern[str]],
  490. Sequence[Union[Pattern[str], str]],
  491. Pattern[str],
  492. str,
  493. ],
  494. useInnerText: bool = None,
  495. timeout: float = None,
  496. ignoreCase: bool = None,
  497. ) -> None:
  498. __tracebackhide__ = True
  499. if isinstance(expected, collections.abc.Sequence) and not isinstance(
  500. expected, str
  501. ):
  502. expected_text = to_expected_text_values(
  503. expected,
  504. normalize_white_space=True,
  505. ignoreCase=ignoreCase,
  506. )
  507. await self._expect_impl(
  508. "to.have.text.array",
  509. FrameExpectOptions(
  510. expectedText=expected_text,
  511. useInnerText=useInnerText,
  512. timeout=timeout,
  513. ),
  514. expected,
  515. "Locator expected to have text",
  516. 'Expect "to_have_text"',
  517. )
  518. else:
  519. expected_text = to_expected_text_values(
  520. [expected], normalize_white_space=True, ignoreCase=ignoreCase
  521. )
  522. await self._expect_impl(
  523. "to.have.text",
  524. FrameExpectOptions(
  525. expectedText=expected_text,
  526. useInnerText=useInnerText,
  527. timeout=timeout,
  528. ),
  529. expected,
  530. "Locator expected to have text",
  531. 'Expect "to_have_text"',
  532. )
  533. async def not_to_have_text(
  534. self,
  535. expected: Union[
  536. Sequence[str],
  537. Sequence[Pattern[str]],
  538. Sequence[Union[Pattern[str], str]],
  539. Pattern[str],
  540. str,
  541. ],
  542. useInnerText: bool = None,
  543. timeout: float = None,
  544. ignoreCase: bool = None,
  545. ) -> None:
  546. __tracebackhide__ = True
  547. await self._not.to_have_text(expected, useInnerText, timeout, ignoreCase)
  548. async def to_be_attached(
  549. self,
  550. attached: bool = None,
  551. timeout: float = None,
  552. ) -> None:
  553. __tracebackhide__ = True
  554. if attached is None:
  555. attached = True
  556. attached_string = "attached" if attached else "detached"
  557. await self._expect_impl(
  558. ("to.be.attached" if attached else "to.be.detached"),
  559. FrameExpectOptions(timeout=timeout),
  560. None,
  561. f"Locator expected to be {attached_string}",
  562. 'Expect "to_be_attached"',
  563. )
  564. async def to_be_checked(
  565. self,
  566. timeout: float = None,
  567. checked: bool = None,
  568. indeterminate: bool = None,
  569. ) -> None:
  570. __tracebackhide__ = True
  571. expected_value = {}
  572. if indeterminate is not None:
  573. expected_value["indeterminate"] = indeterminate
  574. if checked is not None:
  575. expected_value["checked"] = checked
  576. checked_string: str
  577. if indeterminate:
  578. checked_string = "indeterminate"
  579. else:
  580. checked_string = "unchecked" if checked is False else "checked"
  581. await self._expect_impl(
  582. "to.be.checked",
  583. FrameExpectOptions(timeout=timeout, expectedValue=expected_value),
  584. None,
  585. f"Locator expected to be {checked_string}",
  586. 'Expect "to_be_checked"',
  587. )
  588. async def not_to_be_attached(
  589. self,
  590. attached: bool = None,
  591. timeout: float = None,
  592. ) -> None:
  593. __tracebackhide__ = True
  594. await self._not.to_be_attached(attached=attached, timeout=timeout)
  595. async def not_to_be_checked(
  596. self,
  597. timeout: float = None,
  598. ) -> None:
  599. __tracebackhide__ = True
  600. await self._not.to_be_checked(timeout)
  601. async def to_be_disabled(
  602. self,
  603. timeout: float = None,
  604. ) -> None:
  605. __tracebackhide__ = True
  606. await self._expect_impl(
  607. "to.be.disabled",
  608. FrameExpectOptions(timeout=timeout),
  609. None,
  610. "Locator expected to be disabled",
  611. 'Expect "to_be_disabled"',
  612. )
  613. async def not_to_be_disabled(
  614. self,
  615. timeout: float = None,
  616. ) -> None:
  617. __tracebackhide__ = True
  618. await self._not.to_be_disabled(timeout)
  619. async def to_be_editable(
  620. self,
  621. editable: bool = None,
  622. timeout: float = None,
  623. ) -> None:
  624. __tracebackhide__ = True
  625. if editable is None:
  626. editable = True
  627. editable_string = "editable" if editable else "readonly"
  628. await self._expect_impl(
  629. "to.be.editable" if editable else "to.be.readonly",
  630. FrameExpectOptions(timeout=timeout),
  631. None,
  632. f"Locator expected to be {editable_string}",
  633. 'Expect "to_be_editable"',
  634. )
  635. async def not_to_be_editable(
  636. self,
  637. editable: bool = None,
  638. timeout: float = None,
  639. ) -> None:
  640. __tracebackhide__ = True
  641. await self._not.to_be_editable(editable, timeout)
  642. async def to_be_empty(
  643. self,
  644. timeout: float = None,
  645. ) -> None:
  646. __tracebackhide__ = True
  647. await self._expect_impl(
  648. "to.be.empty",
  649. FrameExpectOptions(timeout=timeout),
  650. None,
  651. "Locator expected to be empty",
  652. 'Expect "to_be_empty"',
  653. )
  654. async def not_to_be_empty(
  655. self,
  656. timeout: float = None,
  657. ) -> None:
  658. __tracebackhide__ = True
  659. await self._not.to_be_empty(timeout)
  660. async def to_be_enabled(
  661. self,
  662. enabled: bool = None,
  663. timeout: float = None,
  664. ) -> None:
  665. __tracebackhide__ = True
  666. if enabled is None:
  667. enabled = True
  668. enabled_string = "enabled" if enabled else "disabled"
  669. await self._expect_impl(
  670. "to.be.enabled" if enabled else "to.be.disabled",
  671. FrameExpectOptions(timeout=timeout),
  672. None,
  673. f"Locator expected to be {enabled_string}",
  674. 'Expect "to_be_enabled"',
  675. )
  676. async def not_to_be_enabled(
  677. self,
  678. enabled: bool = None,
  679. timeout: float = None,
  680. ) -> None:
  681. __tracebackhide__ = True
  682. await self._not.to_be_enabled(enabled, timeout)
  683. async def to_be_hidden(
  684. self,
  685. timeout: float = None,
  686. ) -> None:
  687. __tracebackhide__ = True
  688. await self._expect_impl(
  689. "to.be.hidden",
  690. FrameExpectOptions(timeout=timeout),
  691. None,
  692. "Locator expected to be hidden",
  693. 'Expect "to_be_hidden"',
  694. )
  695. async def not_to_be_hidden(
  696. self,
  697. timeout: float = None,
  698. ) -> None:
  699. __tracebackhide__ = True
  700. await self._not.to_be_hidden(timeout)
  701. async def to_be_visible(
  702. self,
  703. visible: bool = None,
  704. timeout: float = None,
  705. ) -> None:
  706. __tracebackhide__ = True
  707. if visible is None:
  708. visible = True
  709. visible_string = "visible" if visible else "hidden"
  710. await self._expect_impl(
  711. "to.be.visible" if visible else "to.be.hidden",
  712. FrameExpectOptions(timeout=timeout),
  713. None,
  714. f"Locator expected to be {visible_string}",
  715. 'Expect "to_be_visible"',
  716. )
  717. async def not_to_be_visible(
  718. self,
  719. visible: bool = None,
  720. timeout: float = None,
  721. ) -> None:
  722. __tracebackhide__ = True
  723. await self._not.to_be_visible(visible, timeout)
  724. async def to_be_focused(
  725. self,
  726. timeout: float = None,
  727. ) -> None:
  728. __tracebackhide__ = True
  729. await self._expect_impl(
  730. "to.be.focused",
  731. FrameExpectOptions(timeout=timeout),
  732. None,
  733. "Locator expected to be focused",
  734. 'Expect "to_be_focused"',
  735. )
  736. async def not_to_be_focused(
  737. self,
  738. timeout: float = None,
  739. ) -> None:
  740. __tracebackhide__ = True
  741. await self._not.to_be_focused(timeout)
  742. async def to_be_in_viewport(
  743. self,
  744. ratio: float = None,
  745. timeout: float = None,
  746. ) -> None:
  747. __tracebackhide__ = True
  748. await self._expect_impl(
  749. "to.be.in.viewport",
  750. FrameExpectOptions(timeout=timeout, expectedNumber=ratio),
  751. None,
  752. "Locator expected to be in viewport",
  753. 'Expect "to_be_in_viewport"',
  754. )
  755. async def not_to_be_in_viewport(
  756. self, ratio: float = None, timeout: float = None
  757. ) -> None:
  758. __tracebackhide__ = True
  759. await self._not.to_be_in_viewport(ratio=ratio, timeout=timeout)
  760. async def to_have_accessible_description(
  761. self,
  762. description: Union[str, Pattern[str]],
  763. ignoreCase: bool = None,
  764. timeout: float = None,
  765. ) -> None:
  766. __tracebackhide__ = True
  767. expected_values = to_expected_text_values(
  768. [description], ignoreCase=ignoreCase, normalize_white_space=True
  769. )
  770. await self._expect_impl(
  771. "to.have.accessible.description",
  772. FrameExpectOptions(expectedText=expected_values, timeout=timeout),
  773. None,
  774. "Locator expected to have accessible description",
  775. 'Expect "to_have_accessible_description"',
  776. )
  777. async def not_to_have_accessible_description(
  778. self,
  779. name: Union[str, Pattern[str]],
  780. ignoreCase: bool = None,
  781. timeout: float = None,
  782. ) -> None:
  783. __tracebackhide__ = True
  784. await self._not.to_have_accessible_description(name, ignoreCase, timeout)
  785. async def to_have_accessible_name(
  786. self,
  787. name: Union[str, Pattern[str]],
  788. ignoreCase: bool = None,
  789. timeout: float = None,
  790. ) -> None:
  791. __tracebackhide__ = True
  792. expected_values = to_expected_text_values(
  793. [name], ignoreCase=ignoreCase, normalize_white_space=True
  794. )
  795. await self._expect_impl(
  796. "to.have.accessible.name",
  797. FrameExpectOptions(expectedText=expected_values, timeout=timeout),
  798. None,
  799. "Locator expected to have accessible name",
  800. 'Expect "to_have_accessible_name"',
  801. )
  802. async def not_to_have_accessible_name(
  803. self,
  804. name: Union[str, Pattern[str]],
  805. ignoreCase: bool = None,
  806. timeout: float = None,
  807. ) -> None:
  808. __tracebackhide__ = True
  809. await self._not.to_have_accessible_name(name, ignoreCase, timeout)
  810. async def to_have_role(self, role: AriaRole, timeout: float = None) -> None:
  811. __tracebackhide__ = True
  812. if isinstance(role, Pattern):
  813. raise Error('"role" argument in to_have_role must be a string')
  814. expected_values = to_expected_text_values([role])
  815. await self._expect_impl(
  816. "to.have.role",
  817. FrameExpectOptions(expectedText=expected_values, timeout=timeout),
  818. None,
  819. "Locator expected to have accessible role",
  820. 'Expect "to_have_role"',
  821. )
  822. async def to_have_accessible_error_message(
  823. self,
  824. errorMessage: Union[str, Pattern[str]],
  825. ignoreCase: bool = None,
  826. timeout: float = None,
  827. ) -> None:
  828. __tracebackhide__ = True
  829. expected_values = to_expected_text_values(
  830. [errorMessage], ignoreCase=ignoreCase, normalize_white_space=True
  831. )
  832. await self._expect_impl(
  833. "to.have.accessible.error.message",
  834. FrameExpectOptions(expectedText=expected_values, timeout=timeout),
  835. None,
  836. "Locator expected to have accessible error message",
  837. 'Expect "to_have_accessible_error_message"',
  838. )
  839. async def not_to_have_accessible_error_message(
  840. self,
  841. errorMessage: Union[str, Pattern[str]],
  842. ignoreCase: bool = None,
  843. timeout: float = None,
  844. ) -> None:
  845. __tracebackhide__ = True
  846. await self._not.to_have_accessible_error_message(
  847. errorMessage=errorMessage, ignoreCase=ignoreCase, timeout=timeout
  848. )
  849. async def not_to_have_role(self, role: AriaRole, timeout: float = None) -> None:
  850. __tracebackhide__ = True
  851. await self._not.to_have_role(role, timeout)
  852. async def to_match_aria_snapshot(
  853. self, expected: str, timeout: float = None
  854. ) -> None:
  855. __tracebackhide__ = True
  856. await self._expect_impl(
  857. "to.match.aria",
  858. FrameExpectOptions(expectedValue=expected, timeout=timeout),
  859. expected,
  860. "Locator expected to match Aria snapshot",
  861. 'Expect "to_match_aria_snapshot"',
  862. )
  863. async def not_to_match_aria_snapshot(
  864. self, expected: str, timeout: float = None
  865. ) -> None:
  866. __tracebackhide__ = True
  867. await self._not.to_match_aria_snapshot(expected, timeout)
  868. class APIResponseAssertions:
  869. def __init__(
  870. self,
  871. response: APIResponse,
  872. timeout: float = None,
  873. is_not: bool = False,
  874. message: Optional[str] = None,
  875. ) -> None:
  876. self._loop = response._loop
  877. self._dispatcher_fiber = response._dispatcher_fiber
  878. self._timeout = timeout
  879. self._is_not = is_not
  880. self._actual = response
  881. self._custom_message = message
  882. @property
  883. def _not(self) -> "APIResponseAssertions":
  884. return APIResponseAssertions(
  885. self._actual, self._timeout, not self._is_not, self._custom_message
  886. )
  887. async def to_be_ok(
  888. self,
  889. ) -> None:
  890. __tracebackhide__ = True
  891. if self._is_not is not self._actual.ok:
  892. return
  893. message = f"Response status expected to be within [200..299] range, was '{self._actual.status}'"
  894. if self._is_not:
  895. message = message.replace("expected to", "expected not to")
  896. out_message = self._custom_message or message
  897. out_message += format_call_log(await self._actual._fetch_log())
  898. content_type = self._actual.headers.get("content-type")
  899. is_text_encoding = content_type and is_textual_mime_type(content_type)
  900. text = await self._actual.text() if is_text_encoding else None
  901. if text is not None:
  902. out_message += f"\n Response Text:\n{text[:1000]}"
  903. raise AssertionError(out_message)
  904. async def not_to_be_ok(self) -> None:
  905. __tracebackhide__ = True
  906. await self._not.to_be_ok()
  907. def expected_regex(
  908. pattern: Pattern[str],
  909. match_substring: bool,
  910. normalize_white_space: bool,
  911. ignoreCase: Optional[bool] = None,
  912. ) -> ExpectedTextValue:
  913. expected = ExpectedTextValue(
  914. regexSource=pattern.pattern,
  915. regexFlags=escape_regex_flags(pattern),
  916. matchSubstring=match_substring,
  917. normalizeWhiteSpace=normalize_white_space,
  918. ignoreCase=ignoreCase,
  919. )
  920. if expected["ignoreCase"] is None:
  921. del expected["ignoreCase"]
  922. return expected
  923. def to_expected_text_values(
  924. items: Union[
  925. Sequence[Pattern[str]], Sequence[str], Sequence[Union[str, Pattern[str]]]
  926. ],
  927. match_substring: bool = False,
  928. normalize_white_space: bool = False,
  929. ignoreCase: Optional[bool] = None,
  930. ) -> Sequence[ExpectedTextValue]:
  931. out: List[ExpectedTextValue] = []
  932. assert isinstance(items, (list, tuple))
  933. for item in items:
  934. if isinstance(item, str):
  935. o = ExpectedTextValue(
  936. string=item,
  937. matchSubstring=match_substring,
  938. normalizeWhiteSpace=normalize_white_space,
  939. ignoreCase=ignoreCase,
  940. )
  941. if o["ignoreCase"] is None:
  942. del o["ignoreCase"]
  943. out.append(o)
  944. elif isinstance(item, Pattern):
  945. out.append(
  946. expected_regex(item, match_substring, normalize_white_space, ignoreCase)
  947. )
  948. else:
  949. raise Error("value must be a string or regular expression")
  950. return out