watch.py
6.64 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
"""tool.bat 菜单 12:在门店电脑桌面上观看截图登录方式 ui 的全过程(有界面的浏览器,服务照常在后台运行)。
1 逐步确认(推荐):打开 IAM 登录页 → 点用户名框 → 逐字输入账号 → 点密码框 → 逐字输入密码 → 点「登录」(唯一一次提交)
→ 回到 DMS → 打开采购订单管理(列表)→ 截图 → 打开采购订单详情 → 截图。每一步之前红框标出目标,
页面右上角面板上点「确认执行」才做,点「取消」就停。
2 自动慢放:同样的步骤,每步停 2 秒自动执行,红框照样显示。
3 只定位不操作:只打开登录页,依次红框标出三个目标(用户名框 / 密码框 / 登录按钮),显示选择器、坐标、大小、
是否被遮挡;不输入、不点击,零风险检查定位有没有偏。
说明:ui 方式只有「登录」这一段是模拟人工(真实鼠标点击 + 逐字键盘输入,页面收到的是浏览器原生输入事件);
登录后打开采购单页面是直接打开网址,不是点菜单。用独立的浏览器缓存目录 run\\chrome_profile_demo,不影响服务的截图浏览器。
"""
import glob
import os
import shutil
from .. import actions
from ..browser import screenshot, ui_login
from ..browser.confirm import ConfirmPanel
from ..infra.audit import AuditTrail
from ..infra.config import ConfigError, require_account
from ..infra.localstore import RunDir
INTRO = """在这台电脑的桌面上打开一个有界面的浏览器,完整演示截图登录方式 ui:
登录页 → 点用户名框 → 逐字输入账号 → 点密码框 → 逐字输入密码 → 点「登录」(只点一次)
→ 回到 DMS → 打开「采购订单管理」→ 截图 → 打开最新一张采购单的详情 → 截图
每一步之前都会用红框标出要点的位置。Windows 服务在后台照常运行,不用停。
1. 逐步确认(推荐):每一步在浏览器右上角面板上点「确认执行」才做,点「取消」就停
2. 自动慢放:每步停 2 秒自动执行(红框照样显示)
3. 只定位不操作:只打开登录页,红框依次标出用户名框 / 密码框 / 登录按钮,不输入、不点击"""
def _watch_cfg(cfg):
w = cfg.copy()
w.strategies = ["ui"]
w.ui_confirm = True # 有界面
w.headless = False
w.chrome_profile = os.path.join(cfg.run_dir, "chrome_profile_demo")
shutil.rmtree(w.chrome_profile, ignore_errors=True)
return w
def run(app, ask, say):
cfg = require_account(app.cfg())
if not ui_login.interactive_desktop():
raise ConfigError("需要在门店电脑的桌面上运行(双击 tool.bat);Windows 服务在后台会话里看不到浏览器窗口")
say(INTRO)
choice = ask("请输入 1 / 2 / 3,直接回车返回:").strip()
if choice not in ("1", "2", "3"):
return 0
w = _watch_cfg(cfg)
if choice == "3":
return locate_only(w, ask, say)
if ask("将用账号 %s 真实登录一次 DMS(只点 1 次登录,算一次密码登录;DMS 若只允许一个会话,服务下次任务会自动重新登录)。"
"输入 Y 继续:" % cfg.username).strip().upper() != "Y":
return 0
_, sess = app.cfg_sess()
screenshot.WATCH.update(mode="confirm" if choice == "1" else "auto", panel=None,
hold=lambda: ask("浏览器先留着给你看,看完按回车关闭浏览器……"))
try:
r = actions.screenshot_po(w, sess)
say("截图完成:%s" % ",".join(r["files"]))
out = r["dir"]
rc = 0
except Exception as e: # noqa: BLE001
say("[停止] %s" % e)
out = _latest_dir(cfg.data_dir, "_screenshot")
rc = 2
finally:
screenshot.WATCH.update(mode="", panel=None, hold=None)
_open(out, say)
return rc
def locate_only(w, ask, say):
rd = RunDir(w.data_dir, "ui_locate")
audit = AuditTrail(rd.path, title="ui 登录定位检查(只定位,不输入、不点击)")
chrome = relay = None
ok = False
try:
chrome, relay = screenshot.open_browser(w, audit, None, visible=True)
page = ui_login.open_login_page(chrome, w, audit)
say("已打开 IAM 登录页:%s" % page)
panel = ConfirmPanel(chrome, 600, total=len(ui_login.TARGETS))
results = ui_login.locate_targets(chrome)
for t in results:
r = t["rect"]
if not r:
detail = "页面上找不到 %s" % t["sel"]
else:
detail = "选择器 %s(%s) 中心坐标 (%.0f, %.0f) 大小 %.0f×%.0f %s" % (
t["sel"], r.get("tag"), r["cx"], r["cy"], r["w"], r["h"],
"可点击(中心点没有被遮挡)" if t["hit"] else ("不可见" if not t["visible"] else "中心点被别的元素挡住(%s)" % (t.get("blocker") or "未知元素")))
say(" %s:%s" % (t["what"], detail))
rect = {k: r[k] for k in ("x", "y", "w", "h")} if r else None
panel.show("定位:%s" % t["what"], detail, rect, "只标出位置,不输入、不点击", 3)
shot = "locate_%s.png" % t["sel"].strip("#")
chrome.screenshot(rd.file(shot))
rd.image("%s:%s" % (t["what"], detail), shot)
audit.event("ui.locate", "%s:%s" % (t["what"], detail), level="info" if t["hit"] else "warn", shot=shot)
ok = all(t["hit"] for t in results)
msg = "三个目标都定位正确" if ok else "有目标定位不正常,正式登录时程序会停下、不会点错(详见控制台)"
say(msg)
panel.done("定位检查结束", msg)
ask("看完按回车关闭浏览器……")
finally:
if relay:
relay.stop()
if chrome:
chrome.close()
audit.finish(ok)
rd.files += ["audit.html", "audit.jsonl"]
rd.finish(ok, {"message": "ui 登录定位检查" + ("正常" if ok else "有异常")})
_open(rd.path, say)
return 0 if ok else 2
def _latest_dir(data_dir, suffix):
dirs = sorted(glob.glob(os.path.join(data_dir, "*", "*" + suffix)))
return dirs[-1] if dirs else None
def _open(path, say):
if not path:
return
for name in ("audit.html", "index.html"):
f = os.path.join(path, name)
if os.path.exists(f):
say("每一步的时间线和截图:%s" % f)
try:
os.startfile(f) # noqa: S606(Windows)
except (AttributeError, OSError):
pass
return