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()
|
||||
@@ -442,6 +442,7 @@ class EditorLoginTests(unittest.TestCase):
|
||||
self.assertEqual("127.0.0.1:9222", cdp.cdp_host)
|
||||
find_product_tab.assert_called_once_with("51100639510", host="127.0.0.1:9222")
|
||||
create_tab_info.assert_not_called()
|
||||
self.assertIn(("Page.bringToFront", {}), fake.sent)
|
||||
self.assertIn(
|
||||
(
|
||||
"Page.navigate",
|
||||
@@ -469,7 +470,52 @@ class EditorLoginTests(unittest.TestCase):
|
||||
self.assertEqual("target-new", cdp.target_id)
|
||||
self.assertTrue(cdp.created_by_app)
|
||||
self.assertEqual("127.0.0.1:9223", cdp.cdp_host)
|
||||
create_tab_info.assert_called_once()
|
||||
create_tab_info.assert_called_once_with(
|
||||
(
|
||||
"https://seller.shopee.tw/portal/product/51100639511"
|
||||
"?pageEntry=product_list&ignore-html-cache=1"
|
||||
),
|
||||
host="127.0.0.1:9223",
|
||||
background=False,
|
||||
)
|
||||
|
||||
def test_open_product_can_skip_bring_to_front_for_collection(self):
|
||||
fake = FakeProductCDP("ws-new")
|
||||
with mock.patch("app.editor.find_product_tab", return_value=None), mock.patch(
|
||||
"app.editor.create_tab_info",
|
||||
return_value={"id": "target-new", "webSocketDebuggerUrl": "ws-new"},
|
||||
) as create_tab_info, mock.patch(
|
||||
"app.editor.CDP",
|
||||
return_value=fake,
|
||||
), mock.patch("app.editor._ensure_page_domains"), mock.patch("app.editor._wait_ready"):
|
||||
cdp = editor.open_product(
|
||||
{"debug_port": 9223},
|
||||
"51100639511",
|
||||
bring_to_front=False,
|
||||
)
|
||||
|
||||
self.assertIs(fake, cdp)
|
||||
self.assertNotIn(("Page.bringToFront", {}), fake.sent)
|
||||
self.assertIn(
|
||||
(
|
||||
"Page.navigate",
|
||||
{
|
||||
"url": (
|
||||
"https://seller.shopee.tw/portal/product/51100639511"
|
||||
"?pageEntry=product_list&ignore-html-cache=1"
|
||||
)
|
||||
},
|
||||
),
|
||||
fake.sent,
|
||||
)
|
||||
create_tab_info.assert_called_once_with(
|
||||
(
|
||||
"https://seller.shopee.tw/portal/product/51100639511"
|
||||
"?pageEntry=product_list&ignore-html-cache=1"
|
||||
),
|
||||
host="127.0.0.1:9223",
|
||||
background=True,
|
||||
)
|
||||
|
||||
def test_open_product_installs_toast_observer(self):
|
||||
fake = FakeProductCDP("ws-new", ready=True)
|
||||
@@ -560,7 +606,7 @@ class EditorLoginTests(unittest.TestCase):
|
||||
cdp.created_by_app = True
|
||||
cdp.cdp_host = "127.0.0.1:9222"
|
||||
|
||||
with mock.patch("app.editor.open_product", return_value=cdp), mock.patch(
|
||||
with mock.patch("app.editor.open_product", return_value=cdp) as open_product, mock.patch(
|
||||
"app.editor.read_title",
|
||||
return_value="旧标题",
|
||||
), mock.patch(
|
||||
@@ -576,6 +622,12 @@ class EditorLoginTests(unittest.TestCase):
|
||||
)
|
||||
|
||||
self.assertEqual("旧标题", result["old_title"])
|
||||
open_product.assert_called_once_with(
|
||||
{"debug_port": 9222},
|
||||
"51100639510",
|
||||
on_step=None,
|
||||
bring_to_front=False,
|
||||
)
|
||||
self.assertTrue(cdp.closed)
|
||||
close_tab.assert_called_once_with("target-new", host="127.0.0.1:9222")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user