|
| 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 | + |
| 15 | +import asyncio |
| 16 | +import pytest |
| 17 | + |
| 18 | +from playwright.console_message import ConsoleMessage |
| 19 | +from typing import List |
| 20 | + |
| 21 | +from playwright.page import Page |
| 22 | + |
| 23 | + |
| 24 | +async def test_should_work(page: Page, server): |
| 25 | + async def handle_message(m, messages): |
| 26 | + messages.append(m) |
| 27 | + |
| 28 | + messages: List[ConsoleMessage] = [] |
| 29 | + page.once("console", lambda m: asyncio.ensure_future(handle_message(m, messages))) |
| 30 | + await asyncio.gather( |
| 31 | + page.evaluate('() => console.log("hello", 5, {foo: "bar"})'), |
| 32 | + page.waitForEvent("console"), |
| 33 | + ) |
| 34 | + assert len(messages) == 1 |
| 35 | + message = messages[0] |
| 36 | + assert message.text == "hello 5 JSHandle@object" |
| 37 | + assert message.type == "log" |
| 38 | + assert await message.args[0].jsonValue() == "hello" |
| 39 | + assert await message.args[1].jsonValue() == 5 |
| 40 | + assert await message.args[2].jsonValue() == {"foo": "bar"} |
| 41 | + |
| 42 | + |
| 43 | +async def test_should_emit_same_log_twice(page, server): |
| 44 | + async def handle_message(m, messages): |
| 45 | + messages.append(m) |
| 46 | + |
| 47 | + messages = [] |
| 48 | + page.on( |
| 49 | + "console", lambda m: asyncio.ensure_future(handle_message(m.text, messages)) |
| 50 | + ) |
| 51 | + await page.evaluate('() => { for (let i = 0; i < 2; ++i ) console.log("hello"); } ') |
| 52 | + assert messages == ["hello", "hello"] |
| 53 | + |
| 54 | + |
| 55 | +async def test_should_use_text_for__str__(page): |
| 56 | + async def handle_message(m, messages): |
| 57 | + messages.append(m) |
| 58 | + |
| 59 | + messages = [] |
| 60 | + page.on( |
| 61 | + "console", lambda m: asyncio.ensure_future(handle_message(m.text, messages)) |
| 62 | + ) |
| 63 | + await page.evaluate('() => console.log("Hello world")') |
| 64 | + assert len(messages) == 1 |
| 65 | + assert str(messages[0]) == "Hello world" |
| 66 | + |
| 67 | + |
| 68 | +async def test_should_work_for_different_console_API_calls(page, server): |
| 69 | + async def handle_message(m, messages): |
| 70 | + messages.append(m) |
| 71 | + |
| 72 | + messages = [] |
| 73 | + page.on("console", lambda m: asyncio.ensure_future(handle_message(m, messages))) |
| 74 | + # All console events will be reported before 'page.evaluate' is finished. |
| 75 | + await page.evaluate( |
| 76 | + """() => { |
| 77 | + // A pair of time/timeEnd generates only one Console API call. |
| 78 | + console.time('calling console.time'); |
| 79 | + console.timeEnd('calling console.time'); |
| 80 | + console.trace('calling console.trace'); |
| 81 | + console.dir('calling console.dir'); |
| 82 | + console.warn('calling console.warn'); |
| 83 | + console.error('calling console.error'); |
| 84 | + console.log(Promise.resolve('should not wait until resolved!')); |
| 85 | + }""" |
| 86 | + ) |
| 87 | + assert list(map(lambda msg: msg.type, messages)) == [ |
| 88 | + "timeEnd", |
| 89 | + "trace", |
| 90 | + "dir", |
| 91 | + "warning", |
| 92 | + "error", |
| 93 | + "log", |
| 94 | + ] |
| 95 | + |
| 96 | + assert "calling console.time" in messages[0].text |
| 97 | + assert list(map(lambda msg: msg.text, messages[1:])) == [ |
| 98 | + "calling console.trace", |
| 99 | + "calling console.dir", |
| 100 | + "calling console.warn", |
| 101 | + "calling console.error", |
| 102 | + "JSHandle@promise", |
| 103 | + ] |
| 104 | + |
| 105 | + |
| 106 | +async def test_should_not_fail_for_window_object(page, server): |
| 107 | + async def handle_message(m, messages): |
| 108 | + messages.append(m) |
| 109 | + |
| 110 | + messages = [] |
| 111 | + page.once("console", lambda m: asyncio.ensure_future(handle_message(m, messages))) |
| 112 | + await asyncio.gather( |
| 113 | + page.evaluate("() => console.error(window)"), page.waitForEvent("console") |
| 114 | + ) |
| 115 | + assert len(messages) == 1 |
| 116 | + assert messages[0].text == "JSHandle@object" |
| 117 | + |
| 118 | + |
| 119 | +async def test_should_trigger_correct_Log(page, server): |
| 120 | + await page.goto("about:blank") |
| 121 | + message = ( |
| 122 | + await asyncio.gather( |
| 123 | + page.waitForEvent("console"), |
| 124 | + page.evaluate("async url => fetch(url).catch(e => {})", server.EMPTY_PAGE), |
| 125 | + ) |
| 126 | + )[0] |
| 127 | + assert "Access-Control-Allow-Origin" in message.text |
| 128 | + assert message.type == "error" |
| 129 | + |
| 130 | + |
| 131 | +async def test_should_have_location_for_console_API_calls(page, server): |
| 132 | + await page.goto(server.EMPTY_PAGE) |
| 133 | + message = ( |
| 134 | + await asyncio.gather( |
| 135 | + page.waitForEvent("console"), page.goto(server.PREFIX + "/consolelog.html"), |
| 136 | + ) |
| 137 | + )[0] |
| 138 | + assert message.text == "yellow" |
| 139 | + assert message.type == "log" |
| 140 | + location = message.location |
| 141 | + # Engines have different column notion. |
| 142 | + del location["columnNumber"] |
| 143 | + assert location == {"url": server.PREFIX + "/consolelog.html", "lineNumber": 7} |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.skip_browser("firefox") # a fix just landed upstream, will roll later |
| 147 | +async def test_should_not_throw_when_there_are_console_messages_in_detached_iframes( |
| 148 | + page, server |
| 149 | +): |
| 150 | + await page.goto(server.EMPTY_PAGE) |
| 151 | + popup = ( |
| 152 | + await asyncio.gather( |
| 153 | + page.waitForEvent("popup"), |
| 154 | + page.evaluate( |
| 155 | + """async() => { |
| 156 | + // 1. Create a popup that Playwright is not connected to. |
| 157 | + const win = window.open(''); |
| 158 | + window._popup = win; |
| 159 | + if (window.document.readyState !== 'complete') |
| 160 | + await new Promise(f => window.addEventListener('load', f)); |
| 161 | + // 2. In this popup, create an iframe that console.logs a message. |
| 162 | + win.document.body.innerHTML = `<iframe src='/consolelog.html'></iframe>`; |
| 163 | + const frame = win.document.querySelector('iframe'); |
| 164 | + if (!frame.contentDocument || frame.contentDocument.readyState !== 'complete') |
| 165 | + await new Promise(f => frame.addEventListener('load', f)); |
| 166 | + // 3. After that, remove the iframe. |
| 167 | + frame.remove(); |
| 168 | + }""" |
| 169 | + ), |
| 170 | + ) |
| 171 | + )[0] |
| 172 | + # 4. Connect to the popup and make sure it doesn't throw. |
| 173 | + assert await popup.evaluate("1 + 1") == 2 |
0 commit comments