Best Python code snippet using playwright-python
_page.py
Source:_page.py
...696 if event != Page.Events.Close:697 wait_helper.reject_on_event(self, Page.Events.Close, Error("Page closed"))698 wait_helper.wait_for_event(self, event, predicate)699 return EventContextManagerImpl(wait_helper.result())700 def expect_console_message(701 self,702 predicate: Callable[[ConsoleMessage], bool] = None,703 timeout: float = None,704 ) -> EventContextManagerImpl[ConsoleMessage]:705 return self.expect_event(Page.Events.Console, predicate, timeout)706 def expect_download(707 self,708 predicate: Callable[[Download], bool] = None,709 timeout: float = None,710 ) -> EventContextManagerImpl[Download]:711 return self.expect_event(Page.Events.Download, predicate, timeout)712 def expect_file_chooser(713 self,714 predicate: Callable[[FileChooser], bool] = None,...
test_worker.py
Source:test_worker.py
...41 with pytest.raises(Error) as exc:42 await worker_this_obj.get_property("self")43 assert "Most likely the worker has been closed." in exc.value.message44async def test_workers_should_report_console_logs(page):45 async with page.expect_console_message() as message_info:46 await page.evaluate(47 '() => new Worker(URL.createObjectURL(new Blob(["console.log(1)"], {type: "application/javascript"})))'48 )49 message = await message_info.value50 assert message.text == "1"51@pytest.mark.skip_browser("firefox") # TODO: investigate further @pavelfeldman52async def test_workers_should_have_JSHandles_for_console_logs(page):53 log_promise = asyncio.Future()54 page.on("console", lambda m: log_promise.set_result(m))55 await page.evaluate(56 "() => new Worker(URL.createObjectURL(new Blob(['console.log(1,2,3,this)'], {type: 'application/javascript'})))"57 )58 log = await log_promise59 assert log.text == "1 2 3 JSHandle@object"...
text_play_wright.py
Source:text_play_wright.py
...106# sheet.cell(i, co+25).value = photo[co]107# book.save('text1play.xlsx')108# except Exception as e:109# print(e)...
test_console.py
Source:test_console.py
...15from playwright.async_api import ConsoleMessage, Page16async def test_console_should_work(page: Page, server):17 messages: List[ConsoleMessage] = []18 page.once("console", lambda m: messages.append(m))19 async with page.expect_console_message() as message_info:20 await page.evaluate('() => console.log("hello", 5, {foo: "bar"})')21 message = await message_info.value22 assert message.text == "hello 5 JSHandle@object"23 assert str(message) == "hello 5 JSHandle@object"24 assert message.type == "log"25 assert await message.args[0].json_value() == "hello"26 assert await message.args[1].json_value() == 527 assert await message.args[2].json_value() == {"foo": "bar"}28async def test_console_should_emit_same_log_twice(page):29 messages = []30 page.on("console", lambda m: messages.append(m.text))31 await page.evaluate('() => { for (let i = 0; i < 2; ++i ) console.log("hello"); } ')32 assert messages == ["hello", "hello"]33async def test_console_should_use_text_for__str__(page):34 messages = []35 page.on("console", lambda m: messages.append(m))36 await page.evaluate('() => console.log("Hello world")')37 assert len(messages) == 138 assert str(messages[0]) == "Hello world"39async def test_console_should_work_for_different_console_api_calls(page, server):40 messages = []41 page.on("console", lambda m: messages.append(m))42 # All console events will be reported before 'page.evaluate' is finished.43 await page.evaluate(44 """() => {45 // A pair of time/timeEnd generates only one Console API call.46 console.time('calling console.time');47 console.timeEnd('calling console.time');48 console.trace('calling console.trace');49 console.dir('calling console.dir');50 console.warn('calling console.warn');51 console.error('calling console.error');52 console.log(Promise.resolve('should not wait until resolved!'));53 }"""54 )55 assert list(map(lambda msg: msg.type, messages)) == [56 "timeEnd",57 "trace",58 "dir",59 "warning",60 "error",61 "log",62 ]63 assert "calling console.time" in messages[0].text64 assert list(map(lambda msg: msg.text, messages[1:])) == [65 "calling console.trace",66 "calling console.dir",67 "calling console.warn",68 "calling console.error",69 "JSHandle@promise",70 ]71async def test_console_should_not_fail_for_window_object(page):72 async with page.expect_console_message() as message_info:73 await page.evaluate("console.error(window)")74 message = await message_info.value75 assert message.text == "JSHandle@object"76async def test_console_should_trigger_correct_log(page, server):77 await page.goto("about:blank")78 async with page.expect_console_message() as message_info:79 await page.evaluate("async url => fetch(url).catch(e => {})", server.EMPTY_PAGE)80 message = await message_info.value81 assert "Access-Control-Allow-Origin" in message.text82 assert message.type == "error"83async def test_console_should_have_location_for_console_api_calls(page, server):84 await page.goto(server.EMPTY_PAGE)85 async with page.expect_console_message() as message_info:86 await page.goto(server.PREFIX + "/consolelog.html"),87 message = await message_info.value88 assert message.text == "yellow"89 assert message.type == "log"90 location = message.location91 # Engines have different column notion.92 assert location["url"] == server.PREFIX + "/consolelog.html"93 assert location["lineNumber"] == 794async def test_console_should_not_throw_when_there_are_console_messages_in_detached_iframes(95 page, server96):97 await page.goto(server.EMPTY_PAGE)98 async with page.expect_popup() as page_info:99 await page.evaluate(...
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!!