Skip to content

Commit 2ca17ac

Browse files
authored
fix(cdp): fix cdp session, add tests (microsoft#777)
1 parent 201c721 commit 2ca17ac

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

playwright/_impl/_cdp_session.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from playwright._impl._connection import ChannelOwner
1818
from playwright._impl._helper import locals_to_params
19-
from playwright._impl._js_handle import parse_result
2019

2120

2221
class CDPSession(ChannelOwner):
@@ -27,11 +26,10 @@ def __init__(
2726
self._channel.on("event", lambda params: self._on_event(params))
2827

2928
def _on_event(self, params: Any) -> None:
30-
self.emit(params["method"], parse_result(params["params"]))
29+
self.emit(params["method"], params["params"])
3130

3231
async def send(self, method: str, params: Dict = None) -> Dict:
33-
result = await self._channel.send("send", locals_to_params(locals()))
34-
return parse_result(result)
32+
return await self._channel.send("send", locals_to_params(locals()))
3533

3634
async def detach(self) -> None:
3735
await self._channel.send("detach")

tests/async/test_cdp_session.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import asyncio
16-
1715
import pytest
1816

1917
from playwright.async_api import Error
@@ -22,17 +20,21 @@
2220
@pytest.mark.only_browser("chromium")
2321
async def test_should_work(page):
2422
client = await page.context.new_cdp_session(page)
25-
26-
await asyncio.gather(
27-
client.send("Runtime.enable"),
28-
client.send("Runtime.evaluate", {"expression": "window.foo = 'bar'"}),
23+
events = []
24+
client.on("Runtime.consoleAPICalled", lambda params: events.append(params))
25+
await client.send("Runtime.enable")
26+
result = await client.send(
27+
"Runtime.evaluate",
28+
{"expression": "window.foo = 'bar'; console.log('log'); 'result'"},
2929
)
30+
assert result == {"result": {"type": "string", "value": "result"}}
3031
foo = await page.evaluate("() => window.foo")
3132
assert foo == "bar"
33+
assert events[0]["args"][0]["value"] == "log"
3234

3335

3436
@pytest.mark.only_browser("chromium")
35-
async def test_should_send_events(page, server):
37+
async def test_should_receive_events(page, server):
3638
client = await page.context.new_cdp_session(page)
3739
await client.send("Network.enable")
3840
events = []

tests/sync/test_cdp_session.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy