196 lines
6.6 KiB
Python
196 lines
6.6 KiB
Python
import random
|
||
import re
|
||
import time
|
||
|
||
import tidevice
|
||
import wda
|
||
from wda import Client
|
||
from Utils.AiUtils import AiUtils
|
||
from Utils.LogManager import LogManager
|
||
|
||
|
||
# 页面控制工具类
|
||
class ControlUtils(object):
|
||
|
||
# 获取设备上的app列表
|
||
@classmethod
|
||
def getDeviceAppList(self, udid):
|
||
device = tidevice.Device(udid)
|
||
# 获取已安装的应用列表
|
||
apps = []
|
||
for app in device.installation.iter_installed():
|
||
apps.append({
|
||
"name": app.get("CFBundleDisplayName", "Unknown"),
|
||
"bundleId": app.get("CFBundleIdentifier", "Unknown"),
|
||
"version": app.get("CFBundleShortVersionString", "Unknown"),
|
||
"path": app.get("Path", "Unknown")
|
||
})
|
||
|
||
# 筛选非系统级应用(过滤掉以 com.apple 开头的系统应用)
|
||
noSystemApps = [app for app in apps if not app["bundleId"].startswith("com.apple")]
|
||
return noSystemApps
|
||
|
||
# 打开Tik Tok
|
||
@classmethod
|
||
def openTikTok(cls, session: Client, udid):
|
||
apps = cls.getDeviceAppList(udid)
|
||
tk = ""
|
||
for app in apps:
|
||
if app.get("name", "") == "TikTok":
|
||
tk = app.get("bundleId", "")
|
||
|
||
currentApp = session.app_current()
|
||
if currentApp != tk:
|
||
session.app_start(tk)
|
||
|
||
# 关闭Tik Tok
|
||
@classmethod
|
||
def closeTikTok(cls, session: Client, udid):
|
||
apps = cls.getDeviceAppList(udid)
|
||
tk = ""
|
||
for app in apps:
|
||
if app.get("name", "") == "TikTok":
|
||
tk = app.get("bundleId", "")
|
||
session.app_stop(tk)
|
||
|
||
# 返回
|
||
@classmethod
|
||
def clickBack(cls, session: Client):
|
||
try:
|
||
back = session.xpath(
|
||
"//*[@label='返回']"
|
||
" | "
|
||
"//*[@label='返回上一屏幕']"
|
||
" | "
|
||
"//XCUIElementTypeButton[@visible='true' and @name='TTKProfileNavBarBaseItemComponent' and @label='IconChevronLeftOffsetLTR']"
|
||
)
|
||
|
||
if back.exists:
|
||
back.click()
|
||
return True
|
||
elif session.xpath("//*[@name='nav_bar_start_back']").exists:
|
||
back = session.xpath("//*[@name='nav_bar_start_back']")
|
||
back.click()
|
||
return True
|
||
elif session.xpath(
|
||
"//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]").exists:
|
||
back = session.xpath(
|
||
"//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]")
|
||
back.click()
|
||
return True
|
||
else:
|
||
return False
|
||
except Exception as e:
|
||
print(e)
|
||
return False
|
||
|
||
# 点赞
|
||
@classmethod
|
||
def clickLike(cls, session: Client, udid):
|
||
scale = session.scale
|
||
x, y = AiUtils.findImageInScreen("add", udid)
|
||
print(x, y)
|
||
if x > -1:
|
||
LogManager.info("点赞了", udid)
|
||
session.click(x // scale, y // scale + 50)
|
||
return True
|
||
else:
|
||
LogManager.info("没有找到目标", udid)
|
||
return False
|
||
|
||
# 点击搜索
|
||
@classmethod
|
||
def clickSearch(cls, session: Client):
|
||
obj = session.xpath("//*[@name='搜索']")
|
||
try:
|
||
if obj.exists:
|
||
obj.click()
|
||
return True
|
||
except Exception as e:
|
||
print(e)
|
||
return False
|
||
|
||
# 点击收件箱按钮
|
||
@classmethod
|
||
def clickMsgBox(cls, session: Client):
|
||
box = session.xpath("//XCUIElementTypeButton[name='a11y_vo_inbox']")
|
||
if box.exists:
|
||
box.click()
|
||
return True
|
||
else:
|
||
return False
|
||
|
||
# 获取主播详情页的第一个视频
|
||
@classmethod
|
||
def clickFirstVideoFromDetailPage(cls, session: Client):
|
||
# videoCell = session.xpath(
|
||
# '//Window/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[1]/ScrollView[1]/Other[1]/CollectionView[1]/Cell[2]')
|
||
|
||
videoCell = session.xpath(
|
||
'(//XCUIElementTypeCollectionView//XCUIElementTypeCell[.//XCUIElementTypeImage[@name="profile_video"]])[1]')
|
||
|
||
tab = session.xpath(
|
||
'//XCUIElementTypeButton[@name="TTKProfileTabVideoButton_0" or contains(@label,"作品") or contains(@name,"作品")]'
|
||
).get(timeout=5) # 某些版本 tab.value 可能就是数量;或者 tab.label 类似 “作品 7”
|
||
m = re.search(r"\d+", tab.label)
|
||
|
||
num = 0
|
||
|
||
if m:
|
||
# 判断当前的作品的数量
|
||
num = int(m.group())
|
||
print("作品数量为:", num)
|
||
|
||
if videoCell.exists:
|
||
videoCell.click()
|
||
# 点击视频
|
||
print("找到主页的第一个视频")
|
||
return True, num
|
||
else:
|
||
print("没有找到主页的第一个视频")
|
||
return False, num
|
||
|
||
@classmethod
|
||
def clickFollow(cls, session, aid):
|
||
# 1) 含“关注/已关注/Follow/Following”的首个 cell
|
||
cell_xpath = (
|
||
'(//XCUIElementTypeCollectionView[@name="TTKSearchCollectionComponent"]'
|
||
'//XCUIElementTypeCell[.//XCUIElementTypeButton[@name="关注" or @name="Follow" or @name="已关注" or @name="Following"]])[1]'
|
||
)
|
||
cell = session.xpath(cell_xpath).get(timeout=5)
|
||
|
||
# 2) 先试“用户信息 Button”(label/name 里包含 aid)
|
||
profile_btn_xpath = (
|
||
f'{cell_xpath}//XCUIElementTypeButton[contains(@label, "{aid}") or contains(@name, "{aid}")]'
|
||
)
|
||
|
||
try:
|
||
profile_btn = session.xpath(profile_btn_xpath).get(timeout=3)
|
||
profile_btn.click()
|
||
except wda.WDAElementNotFoundError:
|
||
# 3) 兜底:用“关注”按钮做锚点,向左偏移点击头像/用户名区域
|
||
follow_btn_xpath = (
|
||
f'{cell_xpath}//XCUIElementTypeButton[@name="关注" or @name="Follow" or @name="已关注" or @name="Following"]'
|
||
)
|
||
follow_btn = session.xpath(follow_btn_xpath).get(timeout=5)
|
||
rect = follow_btn.bounds
|
||
left_x = max(1, rect.x - 20)
|
||
center_y = rect.y + rect.height // 2
|
||
session.tap(left_x, center_y)
|
||
|
||
|
||
# 点击一个随机范围
|
||
@classmethod
|
||
def tap_mini_cluster(cls, center_x: int, center_y: int, session, points=5, duration_ms=60):
|
||
pass
|
||
|
||
# 检测五分钟前和当前的状态是否相同
|
||
# @classmethod
|
||
# def compareCurrentWithPreviousState(cls,xml):
|
||
|
||
|
||
|
||
|
||
|
||
|