|
| 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 pytest |
| 16 | + |
| 17 | +from playwright.sync_api import Error |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.only_browser("chromium") |
| 21 | +def test_should_work(page): |
| 22 | + client = page.context.new_cdp_session(page) |
| 23 | + events = [] |
| 24 | + client.on("Runtime.consoleAPICalled", lambda params: events.append(params)) |
| 25 | + client.send("Runtime.enable") |
| 26 | + result = client.send( |
| 27 | + "Runtime.evaluate", |
| 28 | + {"expression": "window.foo = 'bar'; console.log('log'); 'result'"}, |
| 29 | + ) |
| 30 | + assert result == {"result": {"type": "string", "value": "result"}} |
| 31 | + foo = page.evaluate("() => window.foo") |
| 32 | + assert foo == "bar" |
| 33 | + assert events[0]["args"][0]["value"] == "log" |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.only_browser("chromium") |
| 37 | +def test_should_receive_events(page, server): |
| 38 | + client = page.context.new_cdp_session(page) |
| 39 | + client.send("Network.enable") |
| 40 | + events = [] |
| 41 | + client.on("Network.requestWillBeSent", lambda event: events.append(event)) |
| 42 | + page.goto(server.EMPTY_PAGE) |
| 43 | + assert len(events) == 1 |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.only_browser("chromium") |
| 47 | +def test_should_be_able_to_detach_session(page): |
| 48 | + client = page.context.new_cdp_session(page) |
| 49 | + client.send("Runtime.enable") |
| 50 | + eval_response = client.send( |
| 51 | + "Runtime.evaluate", {"expression": "1 + 2", "returnByValue": True} |
| 52 | + ) |
| 53 | + assert eval_response["result"]["value"] == 3 |
| 54 | + client.detach() |
| 55 | + with pytest.raises(Error) as exc_info: |
| 56 | + client.send("Runtime.evaluate", {"expression": "3 + 1", "returnByValue": True}) |
| 57 | + assert "Target page, context or browser has been closed" in exc_info.value.message |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.only_browser("chromium") |
| 61 | +def test_should_not_break_page_close(browser): |
| 62 | + context = browser.new_context() |
| 63 | + page = context.new_page() |
| 64 | + session = page.context.new_cdp_session(page) |
| 65 | + session.detach() |
| 66 | + page.close() |
| 67 | + context.close() |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.only_browser("chromium") |
| 71 | +def test_should_detach_when_page_closes(browser): |
| 72 | + context = browser.new_context() |
| 73 | + page = context.new_page() |
| 74 | + session = context.new_cdp_session(page) |
| 75 | + page.close() |
| 76 | + with pytest.raises(Error): |
| 77 | + session.detach() |
| 78 | + context.close() |
0 commit comments