- 前提背景:app内部存在多处广告,需要进行进行自动化:
1. 查看app是否成功跳转了页面
2. 页面是否空白
3. 大致经历的耗时 - 主要思路如下:点击前进行截图操作,点击后进行判断
- 判断图片是否空白
def is_blank(image_path, gray_value=250, threshold=0.9): """ 函数会计算一幅图像中超过250(在0到255的灰度值中,255代表白色)的像素的比例, 如果这个比例超过了阈值(默认为0.05,即5%),那么就判断这幅图像为空白。 :param image_path: :param gray_value: :param threshold: :return: """ # 记录函数开始运行的时间 # start_time = time.time() # 打开图像 img = Image.open(image_path) # 将图像转换为灰度模式 img = img.convert('L') # 将图像转换为numpy数组 img_array = np.asarray(img) # 计算图像的总像素 total_pixels = img_array.shape[0] * img_array.shape[1] # 计算图像中白色(或者接近白色)像素的数量 white_pixels = np.sum(img_array > gray_value) # 如果图像中超过一定比例的像素是白色(或接近白色),则判断图像为空白 # 记录函数结束运行的时间 # end_time = time.time() # 计算并打印函数运行的总时间 # elapsed_time = end_time - start_time # print(f"The function took {elapsed_time} seconds to complete.") return white_pixels / total_pixels > threshold
- 图片比较是否相同
def is_same_image(file1, file2, threshold=10): # 打开图片 img1 = Image.open(file1) img2 = Image.open(file2) # 对比基本信息 if img1.size != img2.size or img1.mode != img2.mode: return False # 对比像素信息 for i in range(img1.size[0]): for j in range(img1.size[1]): p1 = img1.getpixel((i, j)) p2 = img2.getpixel((i, j)) diff = sum([abs(p1[k] - p2[k]) for k in range(3)]) if diff > threshold: return False # 两张图片一致 return True
- 最后删除图片
def delete(image_path): """ 删除图片 :param image_path: :return: """ if os.path.isfile(image_path): os.remove(image_path) print('图片删除成功')
- 获取当前app的activity
def get_current_activity(): """ 获取当前页面的activity :return: """ cmd = 'adb -s {} shell "dumpsys window | grep mCurrentFocus"'.format( get_android_devices()) activity_info = invoke(cmd).splitlines() log.info(activity_info) activity = str(activity_info[0]).split("/")[-1][:-1] log.info(activity) return activity
- 完整代码
import os
from PIL import Image
import numpy as np
activity1 = get_current_activity() save_path = os.path.join(os.getcwd(), 'screenshots') # 保存截图的本地路径 if not os.path.exists(save_path): os.makedirs(save_path) screenshot_name = "闪屏点击前图片.png" screenshot_path1 = os.path.join(save_path, screenshot_name) # 图片保存路径 d.screenshot(screenshot_path1) if d(textContains='第三方应用').exists: d(textContains='第三方应用').click() else: assert False, "不存在对应按钮,需要重新载入新方法" start_time = time.time() time.sleep(3) # 截屏 screenshot_name = "闪屏.png" screenshot_path = os.path.join(save_path, screenshot_name) # 图片保存路径 d.screenshot(screenshot_path) for i in range(0, 3): if is_blank(screenshot_path): delete(screenshot_path) time.sleep(1) screenshot_name = "闪屏.png" screenshot_path = os.path.join(save_path, screenshot_name) # 图片保存路径 d.screenshot(screenshot_path) end_time = time.time() elapsed_time = end_time - start_time if i == 2: assert False, "经过{}秒,当前页面仍然空白".format(elapsed_time) else: delete(screenshot_path) end_time = time.time() elapsed_time = end_time - start_time print("经过{}秒,进入页面".format(elapsed_time)) break # 判断跳转到别的app screenshot_name = "闪屏点击后图片.png" screenshot_path2 = os.path.join(save_path, screenshot_name) # 图片保存路径 d.screenshot(screenshot_path2) if is_same_image(screenshot_path1, screenshot_path2): assert False, "没有跳转任何页面" else: if api.get_current_app() != 'xxx': print("跳转到别的app【{}】".format(api.get_current_app())) else: # 判断跳转到别的非首页的activity if get_current_activity() != activity1: print("跳转到当前app页面:【{}】".format(get_current_activity())) else: print("在app当前页面弹出框") d.app_start('xxx包','xx activity')即可进行闪屏点击,判断跳转进入的页面
- uiautomator2 uiautomator 页面 广告 pythonuiautomator2 uiautomator页面 广告 uiautomator2 uiautomator activity页面 uiautomator2 uiautomator weditor python uiautomator2 uiautomator键盘python uiautomator2 uiautomator python uiautomator2 uiautomator2截图uiautomator报告 39 attributeerror mutablemapping uiautomator2 uiautomator2 uiautomator链接 手机 uiautomator2 uiautomator环境 教程