import tidevice from wda import Client from Entity.Variables import tikTokApp 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(tikTokApp) # 关闭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: x, y = AiUtils.findImageInScreen("back") if x > 0: r = session.swipe_right() if r.status == 0: return True else: return False else: print("本页面无法返回") 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: print("点赞了") session.click(x // scale, y // scale + 50) return True else: print("没有找到目标") return False # 点击评论 # @classmethod # def clickComment(cls, session: Client): # scale = session.scale # x, y = AiUtils.findImageInScreen("add") # print(x,y) # if x > -1: # print("找到目标了") # r = session.click(x // scale, y // scale + 120) # if r.status == 0: # return True # else: # return False # else: # print("没有找到目标") # 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 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]') if videoCell.exists: videoCell.click() # 点击视频 return True else: return False # 获取关注按钮 @classmethod def clickFollowButton(cls, session: Client): followButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]/Button[1]") if followButton.exists: print("找到了") followButton.click() return True else: print("没找到") return False # 查找发消息按钮 @classmethod def clickSendMesageButton(cls, session: Client): msgButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]") if msgButton.exists: print("找到了") print(msgButton.name) msgButton.click() return True else: print("没找到") return False