關於手機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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章