APP休眠拉流脚本

This commit is contained in:
2025-10-30 15:32:01 +08:00
commit a76e84be77

290
xing.py Normal file
View File

@@ -0,0 +1,290 @@
import time
from time import sleep
from appium import webdriver
from PIL import Image #图像处理库,用来截图的
import os
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.ie.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
import cv2
import subprocess #用来启动和停止adb logcat
#-----------------------------------------------------
#-----------------------------------------------------
#-----------------------------------------------------休眠拉流前板,如果前板超时未出图后,拉流后板直播画面------------------------
#-----------------------------------------------------
#-----------------------------------------------------
#启动adb logcat
#logcat_process = subprocess.Popen(['adb', 'logcat', '-v', 'time'], stdout=open('./picture/logcat_output.txt', 'w'))
# 配置 Desired Capabilities
desired_caps = {
'platformName': 'Android', # 或者 'iOS'
'platformVersion': '10', # 安卓版本
#'deviceName': '10AD5C3BZA001FG', # 设备ID或名称
#'appPackage': 'com.example.hassecurity',
#'appActivity': 'com.example.hassecurity.MainActivity',
'appPackage': 'cn.com.hichs.hassecurity',
'appActivity': 'com.example.hassecurity.MainActivity',
'automationName': 'UiAutomator2',
# 'app': 'D:/123/app-release.apk', # Flutter APK 的路径
#'automationName': 'Flutter', # 指定使用 Flutter 驱动,appium2.x不在支持了
'noReset': True , # 避免每次打开APP都重置应用状态
#'adbLogCat': True,#启动logcat
}
# 启动 Appium 驱动
i=1
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
while True:
#driver.press_keycode(3)
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
sleep(1)
threshold = 80#设置图像验证阙值
try:
for i in range(1,1000):
sleep(1)
xium_time = time.time()
p = 0
while True:
# 截取整个屏幕的截图并保存
sleep(12)
screenshot_path = r"D:\hassecutrity\hassecutrityAuto\picture\quauping2.png"
sleep(1)
driver.get_screenshot_as_file(screenshot_path)
# 加载截图
image = Image.open(screenshot_path)
# 定义要截取的区域left, top, right, bottom
left = 55 # 左上角 X 坐标
top = 789 # 左上角 Y 坐标
right = 393 # 右下角 X 坐标
bottom = 851 # 右下角 Y 坐标
# 裁剪出指定范围的区域
cropped_image = image.crop((left, top, right, bottom))
# 保存裁剪后的图片
cropped_image_path = r"D:\hassecutrity\hassecutrityAuto\picture\dormancy2.png"
cropped_image.save(cropped_image_path)
#将用来对比的图片进行赋值
reference_image1 = cv2.imread('./picture/dormancy.png')
reference_image2 = cv2.imread('./picture/dormancy2.png')
# 计算图像差异
mse = ((reference_image1 - reference_image2) ** 2).mean()
print(mse)
if 1<int(mse) <34 :
print("未休眠")
elif int(mse) == 0:
sleep(2)
p =p+1
if p==2:
break
elif int(mse) >= 34:
print("卡死")
driver.back()
#driver.press_keycode(3)
sleep(2)
try:
# WebDriverWait(driver, 2, 0.1).until(
# lambda x: driver.find_element(AppiumBy.ID, "android:id/aerr_close"))
# driver.find_element(AppiumBy.ID, "android:id/aerr_close").click()
# sleep(2)
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "玲珑有智").click()
except Exception as e: #需要加一个except防止代码执行内部try报错后再执行finally后会直接进入最外层try-except模块中的except中
print(f"错误: {e}")
try:
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button").click()
except:
driver.tap([(343, 525)])
sleep(1)
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button").click()
finally:
True
finally:
True
#sleep(2)
#driver.tap([(613,395)])
start_time = time.time()
driver.tap([(343, 525)])
sleep(2)
assert driver.find_element(AppiumBy.ACCESSIBILITY_ID,"录制")
exit_flag = False
#开始点击预览加载出图
while True:
# 截取整个屏幕的截图并保存
wucha1_time = time.time()
screenshot_path = r"D:\hassecutrity\hassecutrityAuto\picture\quauping3.png"
driver.get_screenshot_as_file(screenshot_path)
# 加载截图
image = Image.open(screenshot_path)
# 裁剪出直播画面left, top, right, bottom
left = 37 # 左上角 X 坐标
top = 258 # 左上角 Y 坐标
right = 666 # 右下角 X 坐标
bottom = 633 # 右下角 Y 坐标
# 裁剪出指定范围的区域
cropped_image = image.crop((left, top, right, bottom))
# 保存裁剪后的图片
cropped_image_path = r"D:\hassecutrity\hassecutrityAuto\picture\live2.png"
sleep(1)
cropped_image.save(cropped_image_path)
reference_image1 = cv2.imread('./picture/live.png')
reference_image2 = cv2.imread('./picture/live2.png')
# 计算图像差异
mse = ((reference_image1 - reference_image2) ** 2).mean()
print(mse)
#------------------------------------判断前板出图---------------------------------------------------------
if int(mse) != 0:
print("出图成功")
consume_time = time.time() - start_time
wucha2_time = time.time() - wucha1_time
laliu_time = consume_time - wucha2_time
PASS_time = time.strftime('%Y-%m-%d-%H-%M-%S')
screenshot_path_fail = r"./picture/fail/" +"前板直播"+ str(PASS_time) + ".png"
driver.get_screenshot_as_file(screenshot_path_fail)
with open('./picture/loop_live.txt', 'a') as log_file:
# for success in success_times:
log_file.write(
"" + str(i) + "次成功,打开前板直播总时长为:" + str(consume_time) + ",时间点:" + str(PASS_time) +",拉流耗时:"+str(laliu_time)+ "\n")
sleep(5)
#driver.back()
try:
driver.find_element(AppiumBy.CLASS_NAME,"android.widget.Button").click()
except:
driver.tap([(343, 525)])
sleep(1)
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button").click()
#
finally:
break
if time.time()-start_time > 30:
print("出图失败")
Fail_time = time.strftime('%Y-%m-%d-%H-%M-%S')
screenshot_path_fail = r"./picture/fail/" + "前板直播"+str(Fail_time) + ".png"
driver.get_screenshot_as_file(screenshot_path_fail)
with open('./picture/loop_live.txt', 'a') as log_file:
# for success in success_times:
log_file.write(
"" + str(i) + "次前板直播失败" ",时间点:" + str(Fail_time) + "\n")
bank_time = time.time()
#前板失败开始唤醒后板直播
# 点击切换后板按键
try:
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button")
driver.tap([(598, 104)])
except:
driver.tap([(343, 525)])
sleep(1)
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button")
driver.tap([(598, 104)])
finally:
sleep(2)
while True:
# 截取整个屏幕的截图并保存
wucha1_time = time.time()
screenshot_path = r"D:\hassecutrity\hassecutrityAuto\picture\quauping3.png"
driver.get_screenshot_as_file(screenshot_path)
# 加载截图
image = Image.open(screenshot_path)
# 裁剪出直播画面left, top, right, bottom
left = 37 # 左上角 X 坐标
top = 258 # 左上角 Y 坐标
right = 666 # 右下角 X 坐标
bottom = 633 # 右下角 Y 坐标
# 裁剪出指定范围的区域
cropped_image = image.crop((left, top, right, bottom))
# 保存裁剪后的图片
cropped_image_path = r"D:\hassecutrity\hassecutrityAuto\picture\live2.png"
sleep(1)
cropped_image.save(cropped_image_path)
reference_image1 = cv2.imread('./picture/live.png')
reference_image2 = cv2.imread('./picture/live2.png')
# 计算图像差异
mse = ((reference_image1 - reference_image2) ** 2).mean()
print(mse)
#---------------------------------------开始判断后板出图时间------------------------------------------------
if int(mse) != 0:
print("出图成功")
consume_time = time.time() - bank_time
bank_PASS_time = time.strftime('%Y-%m-%d-%H-%M-%S')
screenshot_path_fail = r"./picture/fail/" + "后板"+str(bank_PASS_time) + ".png"
driver.get_screenshot_as_file(screenshot_path_fail)
with open('./picture/loop_live.txt', 'a') as log_file:
# for success in success_times:
log_file.write(
"" + str(i) + "次成功,打开后板直播总时长为:" + str(consume_time) + ",时间点:" + str(
bank_PASS_time) + "\n")
sleep(5)
# driver.back()
try:
sleep(1)
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button").click()
except:
driver.tap([(343, 525)])
sleep(0.5)
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button").click()
# driver.tap([(55, 104)])
finally:
assert driver.find_element(AppiumBy.ACCESSIBILITY_ID, "所有设备")
exit_flag = True
break
if time.time() - bank_time > 25:
print("出图失败")
Fail_time = time.strftime('%Y-%m-%d-%H-%M-%S')
screenshot_path_fail = r"./picture/fail/" + "后板"+str(Fail_time) + ".png"
driver.get_screenshot_as_file(screenshot_path_fail)
with open('./picture/loop_live.txt', 'a') as log_file:
# for success in success_times:
log_file.write(
"" + str(i) + "次后板直播失败" ",时间点:" + str(Fail_time) + "\n")
try:
sleep(1)
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button").click()
except:
driver.tap([(343, 525)])
sleep(0.5)
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button").click()
# driver.tap([(55, 104)])
finally:
sleep(2)
assert driver.find_element(AppiumBy.ACCESSIBILITY_ID, "所有设备")
exit_flag = True
break
if exit_flag:
break
sleep(3)
except:
continue