Best Python code snippet using playwright-python
_assertions.py
Source:_assertions.py
...23 self._actual_locator = locator24 self._loop = locator._loop25 self._dispatcher_fiber = locator._dispatcher_fiber26 self._is_not = is_not27 async def _expect_impl(28 self,29 expression: str,30 expect_options: FrameExpectOptions,31 expected: Any,32 message: str,33 ) -> None:34 __tracebackhide__ = True35 expect_options["isNot"] = self._is_not36 if expect_options.get("timeout") is None:37 expect_options["timeout"] = 5_00038 if expect_options["isNot"]:39 message = message.replace("expected to", "expected not to")40 if "useInnerText" in expect_options and expect_options["useInnerText"] is None:41 del expect_options["useInnerText"]42 result = await self._actual_locator._expect(expression, expect_options)43 if result["matches"] == self._is_not:44 actual = result.get("received")45 log = "\n".join(result.get("log", "")).strip()46 if log:47 log = "\nCall log:\n" + log48 if expected is not None:49 raise AssertionError(50 f"{message} '{expected}'\nActual value: {actual} {log}"51 )52 raise AssertionError(f"{message}\nActual value: {actual} {log}")53class PageAssertions(AssertionsBase):54 def __init__(self, page: Page, is_not: bool = False) -> None:55 super().__init__(page.locator(":root"), is_not)56 self._actual_page = page57 @property58 def _not(self) -> "PageAssertions":59 return PageAssertions(self._actual_page, not self._is_not)60 async def to_have_title(61 self, title_or_reg_exp: Union[Pattern, str], timeout: float = None62 ) -> None:63 expected_values = to_expected_text_values(64 [title_or_reg_exp], normalize_white_space=True65 )66 __tracebackhide__ = True67 await self._expect_impl(68 "to.have.title",69 FrameExpectOptions(expectedText=expected_values, timeout=timeout),70 title_or_reg_exp,71 "Page title expected to be",72 )73 async def not_to_have_title(74 self, title_or_reg_exp: Union[Pattern, str], timeout: float = None75 ) -> None:76 __tracebackhide__ = True77 await self._not.to_have_title(title_or_reg_exp, timeout)78 async def to_have_url(79 self, url_or_reg_exp: Union[str, Pattern], timeout: float = None80 ) -> None:81 __tracebackhide__ = True82 base_url = self._actual_page.context._options.get("baseURL")83 if isinstance(url_or_reg_exp, str) and base_url:84 url_or_reg_exp = urljoin(base_url, url_or_reg_exp)85 expected_text = to_expected_text_values([url_or_reg_exp])86 await self._expect_impl(87 "to.have.url",88 FrameExpectOptions(expectedText=expected_text, timeout=timeout),89 url_or_reg_exp,90 "Page URL expected to be",91 )92 async def not_to_have_url(93 self, url_or_reg_exp: Union[Pattern, str], timeout: float = None94 ) -> None:95 __tracebackhide__ = True96 await self._not.to_have_url(url_or_reg_exp, timeout)97class LocatorAssertions(AssertionsBase):98 def __init__(self, locator: Locator, is_not: bool = False) -> None:99 super().__init__(locator, is_not)100 self._actual_locator = locator101 @property102 def _not(self) -> "LocatorAssertions":103 return LocatorAssertions(self._actual_locator, not self._is_not)104 async def to_contain_text(105 self,106 expected: Union[List[Union[Pattern, str]], Pattern, str],107 use_inner_text: bool = None,108 timeout: float = None,109 ) -> None:110 __tracebackhide__ = True111 if isinstance(expected, list):112 expected_text = to_expected_text_values(113 expected, match_substring=True, normalize_white_space=True114 )115 await self._expect_impl(116 "to.contain.text.array",117 FrameExpectOptions(118 expectedText=expected_text,119 useInnerText=use_inner_text,120 timeout=timeout,121 ),122 expected,123 "Locator expected to contain text",124 )125 else:126 expected_text = to_expected_text_values(127 [expected], match_substring=True, normalize_white_space=True128 )129 await self._expect_impl(130 "to.have.text",131 FrameExpectOptions(132 expectedText=expected_text,133 useInnerText=use_inner_text,134 timeout=timeout,135 ),136 expected,137 "Locator expected to contain text",138 )139 async def not_to_contain_text(140 self,141 expected: Union[List[Union[Pattern, str]], Pattern, str],142 use_inner_text: bool = None,143 timeout: float = None,144 ) -> None:145 __tracebackhide__ = True146 await self._not.to_contain_text(expected, use_inner_text, timeout)147 async def to_have_attribute(148 self,149 name: str,150 value: Union[str, Pattern],151 timeout: float = None,152 ) -> None:153 __tracebackhide__ = True154 expected_text = to_expected_text_values([value])155 await self._expect_impl(156 "to.have.attribute",157 FrameExpectOptions(158 expressionArg=name, expectedText=expected_text, timeout=timeout159 ),160 value,161 "Locator expected to have attribute",162 )163 async def not_to_have_attribute(164 self,165 name: str,166 value: Union[str, Pattern],167 timeout: float = None,168 ) -> None:169 __tracebackhide__ = True170 await self._not.to_have_attribute(name, value, timeout)171 async def to_have_class(172 self,173 expected: Union[List[Union[Pattern, str]], Pattern, str],174 timeout: float = None,175 ) -> None:176 __tracebackhide__ = True177 if isinstance(expected, list):178 expected_text = to_expected_text_values(expected)179 await self._expect_impl(180 "to.have.class.array",181 FrameExpectOptions(expectedText=expected_text, timeout=timeout),182 expected,183 "Locator expected to have class",184 )185 else:186 expected_text = to_expected_text_values([expected])187 await self._expect_impl(188 "to.have.class",189 FrameExpectOptions(expectedText=expected_text, timeout=timeout),190 expected,191 "Locator expected to have class",192 )193 async def not_to_have_class(194 self,195 expected: Union[List[Union[Pattern, str]], Pattern, str],196 timeout: float = None,197 ) -> None:198 __tracebackhide__ = True199 await self._not.to_have_class(expected, timeout)200 async def to_have_count(201 self,202 count: int,203 timeout: float = None,204 ) -> None:205 __tracebackhide__ = True206 await self._expect_impl(207 "to.have.count",208 FrameExpectOptions(expectedNumber=count, timeout=timeout),209 count,210 "Locator expected to have count",211 )212 async def not_to_have_count(213 self,214 count: int,215 timeout: float = None,216 ) -> None:217 __tracebackhide__ = True218 await self._not.to_have_count(count, timeout)219 async def to_have_css(220 self,221 name: str,222 value: Union[str, Pattern],223 timeout: float = None,224 ) -> None:225 __tracebackhide__ = True226 expected_text = to_expected_text_values([value])227 await self._expect_impl(228 "to.have.css",229 FrameExpectOptions(230 expressionArg=name, expectedText=expected_text, timeout=timeout231 ),232 value,233 "Locator expected to have CSS",234 )235 async def not_to_have_css(236 self,237 name: str,238 value: Union[str, Pattern],239 timeout: float = None,240 ) -> None:241 __tracebackhide__ = True242 await self._not.to_have_css(name, value, timeout)243 async def to_have_id(244 self,245 id: Union[str, Pattern],246 timeout: float = None,247 ) -> None:248 __tracebackhide__ = True249 expected_text = to_expected_text_values([id])250 await self._expect_impl(251 "to.have.id",252 FrameExpectOptions(expectedText=expected_text, timeout=timeout),253 id,254 "Locator expected to have ID",255 )256 async def not_to_have_id(257 self,258 id: Union[str, Pattern],259 timeout: float = None,260 ) -> None:261 __tracebackhide__ = True262 await self._not.to_have_id(id, timeout)263 async def to_have_js_property(264 self,265 name: str,266 value: Any,267 timeout: float = None,268 ) -> None:269 __tracebackhide__ = True270 await self._expect_impl(271 "to.have.property",272 FrameExpectOptions(273 expressionArg=name, expectedValue=value, timeout=timeout274 ),275 value,276 "Locator expected to have JS Property",277 )278 async def not_to_have_js_property(279 self,280 name: str,281 value: Any,282 timeout: float = None,283 ) -> None:284 __tracebackhide__ = True285 await self._not.to_have_js_property(name, value, timeout)286 async def to_have_value(287 self,288 value: Union[str, Pattern],289 timeout: float = None,290 ) -> None:291 __tracebackhide__ = True292 expected_text = to_expected_text_values([value])293 await self._expect_impl(294 "to.have.value",295 FrameExpectOptions(expectedText=expected_text, timeout=timeout),296 value,297 "Locator expected to have Value",298 )299 async def not_to_have_value(300 self,301 value: Union[str, Pattern],302 timeout: float = None,303 ) -> None:304 __tracebackhide__ = True305 await self._not.to_have_value(value, timeout)306 async def to_have_text(307 self,308 expected: Union[List[Union[Pattern, str]], Pattern, str],309 use_inner_text: bool = None,310 timeout: float = None,311 ) -> None:312 __tracebackhide__ = True313 if isinstance(expected, list):314 expected_text = to_expected_text_values(315 expected, normalize_white_space=True316 )317 await self._expect_impl(318 "to.have.text.array",319 FrameExpectOptions(320 expectedText=expected_text,321 useInnerText=use_inner_text,322 timeout=timeout,323 ),324 expected,325 "Locator expected to have text",326 )327 else:328 expected_text = to_expected_text_values(329 [expected], normalize_white_space=True330 )331 await self._expect_impl(332 "to.have.text",333 FrameExpectOptions(334 expectedText=expected_text,335 useInnerText=use_inner_text,336 timeout=timeout,337 ),338 expected,339 "Locator expected to have text",340 )341 async def not_to_have_text(342 self,343 expected: Union[List[Union[Pattern, str]], Pattern, str],344 use_inner_text: bool = None,345 timeout: float = None,346 ) -> None:347 __tracebackhide__ = True348 await self._not.to_have_text(expected, use_inner_text, timeout)349 async def to_be_checked(350 self,351 timeout: float = None,352 checked: bool = None,353 ) -> None:354 __tracebackhide__ = True355 await self._expect_impl(356 "to.be.checked"357 if checked is None or checked is True358 else "to.be.unchecked",359 FrameExpectOptions(timeout=timeout),360 None,361 "Locator expected to be checked",362 )363 async def not_to_be_checked(364 self,365 timeout: float = None,366 ) -> None:367 __tracebackhide__ = True368 await self._not.to_be_checked(timeout)369 async def to_be_disabled(370 self,371 timeout: float = None,372 ) -> None:373 __tracebackhide__ = True374 await self._expect_impl(375 "to.be.disabled",376 FrameExpectOptions(timeout=timeout),377 None,378 "Locator expected to be disabled",379 )380 async def not_to_be_disabled(381 self,382 timeout: float = None,383 ) -> None:384 __tracebackhide__ = True385 await self._not.to_be_disabled(timeout)386 async def to_be_editable(387 self,388 timeout: float = None,389 ) -> None:390 __tracebackhide__ = True391 await self._expect_impl(392 "to.be.editable",393 FrameExpectOptions(timeout=timeout),394 None,395 "Locator expected to be editable",396 )397 async def not_to_be_editable(398 self,399 timeout: float = None,400 ) -> None:401 __tracebackhide__ = True402 await self._not.to_be_editable(timeout)403 async def to_be_empty(404 self,405 timeout: float = None,406 ) -> None:407 __tracebackhide__ = True408 await self._expect_impl(409 "to.be.empty",410 FrameExpectOptions(timeout=timeout),411 None,412 "Locator expected to be empty",413 )414 async def not_to_be_empty(415 self,416 timeout: float = None,417 ) -> None:418 __tracebackhide__ = True419 await self._not.to_be_empty(timeout)420 async def to_be_enabled(421 self,422 timeout: float = None,423 ) -> None:424 __tracebackhide__ = True425 await self._expect_impl(426 "to.be.enabled",427 FrameExpectOptions(timeout=timeout),428 None,429 "Locator expected to be enabled",430 )431 async def not_to_be_enabled(432 self,433 timeout: float = None,434 ) -> None:435 __tracebackhide__ = True436 await self._not.to_be_enabled(timeout)437 async def to_be_hidden(438 self,439 timeout: float = None,440 ) -> None:441 __tracebackhide__ = True442 await self._expect_impl(443 "to.be.hidden",444 FrameExpectOptions(timeout=timeout),445 None,446 "Locator expected to be hidden",447 )448 async def not_to_be_hidden(449 self,450 timeout: float = None,451 ) -> None:452 __tracebackhide__ = True453 await self._not.to_be_hidden(timeout)454 async def to_be_visible(455 self,456 timeout: float = None,457 ) -> None:458 __tracebackhide__ = True459 await self._expect_impl(460 "to.be.visible",461 FrameExpectOptions(timeout=timeout),462 None,463 "Locator expected to be visible",464 )465 async def not_to_be_visible(466 self,467 timeout: float = None,468 ) -> None:469 __tracebackhide__ = True470 await self._not.to_be_visible(timeout)471 async def to_be_focused(472 self,473 timeout: float = None,474 ) -> None:475 __tracebackhide__ = True476 await self._expect_impl(477 "to.be.focused",478 FrameExpectOptions(timeout=timeout),479 None,480 "Locator expected to be focused",481 )482 async def not_to_be_focused(483 self,484 timeout: float = None,485 ) -> None:486 __tracebackhide__ = True487 await self._not.to_be_focused(timeout)488class APIResponseAssertions:489 def __init__(self, response: APIResponse, is_not: bool = False) -> None:490 self._loop = response._loop...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!