appium-封装屏幕滑动方法

时间:2021-05-26 17:58:27

from common.logger import Logger
logger=Logger().get_logger()
class BasePage:
    def __init__(self,driver):
        self.driver=driver
    def get_window_size(self):
        logger.info("正在准备获取当前屏幕的大小")
        try:
            size=self.driver.get_window_size()
            width=size["width"]
            height=size["height"]
            logger.info("获取当前屏幕大小成功:宽:{}   高:{}".format(width,height))
            return width,height
        except:
            logger.exception("获取当前屏幕大小失败")
    def swipe_down(self,t=500,n=1):
        logger.info("正在准备向下滑动屏幕")
        try:
            size=self.get_window_size()
            x=size[0]*0.5
            y=size[1]*0.2
            x1=size[0]*0.5
            y1=size[1]*0.8
            for i in range(n):
                self.driver.swipe(x,y,x1,y1,t)
                logger.info("屏幕第{}次向下滑动成功".format(i 1))
        except:
            logger.exception("屏幕向下滑动失败")
    def swipe_up(self,t=500,n=1):
        logger.info("正在准备向上滑动屏幕")
        try:
            size=self.get_window_size()
            x=size[0]*0.5
            y=size[1]*0.8
            x1=size[0]*0.5
            y1=size[1]*0.2
            for i in range(n):
                self.driver.swipe(x,y,x1,y1,t)
                logger.info("屏幕第{}次向上滑动成功".format(i 1))
        except:
            logger.exception("屏幕向上滑动失败")
    def swipe_right(self,t=500,n=1):
        logger.info("正在准备向右滑动屏幕")
        try:
            size = self.get_window_size()
            x = size[0] * 0.2
            y = size[1] * 0.5
            x1 = size[0] * 0.8
            y1 = size[1] * 0.5
            for i in range(n):
                self.driver.swipe(x, y, x1, y1, t)
                logger.info("屏幕第{}次向右滑动成功".format(i   1))
        except:
            logger.exception("屏幕向右滑动失败")

    def swipe_left(self, t=500, n=1):
        logger.info("正在准备向左滑动屏幕")
        try:
            size = self.get_window_size()
            x = size[0] * 0.8
            y = size[1] * 0.5
            x1 = size[0] * 0.2
            y1 = size[1] * 0.5
            for i in range(n):
                self.driver.swipe(x, y, x1, y1, t)
                logger.info("屏幕第{}次向左滑动成功".format(i   1))
        except:
            logger.exception("屏幕向左滑动失败")