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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM