Python+Appium 实现上下左右滑动屏幕

实现较为累赘,可以把获取屏幕size,width,height 属性作为公共属性单抽出来,不过不影响使用
还有可以优化的地方, try: except: 没有起到我预期的效果,暂时先留着

import time
from MocirePatinet import PhoneSet
import traceback


class Slide():

    @staticmethod
    def swipeUp():
        
        # 获取屏幕的size
        size = PhoneSet.driver.get_window_size()
        print(size)
        # 获取屏幕宽度 width
        width = size['width']
        print(width)
        # 获取屏幕高度 height
        height = size['height']
        print(height)
        # 执行滑屏操作,向上(上拉)滑动
        x1 = width * 0.5
        y1 = height * 0.9
        y2 = height * 0.25
        # time.sleep(2)
        print("滑动前")
        PhoneSet.driver.swipe(x1, y1, x1, y2,3000)
        print("滑动后")
        # 增加滑动次数,滑动效果不明显,增加滑动次数

        for i in range(3):
            print("第%d次滑屏" % i)
            time.sleep(2)
            try:
                PhoneSet.driver.swipe(x1, y1, x1, y2,3000)
            except:
                print("break")
                print(traceback.format_exc())
                break

    @staticmethod
    def swipeDown():
        # 获取屏幕的size
        size = PhoneSet.driver.get_window_size()
        print(size)
        # 获取屏幕宽度 width
        width = size['width']
        print(width)
        # 获取屏幕高度 height
        height = size['height']
        print(height)
        # 执行滑屏操作,向下(下拉)滑动
        x1 = width * 0.5
        y1 = height * 0.25
        y2 = height * 0.9
        # time.sleep(2)
        print("滑动前")
        PhoneSet.driver.swipe(x1, y1, x1, y2, 3000)
        print("滑动后")
        # 增加滑动次数,滑动效果不明显,增加滑动次数

        for i in range(3):
            print("第%d次滑屏" % i)
            time.sleep(3)
            try:
                PhoneSet.driver.swipe(x1, y1, x1, y2, 3000)
            except:
                print("break")
                print(traceback.format_exc())
                break
        # time.sleep(3)

    @staticmethod
    def swipeLeft():
        # 获取屏幕size
        size = PhoneSet.driver.get_window_size()
        print(size)
        # 获取屏幕宽度
        width = size['width']
        print(width)
        # 获取屏幕高度
        height = size['height']
        print(height)
        # 执行左滑操作,(向左)滑动
        x1 = width*0.8
        x2 = width*0.2
        y1 = height*0.5

        # 滑动前
        PhoneSet.driver.swipe(x1,y1,x2,y1,2000)
        # 滑动后
        for i in range(1):
            print("第%d次滑屏"% i)
            time.sleep(2)
            PhoneSet.driver.swipe(x1,y1,x2,y1,2000)

    @staticmethod
    def swipeRight():
        # 获取屏幕size
        size = PhoneSet.driver.get_window_size()
        print(size)
        # 获取屏幕宽度width
        width = size['width']
        # 获取屏幕高度height
        height = size['height']
        x1 = width*0.2
        x2 = width*0.8
        y1= height*0.5
        # 滑动前操作,向右滑动
        PhoneSet.driver.swipe(x1,y1,x2,y1,2000)
        # 滑动后
        for i in range(2):
            print("第%d次滑动"%i)
            time.sleep(2)
            PhoneSet.driver.swipe(x1,y1,x2,y1,2000)
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读