platformName 平台的名称:iOS, Android, or FirefoxOS
deviceName 设备号 IOS:instruments -s devices,Android: adb devices
appActivity 启动的Activity
appPackage 启动的包
出现这个界面表示成功
# This sample code uses the Appium python client
# pip install Appium-Python-Client==1.3.0
# Then you can paste this into a file and simply run with Python
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import time
caps = {}
caps["deviceName"] = "127.0.0.1:62001"
caps["platformName"] = "Android"
caps["appActivity"] = "com.android.settings.Settings"
caps["appPackage"] = "com.android.settings"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
el1 = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
# scroll滑动事件
driver.scroll(el1,el2)
# 点击安全
driver.find_element_by_xpath("//*[contains(@text,'安全')]").click()
# 定时
time.sleep(2)
# 屏幕锁定方式
driver.find_element_by_xpath("//*[contains(@text,'屏幕锁定方式')]").click()
# 定时
time.sleep(2)
# 图案
driver.find_element_by_xpath("//*[contains(@text,'图案')]").click()
# 定时
time.sleep(2)
# 180 620 536 620 536 975 180 1335
TouchAction(driver).press(x=180,y=620).wait(100).move_to(x=536,y=620).wait(100)\
.move_to(x=536,y=975).wait(100).move_to(x=180,y=1335).release().perform()
# drag拖拽事件
# driver.drag_and_drop(el1,el2)
driver.quit()
划动
swip滑动事件
⚠️从一个坐标位置滑动到另一个坐标位置,只能是两个点之间的滑动
# 滑动没有持续时间
driver.swipe(188,659,148,248)
# 滑动持续5秒的时间
driver.swipe(188,659,148,248,5000)
scroll滑动事件
⚠️ 从一个元素滑动到另一个元素,直到页面自动停止
# 定位到存储菜单栏
el1 = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
# 定位到WLAN菜单栏
el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
# 执行滑动操作
driver.scroll(el1,el2)
drag拖拽事件
⚠️ 从一个元素滑动到另一个元素,第二个元素替代第一个元素原本屏幕上的位置
# 定位到存储菜单栏
el1 = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
# 定位到WLAN菜单栏
el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
# 执行滑动操作
driver.drag_and_drop(el1,el2)
手指轻敲操作
# 通过元素定位方式敲击屏幕
el = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
TouchAction(driver).tap(el).perform()
# 通过坐标方式敲击屏幕,WLAN坐标:x=155,y=250
# TouchAction(driver).tap(x=155,y=250).perform()
手指按操作
# 通过元素定位方式按下屏幕
el = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
TouchAction(driver).press(el).release().perform()
# 通过坐标方式按下屏幕,WLAN坐标:x=155,y=250
# TouchAction(driver).press(x=155,y=250).release().perform()
等待操作
# 点击WLAN
driver.find_element_by_xpath("//*[contains(@text,'WLAN')]").click()
# 定位到WiredSSID
el =driver.find_element_by_id("android:id/title")
# 通过元素定位方式长按元素
TouchAction(driver).press(el).wait(5000).perform()
# 通过坐标方式模拟长按元素
# 添加等待(有长按)/不添加等待(无长按效果)
# TouchAction(driver).press(x=770,y=667).wait(5000).release().perform()
手指长按操作
# 点击WLAN
driver.find_element_by_xpath("//*[contains(@text,'WLAN')]").click()
# 定位到WiredSSID
el =driver.find_element_by_id("android:id/title")
# 通过元素定位方式长按元素
TouchAction(driver).long_press(el,duration=5000).release().perform()
# 通过坐标方式长按元素,WiredSSID坐标:x=770,y=667
# 添加等待(有长按)/不添加等待(无长按效果)
# TouchAction(driver).long_press(x=770,y=667).perform()
手指移动操作
# 定位到存储
el = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
# 定位到更多
el1 = driver.find_element_by_xpath("//*[contains(@text,'更多')]")
# 元素方式滑动
TouchAction(driver).press(el).move_to(el1).release().perform()
# 坐标的方式滑动
# TouchAction(driver).press(x=240,y=600).wait(100).move_to(x=240,y=100).release().perform()
# 注意press连接一个move_to实际调用的是swip方法,可在log中查询,不要给相对坐标
多个点
# 定位到WLAN
el1 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
# 定位到存储
el2 = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
# 存储上滑到WLAN
driver.drag_and_drop(el2,el1)
# 定位到用户
el3 = driver.find_element_by_xpath("//*[contains(@text,'用户')]")
# 注意 这次使用drag_and_drop方法,传入的"存储定位"仍使用其原始在屏幕上的位置,所以是由存储滑动到用户才可以上滑,否则需要重新"定位存储"
# 存储上滑倒用户位置
driver.drag_and_drop(el2,el3)
# 点击安全按钮
driver.find_element_by_xpath("//*[contains(@text,'安全')]").click()
# 点击屏幕锁定方式按钮
driver.find_element_by_xpath("//*[contains(@text,'屏幕锁定')]").click()
# 点击图案按钮
driver.find_element_by_xpath("//*[contains(@text,'图案')]").click()
# 绘制图案四个坐标 1:(244,967) 2:(723,967) 3:(723,1442) 4:(244,1916)
TouchAction(driver).press(x=244,y=967).wait(100).move_to(x=479,y=0).wait(100)\
.move_to(x=0,y=475).wait(100).move_to(x=-479,y=474).release().perform()