App自动化测试(五)之swipe滑动操作
App自动化测试之swipe滑动操作
分析滑动坐标

从上图我们可以分析出每个点的坐标,
假设屏幕宽为 width,高为 height
A:(0.5 * width,0.1 * height)
B:(0.5 * width,0.9 * height)
C:(0.1 * width,0.5 * height)
D:(0.9 * width,0.5 * height)
进行滑动操作:
向左滑动:D -->> C
向右滑动:C -->> D
向上滑动:B -->> A
向下滑动:A -->> B
swipe滑动操作
swipe(self, start_x, start_y, end_x, end_y, duration=None)
    从一个点滑动到另外一个点
    start_x 是开始滑动的x坐标, start_y 是开始滑动的y坐标
    end_x 是结束点x坐标,end_y是结束点y坐标
    duration是持续时间,单位毫秒,可以不填,一般设置为500-1000之间
1、获取屏幕窗口的大小
window_size = driver.get_window_size()
2、获取宽高
# 获取宽度
width = window_size['width']
# 获取高度
height = window_size['height']
3、使用swipe进行滑动操作
a. 向左滑动(D -->> C)
driver.swipe(0.9 * width, 0.5 * height, 0.1 * width, 0.5 * height, 200)
b. 向右滑动(C -->> D)
driver.swipe(0.1 * width, 0.5 * height, 0.9 * width, 0.5 * height, 200)
c. 向上滑动(B -->> A)
driver.swipe(0.5 * width, 0.9 * height, 0.5 * width, 0.1 * height, 200)
d. 向下滑动(A -->> B)
driver.swipe(0.5 * width, 0.1 * height, 0.5 * width, 0.9 * height, 200)
封装滑动操作,使之更加灵活

通过上图可以知道scale是代表的滑动距离所占的比例,可以得到每个点的坐标:
假设屏幕的宽高为 x,y
A:(0.5 * x, (1-scale)/2 * y )
B:(0.5 * x, (1+scale)/2 * y)
C:((1-scale)/2 * x, 0.5 * y)
D:((1+scale)/2 * x, 0.5 * y)
1、封装滑动操作需要的前置条件
class BasePage(object):
    def __init__(self, driver):
        self.driver = driver
    @property
    def get_window_size(self):
        """获取窗口大小"""
        return self.driver.get_window_size()
    @property
    def x(self):
        """获取x轴宽度"""
        return self.get_window_size['width']
    @property
    def y(self):
        """获取y轴高度"""
        return self.get_window_size['height']
2、封装向左滑动操作
    def swipe_left(self, scale=0.8):
        """
        向左滑动
        :param scale: 滑动距离所占比例
        起点:(1+scale)x/2,  0.5y
        终点:(1-scale)x/2,  0.5y
        """
        self.driver.swipe(self.x * (1 + scale) / 2, self.y * 0.5, 
                          self.x * (1 - scale) / 2, self.y * 0.5)
3、封装向右滑动操作
    def swipe_right(self, scale=0.8):
        """
        向右滑动
        :param scale: 滑动距离所占比例
        起点:(1-scale)x/2,  0.5y
        终点:(1+scale)x/2,  0.5y
        """
        self.driver.swipe(self.x * (1 - scale) / 2, self.y * 0.5, 
                          self.x * (1 + scale) / 2, self.y * 0.5)
4、封装向上滑动操作
    def swipe_up(self, scale=0.8):
        """
        向上滑动
        :param scale: 滑动距离所占比例
        起点:0.5x,  (1+scale)*y/2
        终点:0.5x,  (1-scale)*y/2
        """
        self.driver.swipe(self.x * 0.5, self.y * (1 + scale) / 2, 
                          self.x * 0.5, self.y * (1 - scale) / 2)
5、封装向下滑动操作
    def swipe_down(self, scale=0.8):
        """
        向下滑动
        :param scale: 滑动距离所占比例
        起点:0.5x,  (1-scale)*y/2
        终点:0.5x,  (1+scale)*y/2
        """
        self.driver.swipe(self.x * 0.5, self.y * (1 - scale) / 2, 
                          self.x * 0.5, self.y * (1 + scale) / 2)
6、封装一个综合滑动操作
    def swipe(self, direction, scale):
        """
        综合滑动操作
        :param direction: 滑动方向 'left','right','up','down'
        :param scale: 滑动距离所占比例
        Usage:
            swipe(direction = 'left',scale=0.8)
        """
        swipe_direction = {
            'left': self.swipe_left,
            'right': self.swipe_right,
            'up': self.swipe_up,
            'down': self.swipe_down
        }
        # 如果输入的滑动方向不为'left','right','up','down'则抛出异常
        if direction not in swipe_direction:
            raise ValueError("Please enter the correct direction!")
        return swipe_direction[direction](scale)

                
            
        
浙公网安备 33010602011771号