書接上回,我們說(shuō)到了使用appium錄制腳本

錄制時(shí),代碼區(qū)會(huì)根據(jù)我們的操作實(shí)時(shí)生成代碼。
右側(cè)是可選語(yǔ)言
1 生成可執(zhí)行腳本
2 復(fù)制腳本
3 刪除嘍
接下來(lái),我們講講使用腳本
首先運(yùn)行appium,確保server已啟動(dòng)
本地運(yùn)行python,我用的是VSCode
將從appium中復(fù)制的腳本貼到VScode中,我們運(yùn)行一哈看看效果
遇到的問題
1 No module named appium
沒有安裝Python-Client
安裝一哈
pip install Appium-Python-Client
2 name 'TouchAction' is not defined
from appium.webdriver.common.touch_action import TouchAction
然后就可以起飛了
代碼如下
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
caps = {}
caps["platformName"] = "iOS"
caps["platformVersion"] = "12.4"
caps["deviceName"] = "iPhone"
caps["automationName"] = "XCUITest"
caps["app"] = "com.*****"
caps["udid"] = "ed428*********bd63e4"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
TouchAction(driver).tap(x=265, y=118).perform()
TouchAction(driver).tap(x=37, y=46).perform()
TouchAction(driver).tap(x=118, y=641).perform()
TouchAction(driver).tap(x=249, y=171).perform()
driver.back()
TouchAction(driver).tap(x=171, y=265).perform()
# driver.quit()
代碼中
TouchAction(driver).tap(x=265, y=118).perform()
就屬于模擬根據(jù)坐標(biāo)模擬點(diǎn)擊動(dòng)作.
同樣的調(diào)用方式我們可以使用的還有
tap(self, element=None, x=None, y=None, count=1):
press(self, el=None, x=None, y=None, pressure=None)
long_press(self, el=None, x=None, y=None, duration=1000)
wait(self, ms=0)
move_to(self, el=None, x=None, y=None)
但是使用坐標(biāo)來(lái)操作界面就會(huì)遇到一個(gè)問題,比如actionSheet,alert,或者其他自定義的視圖(效果如模態(tài)視圖),操作會(huì)出現(xiàn)相應(yīng)異常
此時(shí),我們就可以使用第二種方式來(lái)進(jìn)行操作
1 通過appium中的appSource查找你所要操作的元素

然后右側(cè)SelectedElement中可以看到FindBy 和Selector
btn=driver.find_element_by_xpath("http://XCUIElementTypeButton[@name='home dt']")
btn=driver.find_element_by_accessibility_id('home dt')
#這倆是等效操作
btn.click()