How to use to_have_text method in Playwright Python

Best Python code snippet using playwright-python

test_locators.py

Source:test_locators.py Github

copy

Full Screen

...469 is True470 )471def test_locator_query_should_filter_by_text(page: Page, server: Server) -> None:472 page.set_content("<div>Foobar</div><div>Bar</div>")473 expect(page.locator("div", has_text="Foo")).to_have_text("Foobar")474def test_locator_query_should_filter_by_text_2(page: Page, server: Server) -> None:475 page.set_content("<div>foo <span>hello world</span> bar</div>")476 expect(page.locator("div", has_text="hello world")).to_have_text(477 "foo hello world bar"478 )479def test_locator_query_should_filter_by_regex(page: Page, server: Server) -> None:480 page.set_content("<div>Foobar</div><div>Bar</div>")481 expect(page.locator("div", has_text=re.compile(r"Foo.*"))).to_have_text("Foobar")482def test_locator_query_should_filter_by_text_with_quotes(483 page: Page, server: Server484) -> None:485 page.set_content('<div>Hello "world"</div><div>Hello world</div>')486 expect(page.locator("div", has_text='Hello "world"')).to_have_text('Hello "world"')487def test_locator_query_should_filter_by_regex_with_quotes(488 page: Page, server: Server489) -> None:490 page.set_content('<div>Hello "world"</div><div>Hello world</div>')491 expect(page.locator("div", has_text=re.compile('Hello "world"'))).to_have_text(492 'Hello "world"'493 )494def test_locator_query_should_filter_by_regex_and_regexp_flags(495 page: Page, server: Server496) -> None:497 page.set_content('<div>Hello "world"</div><div>Hello world</div>')498 expect(499 page.locator("div", has_text=re.compile('hElLo "world', re.IGNORECASE))500 ).to_have_text('Hello "world"')501def test_locator_should_return_page(page: Page, server: Server) -> None:502 page.goto(server.PREFIX + "/frames/two-frames.html")503 outer = page.locator("#outer")504 assert outer.page == page505 inner = outer.locator("#inner")506 assert inner.page == page507 in_frame = page.frames[1].locator("div")508 assert in_frame.page == page509def test_locator_should_support_has_locator(page: Page, server: Server) -> None:510 page.set_content("<div><span>hello</span></div><div><span>world</span></div>")511 expect(page.locator("div", has=page.locator("text=world"))).to_have_count(1)512 assert (513 page.locator("div", has=page.locator("text=world")).evaluate("e => e.outerHTML")514 == "<div><span>world</span></div>"...

Full Screen

Full Screen

_assertions.py

Source:_assertions.py Github

copy

Full Screen

...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 )...

Full Screen

Full Screen

test_assertions.py

Source:test_assertions.py Github

copy

Full Screen

...131 expect(page.locator("div")).to_have_js_property(132 "foo",133 {"a": 1, "b": "string", "c": datetime.utcfromtimestamp(1627503992000 / 1000)},134 )135def test_assertions_locator_to_have_text(page: Page, server: Server) -> None:136 page.goto(server.EMPTY_PAGE)137 page.set_content("<div id=foobar>kek</div>")138 expect(page.locator("div#foobar")).to_have_text("kek")139 expect(page.locator("div#foobar")).not_to_have_text("top", timeout=100)140 page.set_content("<div>Text \n1</div><div>Text 2a</div>")141 # Should only normalize whitespace in the first item.142 expect(page.locator("div")).to_have_text(["Text 1", re.compile(r"Text \d+a")])143def test_assertions_locator_to_have_value(page: Page, server: Server) -> None:144 page.goto(server.EMPTY_PAGE)145 page.set_content("<input type=text id=foo>")146 my_input = page.locator("#foo")147 expect(my_input).to_have_value("")148 expect(my_input).not_to_have_value("bar", timeout=100)149 my_input.fill("kektus")150 expect(my_input).to_have_value("kektus")151def test_assertions_locator_to_be_checked(page: Page, server: Server) -> None:152 page.goto(server.EMPTY_PAGE)153 page.set_content("<input type=checkbox>")154 my_checkbox = page.locator("input")155 expect(my_checkbox).not_to_be_checked()156 with pytest.raises(AssertionError):157 expect(my_checkbox).to_be_checked(timeout=100)158 expect(my_checkbox).to_be_checked(timeout=100, checked=False)159 with pytest.raises(AssertionError):160 expect(my_checkbox).to_be_checked(timeout=100, checked=True)161 my_checkbox.check()162 expect(my_checkbox).to_be_checked(timeout=100, checked=True)163 with pytest.raises(AssertionError):164 expect(my_checkbox).to_be_checked(timeout=100, checked=False)165 expect(my_checkbox).to_be_checked()166def test_assertions_locator_to_be_disabled_enabled(page: Page, server: Server) -> None:167 page.goto(server.EMPTY_PAGE)168 page.set_content("<input type=checkbox>")169 my_checkbox = page.locator("input")170 expect(my_checkbox).not_to_be_disabled()171 expect(my_checkbox).to_be_enabled()172 with pytest.raises(AssertionError):173 expect(my_checkbox).to_be_disabled(timeout=100)174 my_checkbox.evaluate("e => e.disabled = true")175 expect(my_checkbox).to_be_disabled()176 with pytest.raises(AssertionError):177 expect(my_checkbox).to_be_enabled(timeout=100)178def test_assertions_locator_to_be_editable(page: Page, server: Server) -> None:179 page.goto(server.EMPTY_PAGE)180 page.set_content("<input></input><button disabled>Text</button>")181 expect(page.locator("button")).not_to_be_editable()182 expect(page.locator("input")).to_be_editable()183 with pytest.raises(AssertionError):184 expect(page.locator("button")).to_be_editable(timeout=100)185def test_assertions_locator_to_be_empty(page: Page, server: Server) -> None:186 page.goto(server.EMPTY_PAGE)187 page.set_content(188 "<input value=text name=input1></input><input name=input2></input>"189 )190 expect(page.locator("input[name=input1]")).not_to_be_empty()191 expect(page.locator("input[name=input2]")).to_be_empty()192 with pytest.raises(AssertionError):193 expect(page.locator("input[name=input1]")).to_be_empty(timeout=100)194def test_assertions_locator_to_be_focused(page: Page, server: Server) -> None:195 page.goto(server.EMPTY_PAGE)196 page.set_content("<input type=checkbox>")197 my_checkbox = page.locator("input")198 with pytest.raises(AssertionError):199 expect(my_checkbox).to_be_focused(timeout=100)200 my_checkbox.focus()201 expect(my_checkbox).to_be_focused()202def test_assertions_locator_to_be_hidden_visible(page: Page, server: Server) -> None:203 page.goto(server.EMPTY_PAGE)204 page.set_content("<div style='width: 50px; height: 50px;'>Something</div>")205 my_checkbox = page.locator("div")206 expect(my_checkbox).to_be_visible()207 with pytest.raises(AssertionError):208 expect(my_checkbox).to_be_hidden(timeout=100)209 my_checkbox.evaluate("e => e.style.display = 'none'")210 expect(my_checkbox).to_be_hidden()211 with pytest.raises(AssertionError):212 expect(my_checkbox).to_be_visible(timeout=100)213def test_assertions_should_serialize_regexp_correctly(214 page: Page, server: Server215) -> None:216 page.goto(server.EMPTY_PAGE)217 page.set_content("<div>iGnOrEcAsE</div>")218 expect(page.locator("div")).to_have_text(re.compile(r"ignorecase", re.IGNORECASE))219 page.set_content(220 """<div>start221some222lines223between224end</div>"""225 )226 expect(page.locator("div")).to_have_text(re.compile(r"start.*end", re.DOTALL))227 page.set_content(228 """<div>line1229line2230line3</div>"""231 )232 expect(page.locator("div")).to_have_text(re.compile(r"^line2$", re.MULTILINE))233def test_assertions_response_is_ok_pass(page: Page, server: Server) -> None:234 response = page.request.get(server.EMPTY_PAGE)235 expect(response).to_be_ok()236def test_assertions_response_is_ok_pass_with_not(page: Page, server: Server) -> None:237 response = page.request.get(server.PREFIX + "/unknown")238 expect(response).not_to_be_ok()239def test_assertions_response_is_ok_fail(page: Page, server: Server) -> None:240 response = page.request.get(server.PREFIX + "/unknown")241 with pytest.raises(AssertionError) as excinfo:242 expect(response).to_be_ok()243 error_message = str(excinfo.value)244 assert ("→ GET " + server.PREFIX + "/unknown") in error_message...

Full Screen

Full Screen

base_page.py

Source:base_page.py Github

copy

Full Screen

...117 def press(self, locator: str, keyboard: str):118 self.page.press(locator, keyboard)119 @allure.step('Contains Text - {locator} has {text}')120 def have_text(self, locator, text) -> None:...

Full Screen

Full Screen

Playwright tutorial

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.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful