Python 封裝Swipe實(shí)現(xiàn)手機(jī)屏幕滑動(dòng)操作

一. 封裝Swipe目的

  1. Appium的滑動(dòng)API:Swipe(int start x,int start y,int end x,int y,duration)
    使用前首先得獲取手機(jī)屏幕的分辨率,然后根據(jù)分辨率計(jì)算得出滑動(dòng)的起始和終點(diǎn)坐標(biāo)。
    封裝后的swipe,只需調(diào)用對(duì)應(yīng)的方法即可實(shí)現(xiàn) 向上/向下/向左/向右/斜向上 常規(guī)滑動(dòng)屏幕操作。
  2. Appium的API,使用均依賴于webdriver的調(diào)用,必須先建立session連接才能實(shí)現(xiàn)swipe功能
    封裝后的swipe,基于ADB command,只需手機(jī)與PC建立ADB連接即可實(shí)現(xiàn)swipe功能。
  3. Appium的API通過(guò)建立不同的session 實(shí)現(xiàn)同時(shí)操作多部手機(jī)
    封裝后的swipe,直接通過(guò)傳入手機(jī) serial Number 即可實(shí)現(xiàn)同時(shí)操作多部手機(jī)。

綜上,封裝后的swipe可應(yīng)用于Appium自動(dòng)化測(cè)試并獨(dú)立于Appium,且可以實(shí)現(xiàn)多部手機(jī)同時(shí)運(yùn)作的功能。

二. Source Code

# -*- coding: utf-8 -*-

import os
import sys
import re


def get_screen_size(*args):
    """Get size of window and return the size for swipe function
       args 作為可變參數(shù) 表示是否傳入設(shè)備的ID
    """
    if not args:
        # 如果沒(méi)有傳入指定設(shè)備ID,執(zhí)行以下ADB command,獲取設(shè)備屏幕分辨率
        size_file = os.popen("adb shell dumpsys window displays").read()
    else:
        # 如果傳入指定設(shè)備ID,執(zhí)行以下ADB command,獲取設(shè)備屏幕分辨率
        uid = args[0]
        size_file = os.popen("adb -s %s shell dumpsys window displays" % uid).read()
    if not size_file:
        # 獲取設(shè)備分辨率失敗
        print("Cannot get the resolution of screen, please check the ADB.")
        sys.exit()
    else:
        # 正則表達(dá)式匹配設(shè)備分辨率
        size_match = re.search(r"(\d+)x(\d+)", size_file)
        if not size_match:
            # 設(shè)備分辨率匹配失敗
            print("Failed to match the screen size.")
            sys.exit()
        else:
            # 設(shè)備分辨率信息從字符串分割轉(zhuǎn)換為二元元組
            size_screen = re.split(r"x", size_match.group())
            print(size_screen)
            # 字符串元素元組轉(zhuǎn)換為整型元素列表
            size = [int(size_screen[0]), int(size_screen[1])]
            return size


def swipe_up(*args, t=100, n=1):
    """Swipe device screen up in t milliseconds and repeat the operation n times
       t=100 作為命名關(guān)鍵字參數(shù) 表示默認(rèn)的滑動(dòng)時(shí)間為100ms 可自尋設(shè)計(jì)滑動(dòng)時(shí)間
       n=1 作為命名關(guān)鍵字參數(shù) 表示默認(rèn)的滑動(dòng)次數(shù)為1次 可自尋設(shè)計(jì)滑動(dòng)次數(shù)
    """
    size = get_screen_size(*args)
    x1 = size[0] * 0.5
    y1 = size[1] * 0.75
    x2 = size[0] * 0.5
    y2 = size[1] * 0.25
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_down(*args, t=100, n=1):
    """Swipe device screen down in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.5
    y1 = size[1] * 0.25
    x2 = size[0] * 0.5
    y2 = size[1] * 0.75
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_left(*args, t=100, n=1):
    """Swipe device screen left in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.95
    y1 = size[1] * 0.5
    x2 = size[0] * 0.05
    y2 = size[1] * 0.5
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_right(*args, t=100, n=1):
    """Swipe device screen right in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.05
    y1 = size[1] * 0.5
    x2 = size[0] * 0.95
    y2 = size[1] * 0.5
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))


def swipe_oblique(*args, t=100, n=1):
    """Swipe device screen oblique in t milliseconds and repeat the operation n times"""
    size = get_screen_size(*args)
    x1 = size[0] * 0.05
    y1 = size[1] * 0.75
    x2 = size[0] * 0.95
    y2 = size[1] * 0.25
    for i in range(n):
        if not args:
            os.system("adb shell input swipe %f %f %f %f %d" % (x1, y1, x2, y2, t))
        else:
            uid = args[0]
            os.system("adb -s %s shell input swipe %f %f %f %f %d" % (uid, x1, y1, x2, y2, t))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容