1. 程式人生 > >新版appium繪制九宮格的一個註意點

新版appium繪制九宮格的一個註意點

perf eight login 後來 bili sin abi 密碼 tor

在用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()

新版appium繪制九宮格的一個註意點