| 
 | 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 | +from typing import Optional  | 
 | 16 | + | 
 | 17 | +import pytest  | 
 | 18 | + | 
 | 19 | +from playwright.sync_api import Dialog, Page  | 
 | 20 | + | 
 | 21 | +from ..server import HttpRequestWithPostBody, Server  | 
 | 22 | + | 
 | 23 | + | 
 | 24 | +def test_console_event_should_work(page: Page) -> None:  | 
 | 25 | +    with page.context.expect_console_message() as console_info:  | 
 | 26 | +        page.evaluate("() => console.log('hello')")  | 
 | 27 | +    message = console_info.value  | 
 | 28 | +    assert message.text == "hello"  | 
 | 29 | +    assert message.page == page  | 
 | 30 | + | 
 | 31 | + | 
 | 32 | +def test_console_event_should_work_in_popup(page: Page) -> None:  | 
 | 33 | +    with page.context.expect_console_message() as console_info:  | 
 | 34 | +        with page.expect_popup() as popup_info:  | 
 | 35 | +            page.evaluate(  | 
 | 36 | +                """() => {  | 
 | 37 | +                const win = window.open('');  | 
 | 38 | +                win.console.log('hello');  | 
 | 39 | +                }"""  | 
 | 40 | +            )  | 
 | 41 | +    message = console_info.value  | 
 | 42 | +    popup = popup_info.value  | 
 | 43 | +    assert message.text == "hello"  | 
 | 44 | +    assert message.page == popup  | 
 | 45 | + | 
 | 46 | + | 
 | 47 | +# console message from javascript: url is not reported at all  | 
 | 48 | +@pytest.mark.skip_browser("firefox")  | 
 | 49 | +def test_console_event_should_work_in_popup_2(page: Page, browser_name: str) -> None:  | 
 | 50 | +    with page.context.expect_console_message(  | 
 | 51 | +        lambda msg: msg.type == "log"  | 
 | 52 | +    ) as console_info:  | 
 | 53 | +        with page.context.expect_page() as page_info:  | 
 | 54 | +            page.evaluate(  | 
 | 55 | +                """async () => {  | 
 | 56 | +                const win = window.open('javascript:console.log("hello")');  | 
 | 57 | +                await new Promise(f => setTimeout(f, 0));  | 
 | 58 | +                win.close();  | 
 | 59 | +            }"""  | 
 | 60 | +            )  | 
 | 61 | +    message = console_info.value  | 
 | 62 | +    popup = page_info.value  | 
 | 63 | +    assert message.text == "hello"  | 
 | 64 | +    assert message.page == popup  | 
 | 65 | + | 
 | 66 | + | 
 | 67 | +# console message from javascript: url is not reported at all  | 
 | 68 | +@pytest.mark.skip_browser("firefox")  | 
 | 69 | +def test_console_event_should_work_in_immediately_closed_popup(  | 
 | 70 | +    page: Page, browser_name: str  | 
 | 71 | +) -> None:  | 
 | 72 | +    with page.context.expect_console_message(  | 
 | 73 | +        lambda msg: msg.type == "log"  | 
 | 74 | +    ) as console_info:  | 
 | 75 | +        with page.context.expect_page() as page_info:  | 
 | 76 | +            page.evaluate(  | 
 | 77 | +                """() => {  | 
 | 78 | +                const win = window.open('');  | 
 | 79 | +                win.console.log('hello');  | 
 | 80 | +                win.close();  | 
 | 81 | +            }"""  | 
 | 82 | +            )  | 
 | 83 | +    message = console_info.value  | 
 | 84 | +    popup = page_info.value  | 
 | 85 | +    assert message.text == "hello"  | 
 | 86 | +    assert message.page == popup  | 
 | 87 | + | 
 | 88 | + | 
 | 89 | +def test_dialog_event_should_work1(page: Page) -> None:  | 
 | 90 | +    dialog1: Optional[Dialog] = None  | 
 | 91 | + | 
 | 92 | +    def handle_page_dialog(dialog: Dialog) -> None:  | 
 | 93 | +        nonlocal dialog1  | 
 | 94 | +        dialog1 = dialog  | 
 | 95 | +        dialog.accept("hello")  | 
 | 96 | + | 
 | 97 | +    page.on("dialog", handle_page_dialog)  | 
 | 98 | + | 
 | 99 | +    dialog2: Optional[Dialog] = None  | 
 | 100 | + | 
 | 101 | +    def handle_context_dialog(dialog: Dialog) -> None:  | 
 | 102 | +        nonlocal dialog2  | 
 | 103 | +        dialog2 = dialog  | 
 | 104 | + | 
 | 105 | +    page.context.on("dialog", handle_context_dialog)  | 
 | 106 | + | 
 | 107 | +    assert page.evaluate("() => prompt('hey?')") == "hello"  | 
 | 108 | +    assert dialog1  | 
 | 109 | +    assert dialog1 == dialog2  | 
 | 110 | +    assert dialog1.message == "hey?"  | 
 | 111 | +    assert dialog1.page == page  | 
 | 112 | + | 
 | 113 | + | 
 | 114 | +def test_dialog_event_should_work_in_popup1(page: Page) -> None:  | 
 | 115 | +    dialog: Optional[Dialog] = None  | 
 | 116 | + | 
 | 117 | +    def handle_dialog(d: Dialog) -> None:  | 
 | 118 | +        nonlocal dialog  | 
 | 119 | +        dialog = d  | 
 | 120 | +        dialog.accept("hello")  | 
 | 121 | + | 
 | 122 | +    page.context.on("dialog", handle_dialog)  | 
 | 123 | + | 
 | 124 | +    with page.expect_popup() as popup_info:  | 
 | 125 | +        assert page.evaluate("() => window.open('').prompt('hey?')") == "hello"  | 
 | 126 | +    popup = popup_info.value  | 
 | 127 | +    assert dialog  | 
 | 128 | +    assert dialog.message == "hey?"  | 
 | 129 | +    assert dialog.page == popup  | 
 | 130 | + | 
 | 131 | + | 
 | 132 | +# console message from javascript: url is not reported at all  | 
 | 133 | +@pytest.mark.skip_browser("firefox")  | 
 | 134 | +def test_dialog_event_should_work_in_popup_2(page: Page, browser_name: str) -> None:  | 
 | 135 | +    def handle_dialog(dialog: Dialog) -> None:  | 
 | 136 | +        assert dialog.message == "hey?"  | 
 | 137 | +        assert dialog.page is None  | 
 | 138 | +        dialog.accept("hello")  | 
 | 139 | + | 
 | 140 | +    page.context.on("dialog", handle_dialog)  | 
 | 141 | + | 
 | 142 | +    assert page.evaluate("() => window.open('javascript:prompt(\"hey?\")')")  | 
 | 143 | + | 
 | 144 | + | 
 | 145 | +# console message from javascript: url is not reported at all  | 
 | 146 | +@pytest.mark.skip_browser("firefox")  | 
 | 147 | +def test_dialog_event_should_work_in_immdiately_closed_popup(page: Page) -> None:  | 
 | 148 | +    popup = None  | 
 | 149 | + | 
 | 150 | +    def handle_popup(p: Page) -> None:  | 
 | 151 | +        nonlocal popup  | 
 | 152 | +        popup = p  | 
 | 153 | + | 
 | 154 | +    page.on("popup", handle_popup)  | 
 | 155 | + | 
 | 156 | +    with page.context.expect_console_message() as console_info:  | 
 | 157 | +        page.evaluate(  | 
 | 158 | +            """() => {  | 
 | 159 | +                const win = window.open();  | 
 | 160 | +                win.console.log('hello');  | 
 | 161 | +                win.close();  | 
 | 162 | +            }"""  | 
 | 163 | +        )  | 
 | 164 | +    message = console_info.value  | 
 | 165 | + | 
 | 166 | +    assert message.text == "hello"  | 
 | 167 | +    assert message.page == popup  | 
 | 168 | + | 
 | 169 | + | 
 | 170 | +def test_dialog_event_should_work_with_inline_script_tag(  | 
 | 171 | +    page: Page, server: Server  | 
 | 172 | +) -> None:  | 
 | 173 | +    def handle_route(request: HttpRequestWithPostBody) -> None:  | 
 | 174 | +        request.setHeader("content-type", "text/html")  | 
 | 175 | +        request.write(b"""<script>window.result = prompt('hey?')</script>""")  | 
 | 176 | +        request.finish()  | 
 | 177 | + | 
 | 178 | +    server.set_route("/popup.html", handle_route)  | 
 | 179 | +    page.goto(server.EMPTY_PAGE)  | 
 | 180 | +    page.set_content("<a href='popup.html' target=_blank>Click me</a>")  | 
 | 181 | + | 
 | 182 | +    def handle_dialog(dialog: Dialog) -> None:  | 
 | 183 | +        assert dialog.message == "hey?"  | 
 | 184 | +        assert dialog.page == popup  | 
 | 185 | +        dialog.accept("hello")  | 
 | 186 | + | 
 | 187 | +    page.context.on("dialog", handle_dialog)  | 
 | 188 | + | 
 | 189 | +    with page.expect_popup() as popup_info:  | 
 | 190 | +        page.click("a")  | 
 | 191 | +    popup = popup_info.value  | 
 | 192 | +    assert popup.evaluate("window.result") == "hello"  | 
 | 193 | + | 
 | 194 | + | 
 | 195 | +def test_console_event_should_work_with_context_manager(page: Page) -> None:  | 
 | 196 | +    with page.context.expect_console_message() as cm_info:  | 
 | 197 | +        page.evaluate("() => console.log('hello')")  | 
 | 198 | +    message = cm_info.value  | 
 | 199 | +    assert message.text == "hello"  | 
 | 200 | +    assert message.page == page  | 
0 commit comments