关于手机app或者小程序自动化如何移动滑块

1.前言

最近由于某多频繁升级为了有数据无奈弄了自动化.自动化难点在于滑块处理

2.自动化工具选择airtest

1.正常协议过滑块

首先计算出缺口图片滑块图片的距离然后与本地图片的大小与页面上图片css大小进行比例换成✖️我们本地计算出来的距离

2.airtest完成滑动

至此我们要完成airtest移动滑块就需要计算自动化工具要滑动多少

本着上面过协议的思路

我们先要计算出滑块到缺口的距离

先用hook或者mit形式获取滑块

这里一般就使用opencvj计算一下

import cv2


def show(name):
    '''展示圈出来的位置'''
    cv2.imshow('Show', name)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def _tran_canny(image):
    """消除噪声"""
    image = cv2.GaussianBlur(image, (3, 3), 0)
    return cv2.Canny(image, 50, 150)


def detect_displacement(img_slider_path, image_background_path,slide_width):
    """
    距离计算
    :param img_slider_path: 缺口图片位置
    :param image_background_path: 背景图片位置
    :param slide_width: 滑块显示的距离
    :return : 滑动的距离
    
    """
    # # 参数0是灰度模式
    image = cv2.imread(img_slider_path, 0)
    template = cv2.imread(image_background_path, 0)
    # 寻找最佳匹配
    res = cv2.matchTemplate(_tran_canny(image), _tran_canny(template), cv2.TM_CCOEFF_NORMED)
    # 最小值,最大值,并得到最小值, 最大值的索引
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    top_left = max_loc[0] + 30  # 横座标  可加可不加,后面滑动时候也要减去调整建议不加
    # 展示圈出来的区域
    # x, y = max_loc  # 获取x,y位置座标
    # w, h = image.shape[::-1]  # 宽高
    # print(w, h)
    # print(top_left)
    # print(template.shape[1])
    # cv2.rectangle(template, (x, y), (x + w, y + h), (7, 249, 151), 2)
    # show(template)
    top_left = int(top_left * (slide_width / template.shape[1]))
    return top_left

关于代码中slide_width参数,我们要打开airtest中的实时座标然后滑块图片最右侧滑块图片最左侧

我们真实操作滑块滑动距离是上述计算出来的距离-滑块的起始按住的位置小滑块最右的位置

aitest部分代码

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from airtest.core.api import *

def get_poco():
  '''获取设备poco'''
    class_name = '你的设备'
    connect_device(f"Android:///{class_name}?cap_method=JAVACAP&&ori_method=ADBORI&&touch_method=ADBTOUCH")
    set_current(class_name)
    dev = device()
    poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False,device=dev)
    return poco
  
def start_verification(poco):
    w = get_verify_verification_w() #你自己获取上述距离的方法
    print(f"w----------{w}")
    w = w - 90  #90为滑块的-`滑块的起始按住的位置`到 `小滑块最右的位置`
    s_h  = 0.623
    s_w = 0.185
    e_h = s_h +-random.randint(5,20)/1000  #滑块上下宽度浮动
    width, height = poco.get_screen_size()
    e_w = round(s_w + w/width,3)
    print("滑动开始")
    print([(s_w, s_h),(e_w,e_h)])
    poco.swipe([s_w, s_h],[e_w,e_h])  
    print("滑动结束")
    time.sleep(3)
    print("按返回键")

关于swip方法补充

duration – 在屏幕上滑动的时长,默认是 0.5

steps – 滑动过程中的步数,默认为 5

fingers – 滑动的手指数量,1 或者 2,默认为 1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章