zoukankan      html  css  js  c++  java
  • 新版appium绘制九宫格的一个注意点

    在用appium-desktop-setup-1.6.2进行app手势密码设置时,发现move_to(x, y)相对偏移量的方法用不了,绘制的手势也是乱跑

    还会抛一个错误

    selenium.common.exceptions.InvalidCoordinatesException: Message: Coordinate [x=-210.0, y=210.0] is outside of element rect: [0,0][720,1280]

    代码如下

    from appium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from appium.webdriver.common.mobileby import MobileBy
    from appium.webdriver.common.touch_action import TouchAction
    from time import sleep
    
    
    # 由你主动来告诉appium server,我要操作哪个设备上的哪个app
    # Desired Capabilites,键值对,键名都是已经定义好的
    
    # 操作对象的信息准备
    desired_caps={}
    # 使用的手机操作系统
    desired_caps["platformName"] = "Android"
    # 手机操作系统的版本
    desired_caps["platformVersion"]="5.1.1"
    # 使用的设备名字或模拟器的类型
    desired_caps["deviceName"]="Android Emulator"
    # app信息
    desired_caps["appPackage"]="com.xxzb.fenwoo"
    #进入的界面
    desired_caps["appActivity"] = ".activity.addition.WelcomeActivity"
    
    # 连接appium server,并告诉其要操作的对象
    driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
    
    
    # 获取屏幕尺寸
    size=driver.get_window_size()
    for x in range(0,4):
        # 向左滑动
        driver.swipe(size["width"]*0.9,size["height"]*0.5,size["width"]*0.1,size["height"]*0.5,500)
        # 向右滑动
        # driver.swipe(size["width"]*0.1,size["height"]*0.5,size["width"]*0.9,size["height"]*0.5,500)
        sleep(1)
    
    # 点击立即体验
    WebDriverWait(driver,20,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_start")))
    driver.find_element_by_id("com.xxzb.fenwoo:id/btn_start").click()
    # 等待
    WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_login")))
    # 点击登录注册按钮
    driver.find_element_by_id("com.xxzb.fenwoo:id/btn_login").click()
    # 等待
    WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/et_phone")))
    # 输入用户名
    driver.find_element_by_id("com.xxzb.fenwoo:id/et_phone").send_keys("XXXXXX")
    # 点击下一步
    driver.find_element_by_id("com.xxzb.fenwoo:id/btn_next_step").click()
    # 等待
    WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/et_pwd")))
    # 输入密码
    driver.find_element_by_id("com.xxzb.fenwoo:id/et_pwd").send_keys("XXXXXX")
    # 点击确定按钮
    driver.find_element_by_id("com.xxzb.fenwoo:id/btn_next_step").click()
    # 等待
    WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_confirm")))
    # 马上设置
    driver.find_element_by_id("com.xxzb.fenwoo:id/btn_confirm").click()
    # 等待
    WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_gesturepwd_guide")))
    # 立即创建
    driver.find_element_by_id("com.xxzb.fenwoo:id/btn_gesturepwd_guide").click()
    # 等待
    WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/right_btn")))
    # 确定按钮
    driver.find_element_by_id("com.xxzb.fenwoo:id/right_btn").click()
    
    # 手势绘制页面
    ta=TouchAction(driver)
    # 获取九宫格的起点坐标和大小
    ele=driver.find_element_by_id("com.xxzb.fenwoo:id/gesturepwd_create_lockview")
    size=ele.size
    # 获取坐标
    start_point=ele.location
    ta.press(x=start_point["x"]+size["width"]*1/6,y=start_point["y"]+size["height"]*1/6).wait(200).
        move_to(x=size["width"]*2/6,y=0).wait(200).
        move_to(x=size["width"]*2/6,y=0).wait(200).
        move_to(x=-size["width"]*2/6,y=size["width"]*2/6).wait(200).
        move_to(x=0,y=size["width"]*2/6).wait(200).release().wait(200).perform()
    
    # 等待
    # WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/right_btn")))
    # 点击继续
    driver.find_element_by_id("com.xxzb.fenwoo:id/right_btn").click()

    而且一个奇怪的现象是第一个圆圈的坐标点是(150, 379),相对偏移(210, 0)之后得到的坐标是(210, 640)

    后来尝试着

    ta.press(x=0, y=0).release().perfrom()

    神奇的发现(0, 0)点竟然是(360, 640)这个位置

    如果尝试press(x=0, y=10)得到的坐标点是(360, 10), press(x=10, y=0),得到的坐标点是(10, 640),只有当press(x=10, y=10)的时候得到的才是(10, 10),这样就可以解释上面x=210, y=0, 为什么是(210, 640)了。看来x或y不能为0,不然会跑偏

    这个问题目前已经报bug,bug地址为:https://discuss.appium.io/t/set-gesture-password-and-show-pressed-coordinate-error-using-1-6-2-appium-desktop-on-android-simulator/22858

    目前的解决办法是利用绝对坐标定位,修改的代码如下:

    ta.press(x=start_point["x"]+size["width"]*1/6,y=start_point["y"]+size["height"]*1/6).wait(200).
        move_to(x=start_point["x"]+size["width"]*3/6, y=start_point["y"]+size["height"]*1/6).wait(200).
        move_to(x=start_point["x"]+size["width"]*5/6, y=start_point["y"]+size["height"]*1/6).wait(200).
        move_to(x=start_point["x"]+size["width"]*3/6, y=start_point["y"]+size["width"]*3/6).wait(200).
        move_to(x=start_point["x"]+size["width"]*3/6, y=start_point["y"]+size["width"]*5/6).wait(200).release().wait(200).perform()

    ======2020年4月28日更新======

    今天看官方文档,看到这一句话:也就是说当moveTo中有元素对象时,传的坐标是相对于元素位置的相对坐标,没有元素对象时,是绝对坐标

    Appium 客户端库有不同的实现方式,例如:你可以传递坐标或一个元素给 moveTo 事件。当坐标 元素一起传递时,该坐标被理解为是相对于元素位置的相对坐标,而不是绝对位置

  • 相关阅读:
    重载与复写的区别
    Linux常用的网络命令
    JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
    Java中重载与复写的区别、super与this的比较
    JAVA中extends 与implements区别
    Android控件系列之Button以及Android监听器
    Could not find SDK_Root\tools\adb.exe 的解决方法
    路由知识普及和经验分享
    abort函数
    细说Cookie
  • 原文地址:https://www.cnblogs.com/my_captain/p/9341937.html
Copyright © 2011-2022 走看看