栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Appium自动化下拉刷新

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Appium自动化下拉刷新

使用Appium作app自动化测试,绕不开的就是下拉刷新操作,下拉是最常见的,其实还有上滑、左滑、右滑操作。在正式的测试流程里,可以把刷新操作封装成一个模块,需要时直接调用。

Appium中提供了三种滑动的方式:swipe滑动、scroll滑动、drag拖拽事件

swipe()方法是从⼀个坐标位置滑动到另⼀个坐标位置,是两个点之间的滑动。

def swipe(self, start_x, start_y, end_x, end_y, duration=None):
    """Swipe from one point to another point, for an optional duration.

    :Args:
     - start_x - x-coordinate at which to start
     - start_y - y-coordinate at which to start
     - end_x - x-coordinate at which to stop
     - end_y - y-coordinate at which to stop
     - duration - (optional) time to take the swipe, in ms.

    :Usage:
        driver.swipe(100, 100, 100, 400)
    """
    # `swipe` is something like press-wait-move_to-release, which the server
    # will translate into the correct action
    action = TouchAction(self)
    action 
        .press(x=start_x, y=start_y) 
        .wait(ms=duration) 
        .move_to(x=end_x, y=end_y) 
        .release()
    action.perform()
    return self
  • start_x - 滑动开始x轴坐标
  • start_y - 滑动开始y轴坐标
  • end_x - 滑动结束x轴偏移量
  • end_y - 滑动结束y轴偏移量
  • duration - (可选) 执行此次滑动时间,单位毫秒

其中end_x 和 end_y 为基于start_x 和start_y 的偏移量;最终在执行中的 to_x = start_x +end_x 并非end_x ;duration 参数单位为ms(默认5毫秒),注意 1s = 1000ms。

封装刷新模块

便于随时调用,我们可以自行封装代码,将上滑、下滑、左滑、右滑封装成一个刷新模块。

#coding=utf-8
import time

class Slide(object):
    def __init__(self, driver):
        self.driver = driver

    #获取机器屏幕大小x,y
    def get_size(self):
        x = self.driver.get_window_size()['width']
        y = self.driver.get_window_size()['height']
        return x, y

    #屏幕向上滑动
    def swipe_up(self, t): # t是指滑动时间(默认5毫秒,可选参数)
        """
        滑动时X轴不变,Y轴由大到小
        """
        screensize = self.get_size()
        x1 = int(screensize[0] * 0.5) # x坐标
        y1 = int(screensize[1] * 0.75) # 起始y坐标
        y2 = int(screensize[1] * 0.25) # 终点y坐标
        self.driver.swipe(x1, y1, x1, y2, t)

    #屏幕向下滑动
    def swipe_down(self, t):
        """
        滑动时X轴不变,Y轴由小到大
        """
        screensize = self.get_size()
        x1 = int(screensize[0] * 0.5) # x坐标
        y1 = int(screensize[1] * 0.25) # 起始y坐标
        y2 = int(screensize[1] * 0.75) # 终点y坐标
        self.driver.swipe(x1, y1, x1, y2, t)

    #屏幕向左滑动
    def swipe_left(self, n, t): # n是指滑动次数
        """
        滑动时Y轴不变,X轴由大到小
        """
        screensize = self.get_size()
        x1 = int(screensize[0] * 0.9)
        y1 = int(screensize[1] * 0.5)
        x2 = int(screensize[0] * 0.1)
        for i in range(0, n): # for循环,控制滑动次数
            time.sleep(3)
            self.driver.swipe(x1, y1, x2, y1, t)

    #屏幕向右滑动
    def swipe_right(self, n, t):
        """
        滑动时Y轴不变,X轴由小到大
        """
        screensize = self.get_size()
        x1 = int(screensize[0] * 0.1)
        y1 = int(screensize[1] * 0.5)
        x2 = int(screensize[0] * 0.9)
        for i in range(0, n): # for循环,控制滑动次数
            time.sleep(3)
            self.driver.swipe(x1, y1, x2, y1, t)

假设刷新模块的文件名为app_slide 那么可以这样调用

from app_slide import Slide

#调用时传递appium.webdriver
flush = Slide(driver)

#下拉刷新
flush.swipe_down(1000)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939653.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号