ai 开始测试

This commit is contained in:
2025-08-29 20:48:33 +08:00
parent 3fcd72bfd5
commit b75c517488
8 changed files with 213 additions and 126 deletions

View File

@@ -49,7 +49,7 @@ class ScriptManager():
time.sleep(1)
ControlUtils.openTikTok(session, udid)
time.sleep(3)
LogManager.info("养号重启tiktok")
LogManager.info("养号重启tiktok", udid)
AiUtils.makeUdidDir(udid)
# ========= 主循环 =========
@@ -183,7 +183,6 @@ class ScriptManager():
if live_button.exists:
continue
# 下滑一下
client.swipe_up()
@@ -479,6 +478,7 @@ class ScriptManager():
# 关注打招呼以及回复主播消息
def greetNewFollowers(self, udid, needReply, event):
client = wda.USBClient(udid)
session = client.session()
print(f"是否要自动回复消息:{needReply}")
@@ -536,24 +536,8 @@ class ScriptManager():
session.appium_settings({"snapshotMaxDepth": 23})
try:
# 查找“关注”按钮
follow_btn = session.xpath(
'//XCUIElementTypeButton[@name="关注" or @name="Follow" or @name="已关注" or @name="Following"]').get(
timeout=5)
# 获取按钮位置和大小
rect = follow_btn.bounds
left_x = max(1, rect.x - 20) # 向左偏移 20px确保不会点到屏幕外
center_y = rect.y + rect.height // 2
# 打印调试信息
print(f"关注按钮位置 x={rect.x}, y={rect.y}, width={rect.width}, height={rect.height}")
print(f"即将点击的位置 x={left_x}, y={center_y}")
# 点击关注按钮左侧区域(通常是头像)
session.tap(left_x, center_y)
# 点击关注按钮
ControlUtils.clickFollow(session, aid)
except wda.WDAElementNotFoundError:
# 如果没有“关注”按钮,则不点击
print("未找到‘关注’按钮,跳过点击。")
@@ -596,6 +580,7 @@ class ScriptManager():
time.sleep(2)
# 查找add图标
r = ControlUtils.clickLike(session, udid)
# 点赞成功。
# if r == True:
@@ -628,8 +613,8 @@ class ScriptManager():
followButton.click()
else:
LogManager.info("没找到关注按钮", udid)
removeModelFromAnchorList(anchor)
goBack(3)
session.appium_settings({"snapshotMaxDepth": 15})
continue
time.sleep(2)
@@ -641,8 +626,8 @@ class ScriptManager():
msgButton.click()
else:
print("没有识别出发消息按钮")
removeModelFromAnchorList(anchor)
goBack(3)
session.appium_settings({"snapshotMaxDepth": 15})
continue
time.sleep(3)
@@ -666,6 +651,8 @@ class ScriptManager():
# 接着下一个主播
# removeModelFromAnchorList(anchor)
session.appium_settings({"snapshotMaxDepth": 15})
goBack(4)
else:
@@ -697,6 +684,7 @@ class ScriptManager():
time.sleep(3)
print("重新创建wda会话 防止wda会话失效")
client = wda.USBClient(udid)
session = client.session()
@@ -738,9 +726,8 @@ class ScriptManager():
# 调整节点的深度为 7
session.appium_settings({"snapshotMaxDepth": 7})
el = session(xpath='//XCUIElementTypeButton[@name="a11y_vo_inbox"]')
print(111111111111111111111111)
el = session(xpath='//XCUIElementTypeButton[@name="a11y_vo_inbox"]')
# 如果收件箱有消息 则进行点击
if el.exists:
@@ -751,7 +738,6 @@ class ScriptManager():
session.appium_settings({"snapshotMaxDepth": 25})
time.sleep(3)
while True:
info_count = 0
# 创建新的会话
@@ -776,7 +762,6 @@ class ScriptManager():
LogManager.error(f"当前页面不再收件箱页面,重启", udid)
raise Exception("当前页面不再收件箱页面,重启")
print(3333333333333333333333333333333)
m = re.search(r'(\d+)', el.label) # 抓到的第一个数字串
count = int(m.group(1)) if m else 0
@@ -818,7 +803,70 @@ class ScriptManager():
']//XCUIElementTypeStaticText[@value and translate(@value,"0123456789","")=""]'
)
print(44444444444444444444444444444444)
try:
# 如果 2 秒内找不到,会抛异常
user_text = session.xpath(xp_badge_numeric).get(timeout=2.0)
val = (user_text.info.get("value") or
user_text.info.get("label") or
user_text.info.get("name"))
LogManager.info(f"用户未读数量:{val}", udid)
except Exception:
LogManager.warning("当前屏幕没有找到 用户 未读徽标数字", udid)
print("当前屏幕没有找到 用户消息 未读徽标数字", udid)
user_text = None
info_count += 1
if user_text:
user_text.tap()
time.sleep(3)
xml = session.source()
msgs = AiUtils.extract_messages_from_xml(xml)
# 检测出对方发的最后一条信息
last_msg_text = next(item['text'] for item in reversed(msgs) if item['type'] == 'msg')
# 向ai发送信息
# 获取主播的名称
anchor_name = AiUtils.get_navbar_anchor_name(session)
# 找到输入框
sel = session.xpath("//TextView")
if anchor_name not in anchorWithSession:
# 如果是第一次发消息(没有sessionId的情况)
response = Requester.chatToAi({"msg": last_msg_text})
aiResult = response['result']
sessionId = response['session_id']
anchorWithSession[anchor_name] = sessionId
# 找到输入框输入ai返回出来的消息
if sel.exists:
sel.click() # 聚焦
time.sleep(1)
sel.clear_text()
sel.set_text(aiResult + "\n")
else:
LogManager.error("找不到输入框,重启", udid)
raise Exception("找不到输入框,重启")
else:
# 如果不是第一次发消息证明存储的有sessionId
sessionId = anchorWithSession[anchor_name]
response = Requester.chatToAi({"msg": last_msg_text, "sid": sessionId})
aiResult = response['result']
if sel.exists:
sel.click() # 聚焦
time.sleep(1)
sel.clear_text()
sel.set_text(aiResult + "\n")
LogManager.info(f"存储的sessionId:{anchorWithSession}", udid)
time.sleep(1)
# 返回
ControlUtils.clickBack(session)
# 重新回到收件箱页面后,强制刷新节点
session.appium_settings({"snapshotMaxDepth": 25})
time.sleep(1)
try:
# 如果 2 秒内找不到,会抛异常
@@ -894,71 +942,6 @@ class ScriptManager():
badge_text = None
info_count += 1
try:
# 如果 2 秒内找不到,会抛异常
user_text = session.xpath(xp_badge_numeric).get(timeout=2.0)
val = (user_text.info.get("value") or
user_text.info.get("label") or
user_text.info.get("name"))
LogManager.info(f"用户未读数量:{val}", udid)
except Exception:
LogManager.warning("当前屏幕没有找到 用户 未读徽标数字", udid)
print("当前屏幕没有找到 用户消息 未读徽标数字", udid)
user_text = None
info_count += 1
if user_text:
user_text.tap()
time.sleep(3)
xml = session.source()
msgs = AiUtils.extract_messages_from_xml(xml)
# 检测出对方发的最后一条信息
last_msg_text = next(item['text'] for item in reversed(msgs) if item['type'] == 'msg')
# 向ai发送信息
# 获取主播的名称
anchor_name = AiUtils.get_navbar_anchor_name(session)
# 找到输入框
sel = session.xpath("//TextView")
if anchor_name not in anchorWithSession:
# 如果是第一次发消息(没有sessionId的情况)
response = Requester.chatToAi({"msg": last_msg_text})
aiResult = response['result']
sessionId = response['session_id']
anchorWithSession[anchor_name] = sessionId
# 找到输入框输入ai返回出来的消息
if sel.exists:
sel.click() # 聚焦
time.sleep(1)
sel.clear_text()
sel.set_text(aiResult + "\n")
else:
LogManager.error("找不到输入框,重启", udid)
raise Exception("找不到输入框,重启")
else:
# 如果不是第一次发消息证明存储的有sessionId
sessionId = anchorWithSession[anchor_name]
response = Requester.chatToAi({"msg": last_msg_text, "sid": sessionId})
aiResult = response['result']
if sel.exists:
sel.click() # 聚焦
time.sleep(1)
sel.clear_text()
sel.set_text(aiResult + "\n")
LogManager.info(f"存储的sessionId:{anchorWithSession}", udid)
time.sleep(1)
# 返回
ControlUtils.clickBack(session)
# 重新回到收件箱页面后,强制刷新节点
session.appium_settings({"snapshotMaxDepth": 25})
time.sleep(1)
# 双击收件箱 定位到消息的位置
if info_count == 5:
r = el.bounds # 可能是命名属性,也可能是 tuple
@@ -966,9 +949,9 @@ class ScriptManager():
cy = int((r.y + r.height / 2) if hasattr(r, "y") else (r[1] + r[3] / 2))
session.double_tap(cx, cy) # 可能抛异常:方法不存在
LogManager.info(f"双击收件箱 定位到信息", udid)
else:
return
else:
LogManager.error(f"检测不到收件箱", udid)
raise Exception("当前页面找不到收件箱,重启")