• 【Appium】移动端自动化测试,触摸(TouchAction) 与多点触控(MultiAction)


    一、触摸 TouchAction

    在所有的 Appium 客户端库里,TouchAction 触摸对象被创建并被赋予一连串的事件。

    规范里可用的事件有: * 短按(press) * 释放(release) * 移动到(moveTo) * 点击(tap) * 等待(wait) * 长按(longPress) * 取消(cancel) * 执行(perform)

    示例:

    from appium import webdriver
    from appium.webdriver.common.touch_action import TouchAction
    
    driver=webdriver.Remote()
    action = TouchAction(driver) # 创建 TouchAction 对象
    # 在坐标(10,100) 位置按下,等待100ms,滑动到 ele 元素上释放
    action.press(x=10,y=100).wait(100).move_to(el=ele).release()
    # 执行动作
    action.perform()

    注意: action.press(x=10,y=100).wait(100).move_to(el=ele).release() 只是将动作存储在action 对象中,还没有执行,具体执行动作要使用 action.perform()。

    点击操作

    # 在控件的中心点上点击一下
    tap(WebElement el)
    
    # 在(x,y)点点击一下
    tap(int x, int y)
    
    # 以控件el的左上角为基准,x 轴向右移动x单位,y 轴向下移动y单位,在该点上点击
    tap(WebElement el, int x, int y)

    按下操作

    #在控件上执行press操作。
    press(WebElement el)  
    
    #在坐标为(x,y)的点执行press操作
    press(int x, int y)  
    
    #在控件el的左上角的x坐标偏移x单位,y坐标偏移y单位的坐标上执行press操作。
    press(WebElement el, int x, int y)

    长按操作

    # 控件长按
    longPress(WebElement el)
    
    # 点长按
    longPress(int x, int y)
    
    # 偏移点长按
    longPress(WebElement el, int x, int y)

    移动操作

    #以el为目标,从另一个点移动到该目标上
    moveTo(WebElement el) 
    
    #以(x,y)点为目标,从另一个点移动到该目标上 
    moveTo(int x, int y) 
    
    # 以控件el的左上角为基准,偏移(x,y)。以该点为目标,从另一个点移动到该点上。
    moveTo(WebElement el, int x, int y)

    释放操作

    release()   # 释放动作,与按下和长按联合使用

    等待

    # 代表一个空操作,等待一段时间
    wait()
    
    # 等待ms毫秒
    wait(int ms)

    执行 TouchAction 存储的动作

    # 执行动作
    perform()
    
    # 也可以通过 driver 对象直接执行触摸操作
    driver.perform(TouchAction().tap(el))

    实例:九宫格解锁,九宫格坐标与解锁密码如下图

    from appium import webdriver
    from appium.webdriver.common.touch_action import TouchAction
    
    # 九宫格原点的位置,从左至右,从上至下
    site = [(200, 200), (200, 400), (200, 600),
            (400, 200), (400, 400), (400, 600),
            (600, 200), (600, 400), (600, 600)]
    
    pwd = '01246'
    pwd_site = []
    # 将密码转换为坐标位置
    for s in pwd:
        pwd_site.append(site[int(s)])
    
    driver = webdriver.Remote()
    action = TouchAction(driver)
    
    # 在起始点按下
    action.press(pwd_site[0]).wait(200)
    
    # 按照坐标点移动
    for i in range(1, len(pwd_site)):
        action.move_to(pwd_site[i]).wait(200)
    # 释放
    action.release()
    
    # 执行动作
    action.perform()

    多点触控(MultiTouch)

    MultiTouch对象是触摸操作的集合,只有两个方法,add 和 perform 。

    add 于将不同的触摸操作添加到当前的多点触控中。

    perform 执行多点触控动作

    示例:

    from appium import webdriver
    from appium.webdriver.common.touch_action import TouchAction
    from appium.webdriver.common.multi_action import MultiAction
    
    driver = webdriver.Remote()
    
    # 创建触摸事件
    action1 = TouchAction(driver).tap(x=100, y=100)
    action2 = TouchAction(driver).tap(element=ele)
    
    # 创建多点触控对象
    multi_action = MultiAction(driver)
    # 同时执行 action1 和 action2 动作
    multi_action.add(action1).add(action2)
    # 执行多点触控
    multi_action.perform()
  • 相关阅读:
    Windows 8.1更新变化
    Hibernate Criterion
    HDU 2795 Billboard(宣传栏贴公告,线段树应用)
    解决ccSvcHst.exe CPU占用超50%的问题,及其缘由
    设计模式之解释器模式(Interpreter)摘录
    中国软件开发project师之痛
    【翻译】ExtJS vs AngularJS
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    linux概念之内存分析
    java实现第四届蓝桥杯带分数
  • 原文地址:https://www.cnblogs.com/mumuluo/p/14548407.html
Copyright © 2020-2023  润新知