forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cdp_session.py
78 lines (64 loc) · 2.53 KB
/
test_cdp_session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import pytest
from playwright import Error
@pytest.mark.only_browser("chromium")
async def test_should_work(page):
client = await page.context.newCDPSession(page)
await asyncio.gather(
client.send("Runtime.enable"),
client.send("Runtime.evaluate", {"expression": "window.foo = 'bar'"}),
)
foo = await page.evaluate("() => window.foo")
assert foo == "bar"
@pytest.mark.only_browser("chromium")
async def test_should_send_events(page, server):
client = await page.context.newCDPSession(page)
await client.send("Network.enable")
events = []
client.on("Network.requestWillBeSent", lambda event: events.append(event))
await page.goto(server.EMPTY_PAGE)
assert len(events) == 1
@pytest.mark.only_browser("chromium")
async def test_should_be_able_to_detach_session(page):
client = await page.context.newCDPSession(page)
await client.send("Runtime.enable")
eval_response = await client.send(
"Runtime.evaluate", {"expression": "1 + 2", "returnByValue": True}
)
assert eval_response["result"]["value"] == 3
await client.detach()
with pytest.raises(Error) as exc_info:
await client.send(
"Runtime.evaluate", {"expression": "3 + 1", "returnByValue": True}
)
assert "Target page, context or browser has been closed" in exc_info.value.message
@pytest.mark.only_browser("chromium")
async def test_should_not_break_page_close(browser):
context = await browser.newContext()
page = await context.newPage()
session = await page.context.newCDPSession(page)
await session.detach()
await page.close()
await context.close()
@pytest.mark.only_browser("chromium")
async def test_should_detach_when_page_closes(browser):
context = await browser.newContext()
page = await context.newPage()
session = await context.newCDPSession(page)
await page.close()
with pytest.raises(Error):
await session.detach()
await context.close()