Best Python code snippet using playwright-python
test_locators.py
Source:test_locators.py
...507 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>"515 )516 expect(page.locator("div", has=page.locator('text="hello"'))).to_have_count(1)517 assert (518 page.locator("div", has=page.locator('text="hello"')).evaluate(519 "e => e.outerHTML"520 )521 == "<div><span>hello</span></div>"522 )523 expect(page.locator("div", has=page.locator("xpath=./span"))).to_have_count(2)524 expect(page.locator("div", has=page.locator("span"))).to_have_count(2)525 expect(page.locator("div", has=page.locator("span", has_text="wor"))).to_have_count(526 1527 )528 assert (529 page.locator("div", has=page.locator("span", has_text="wor")).evaluate(530 "e => e.outerHTML"531 )532 == "<div><span>world</span></div>"533 )534 expect(535 page.locator(536 "div",537 has=page.locator("span"),538 has_text="wor",539 )540 ).to_have_count(1)541def test_locator_should_enforce_same_frame_for_has_locator(542 page: Page, server: Server543) -> None:544 page.goto(server.PREFIX + "/frames/two-frames.html")545 child = page.frames[1]546 with pytest.raises(Error) as exc_info:547 page.locator("div", has=child.locator("span"))548 assert (549 'Inner "has" locator must belong to the same frame.' in exc_info.value.message...
_assertions.py
Source:_assertions.py
...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,...
test_assertions.py
Source:test_assertions.py
...96 expect(page.locator("div.foobar")).to_have_class([re.compile("foobar")])97 expect(page.locator("div.foobar")).not_to_have_class("kekstar", timeout=100)98 with pytest.raises(AssertionError):99 expect(page.locator("div.foobar")).to_have_class("oh-no", timeout=100)100def test_assertions_locator_to_have_count(page: Page, server: Server) -> None:101 page.goto(server.EMPTY_PAGE)102 page.set_content("<div class=foobar>kek</div><div class=foobar>kek</div>")103 expect(page.locator("div.foobar")).to_have_count(2)104 expect(page.locator("div.foobar")).not_to_have_count(42, timeout=100)105 with pytest.raises(AssertionError):106 expect(page.locator("div.foobar")).to_have_count(42, timeout=100)107def test_assertions_locator_to_have_css(page: Page, server: Server) -> None:108 page.goto(server.EMPTY_PAGE)109 page.set_content("<div class=foobar style='color: rgb(234, 74, 90);'>kek</div>")110 expect(page.locator("div.foobar")).to_have_css("color", "rgb(234, 74, 90)")111 expect(page.locator("div.foobar")).not_to_have_css(112 "color", "rgb(42, 42, 42)", timeout=100113 )114 with pytest.raises(AssertionError):115 expect(page.locator("div.foobar")).to_have_css(116 "color", "rgb(42, 42, 42)", timeout=100117 )118def test_assertions_locator_to_have_id(page: Page, server: Server) -> None:119 page.goto(server.EMPTY_PAGE)120 page.set_content("<div class=foobar id=kek>kek</div>")...
test_browser_user.py
Source:test_browser_user.py
...95 page.click("text=Delete")96 assert page.url == f"{settings['BASE_URL']}/db/test"97 page.goto(table_url)98 locator = page.locator("text=No such table")99 playwright.sync_api.expect(locator).to_have_count(1)100 # Delete the database.101 page.once("dialog", lambda dialog: dialog.accept()) # Callback for next click.102 page.click("text=Delete")103 assert page.url == f"{settings['BASE_URL']}/dbs/owner/{settings['USER_USERNAME']}"104def test_table_csv(settings, page): # 'page' fixture from 'pytest-playwright'105 "Test login, creating a table, inserting data from a CSV file."106 login_user(settings, page)107 # Create a database 'test'.108 page.goto(f"{settings['BASE_URL']}/dbs/owner/{settings['USER_USERNAME']}")109 page.click("text=Create")110 assert page.url == f"{settings['BASE_URL']}/db/"111 page.click('input[name="name"]')112 page.fill('input[name="name"]', "test")113 page.click('button:has-text("Create")')114 assert page.url == f"{settings['BASE_URL']}/db/test"115 # Create a table 't1'.116 page.click("text=Create table")117 assert page.url == f"{settings['BASE_URL']}/table/test"118 page.click('input[name="name"]')119 page.fill('input[name="name"]', "t1")120 page.click('input[name="column0name"]')121 page.fill('input[name="column0name"]', "i")122 page.check("#column0primarykey")123 page.click('input[name="column1name"]')124 page.fill('input[name="column1name"]', "r")125 page.select_option('select[name="column1type"]', "REAL")126 page.click('input[name="column2name"]')127 page.fill('input[name="column2name"]', "j")128 page.click('input[name="column3name"]')129 page.fill('input[name="column3name"]', "t")130 page.select_option('select[name="column3type"]', "TEXT")131 page.check('input[name="column3notnull"]')132 page.click('button:has-text("Create")')133 assert page.url == f"{settings['BASE_URL']}/table/test/t1"134 # Insert data from file.135 page.click("text=Insert from file")136 assert page.url == "http://localhost:5001/table/test/t1/insert"137 with page.expect_file_chooser() as fc_info:138 page.click('input[name="csvfile"]')139 file_chooser = fc_info.value140 file_chooser.set_files("test.csv")141 page.click("text=Insert from CSV file")142 assert page.url == "http://localhost:5001/table/test/t1"143 page.click("text=Database test")144 assert page.url == f"{settings['BASE_URL']}/db/test"145 # Query the database.146 page.click("text=Query")147 assert page.url == "http://localhost:5001/query/test"148 page.click('textarea[name="select"]')149 page.fill('textarea[name="select"]', "i,r")150 page.click('textarea[name="from"]')151 page.fill('textarea[name="from"]', "t1")152 page.click('textarea[name="where"]')153 page.fill('textarea[name="where"]', 't = "blah"')154 page.click("text=Execute query")155 assert page.url == "http://localhost:5001/query/test/rows"156 assert page.locator("#nrows").text_content() == "1"157 locator = page.locator("#rows > tbody > tr")158 playwright.sync_api.expect(locator).to_have_count(1)159 # Modify the query.160 page.click("text=Edit query")161 assert page.url.startswith("http://localhost:5001/query/test")162 page.click('textarea[name="where"]')163 page.fill('textarea[name="where"]', "j = 3")164 page.click("text=Execute query")165 assert page.url == "http://localhost:5001/query/test/rows"166 assert page.locator("#nrows").text_content() == "2"167 locator = page.locator("#rows > tbody > tr")168 playwright.sync_api.expect(locator).to_have_count(2)169 # Delete the database.170 page.click("text=Database test")171 assert page.url == "http://localhost:5001/db/test"172 page.once("dialog", lambda dialog: dialog.accept()) # Callback for next click.173 page.click("text=Delete")174 assert page.url == f"{settings['BASE_URL']}/dbs/owner/{settings['USER_USERNAME']}"175def test_db_upload(settings, page):176 "Test uploading a Sqlite3 database file."177 login_user(settings, page)178 # Upload database file.179 page.goto(f"{settings['BASE_URL']}/dbs/owner/{settings['USER_USERNAME']}")180 page.click("text=Upload")181 assert page.url == "http://localhost:5001/dbs/upload"182 page.once("filechooser", lambda fc: fc.set_files("test.sqlite3"))183 page.click('input[name="sqlite3file"]')184 page.click("text=Upload SQLite3 file")185 assert page.url == "http://localhost:5001/db/test"186 page.click("text=3 rows")187 assert page.url == "http://localhost:5001/table/test/t1"188 locator = page.locator("#rows > tbody > tr")189 playwright.sync_api.expect(locator).to_have_count(3)190 page.click('a[role="button"]:has-text("Schema")')191 assert page.url == "http://localhost:5001/table/test/t1/schema"192 locator = page.locator("#columns > tbody > tr")193 playwright.sync_api.expect(locator).to_have_count(7)194 # Delete the database.195 page.click("text=Database test")196 assert page.url == "http://localhost:5001/db/test"197 page.once("dialog", lambda dialog: dialog.accept()) # Callback for next click.198 page.click("text=Delete")199 assert page.url == f"{settings['BASE_URL']}/dbs/owner/{settings['USER_USERNAME']}"200def test_view(settings, page):201 "Test view creation, based on a table in an uploaded Sqlite3 database file."202 login_user(settings, page)203 # Upload database file.204 page.goto(f"{settings['BASE_URL']}/dbs/owner/{settings['USER_USERNAME']}")205 page.click("text=Upload")206 assert page.url == "http://localhost:5001/dbs/upload"207 page.once("filechooser", lambda fc: fc.set_files("test.sqlite3"))208 page.click('input[name="sqlite3file"]')209 page.click("text=Upload SQLite3 file")210 assert page.url == "http://localhost:5001/db/test"211 # Create the view.212 page.click("text=Query")213 assert page.url == "http://localhost:5001/query/test"214 page.click('textarea[name="select"]')215 page.fill('textarea[name="select"]', "i, r1")216 page.click('textarea[name="from"]')217 page.fill('textarea[name="from"]', "t1")218 page.click('textarea[name="where"]')219 page.fill('textarea[name="where"]', "i2 < 0")220 page.click("text=Execute query")221 assert page.url == "http://localhost:5001/query/test/rows"222 page.click("text=Create view")223 page.click('input[name="name"]')224 page.fill('input[name="name"]', "v1")225 page.click('button:has-text("Create")')226 page.wait_for_timeout(3000)227 locator = page.locator("#rows > tbody > tr")228 playwright.sync_api.expect(locator).to_have_count(2)229 # Delete the database.230 page.click("text=Database test")231 assert page.url == "http://localhost:5001/db/test"232 page.once("dialog", lambda dialog: dialog.accept()) # Callback for next click.233 page.click("text=Delete")234 assert page.url == f"{settings['BASE_URL']}/dbs/owner/{settings['USER_USERNAME']}"...
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!!