fix: avoid focusing chrome during collection
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
from _helpers import REPO_ROOT
|
||||
|
||||
if str(REPO_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(REPO_ROOT))
|
||||
|
||||
from app import cdp
|
||||
|
||||
|
||||
class FakeBrowserCDP:
|
||||
sent = []
|
||||
fail_first_create = False
|
||||
|
||||
def __init__(self, ws_url):
|
||||
self.ws_url = ws_url
|
||||
self.closed = False
|
||||
|
||||
def send(self, method, params=None):
|
||||
self.sent.append((method, params or {}))
|
||||
if method == "Target.createTarget":
|
||||
if self.fail_first_create:
|
||||
type(self).fail_first_create = False
|
||||
raise RuntimeError("Target.createTarget: Invalid parameters")
|
||||
return {"targetId": "target-new"}
|
||||
return {}
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
|
||||
class CdpTests(unittest.TestCase):
|
||||
def test_create_tab_info_can_request_background_target(self):
|
||||
FakeBrowserCDP.sent = []
|
||||
FakeBrowserCDP.fail_first_create = False
|
||||
responses = [
|
||||
{"webSocketDebuggerUrl": "ws-browser"},
|
||||
[
|
||||
{
|
||||
"id": "target-new",
|
||||
"type": "page",
|
||||
"url": "https://seller.shopee.tw/portal/product/1",
|
||||
"webSocketDebuggerUrl": "ws-page",
|
||||
}
|
||||
],
|
||||
]
|
||||
|
||||
with mock.patch("app.cdp.http_get", side_effect=responses), \
|
||||
mock.patch("app.cdp.CDP", FakeBrowserCDP), \
|
||||
mock.patch("app.cdp.time.sleep"):
|
||||
tab = cdp.create_tab_info(
|
||||
"https://seller.shopee.tw/portal/product/1",
|
||||
host="127.0.0.1:9222",
|
||||
background=True,
|
||||
)
|
||||
|
||||
self.assertEqual("ws-page", tab["webSocketDebuggerUrl"])
|
||||
self.assertEqual(
|
||||
[
|
||||
(
|
||||
"Target.createTarget",
|
||||
{
|
||||
"url": "https://seller.shopee.tw/portal/product/1",
|
||||
"background": True,
|
||||
},
|
||||
)
|
||||
],
|
||||
FakeBrowserCDP.sent,
|
||||
)
|
||||
|
||||
def test_create_tab_info_keeps_foreground_default_params(self):
|
||||
FakeBrowserCDP.sent = []
|
||||
FakeBrowserCDP.fail_first_create = False
|
||||
responses = [
|
||||
{"webSocketDebuggerUrl": "ws-browser"},
|
||||
[
|
||||
{
|
||||
"id": "target-new",
|
||||
"type": "page",
|
||||
"url": "https://seller.shopee.tw/portal/product/1",
|
||||
"webSocketDebuggerUrl": "ws-page",
|
||||
}
|
||||
],
|
||||
]
|
||||
|
||||
with mock.patch("app.cdp.http_get", side_effect=responses), \
|
||||
mock.patch("app.cdp.CDP", FakeBrowserCDP), \
|
||||
mock.patch("app.cdp.time.sleep"):
|
||||
cdp.create_tab_info(
|
||||
"https://seller.shopee.tw/portal/product/1",
|
||||
host="127.0.0.1:9222",
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[
|
||||
(
|
||||
"Target.createTarget",
|
||||
{"url": "https://seller.shopee.tw/portal/product/1"},
|
||||
)
|
||||
],
|
||||
FakeBrowserCDP.sent,
|
||||
)
|
||||
|
||||
def test_create_tab_info_falls_back_when_background_is_rejected(self):
|
||||
FakeBrowserCDP.sent = []
|
||||
FakeBrowserCDP.fail_first_create = True
|
||||
responses = [
|
||||
{"webSocketDebuggerUrl": "ws-browser"},
|
||||
[
|
||||
{
|
||||
"id": "target-new",
|
||||
"type": "page",
|
||||
"url": "https://seller.shopee.tw/portal/product/1",
|
||||
"webSocketDebuggerUrl": "ws-page",
|
||||
}
|
||||
],
|
||||
]
|
||||
|
||||
with mock.patch("app.cdp.http_get", side_effect=responses), \
|
||||
mock.patch("app.cdp.CDP", FakeBrowserCDP), \
|
||||
mock.patch("app.cdp.time.sleep"):
|
||||
cdp.create_tab_info(
|
||||
"https://seller.shopee.tw/portal/product/1",
|
||||
host="127.0.0.1:9222",
|
||||
background=True,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[
|
||||
(
|
||||
"Target.createTarget",
|
||||
{
|
||||
"url": "https://seller.shopee.tw/portal/product/1",
|
||||
"background": True,
|
||||
},
|
||||
),
|
||||
(
|
||||
"Target.createTarget",
|
||||
{"url": "https://seller.shopee.tw/portal/product/1"},
|
||||
),
|
||||
],
|
||||
FakeBrowserCDP.sent,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user