Appium自动化测试框架3

滑动与拖拽

swipe

滑动时间的长短会影响最后的结果的

是有一定误差的

from appium import webdriver
import time
# 启动一个字典 包装相应的启动参数
desired_caps = dict()
# 平台的名字,安卓还是IOS 大小写无所谓
desired_caps['platformName'] = 'Android'
# 平台的版本,(5.4.3 可以写 5.4.3 5.4 5)
desired_caps['platformVersion'] = '7.1.2'
# 设备的名字,随便写,不能乱写
desired_caps['deviceName'] = 'samsung'
# 要打开的应用程序 包名
desired_caps['appPackage'] = 'com.android.settings'
# 要打开的界面 启动名 界面名
desired_caps['appActivity'] = '.Settings'

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.swipe(100,2000,100,1000)

driver.swipe(100,2000,100,100)

# 以毫秒为单位 5s 持续时间越长 惯性越小 就不会因为惯性而继续往后滑一点
driver.swipe(100,2000,100,100,5000)

time.sleep(20)
driver.quit()

为什么每次运行滑动的时候会有一些误差

对于time.sleep(5) 真实情况下可能会比5秒多一丢丢 

scroll

直到页面自动停止 即有惯性还会继续滑动 等其停止

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time
# 启动一个字典 包装相应的启动参数
desired_caps = dict()
# 平台的名字,安卓还是IOS 大小写无所谓
desired_caps['platformName'] = 'Android'
# 平台的版本,(5.4.3 可以写 5.4.3 5.4 5)
desired_caps['platformVersion'] = '7.1.2'
# 设备的名字,随便写,不能乱写
desired_caps['deviceName'] = 'samsung'
# 要打开的应用程序 包名
desired_caps['appPackage'] = 'com.android.settings'
# 要打开的界面 启动名 界面名
desired_caps['appActivity'] = '.Settings'

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

save = driver.find_element(AppiumBy.XPATH,"//*[@text='蓝牙']")
more = driver.find_element(AppiumBy.XPATH,"//*[@text='通知']")

driver.scroll(save,more)

drag_and_drop

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

save = driver.find_element(AppiumBy.XPATH,"//*[@text='蓝牙']")
more = driver.find_element(AppiumBy.XPATH,"//*[@text='通知']")

driver.drag_and_drop(save,more)

TouchAction高级手势

创建对象 通过对象调用想要执行的方法 通过perform执行

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time
from appium.webdriver.common.touch_action import TouchAction
# 启动一个字典 包装相应的启动参数
desired_caps = dict()
# 平台的名字,安卓还是IOS 大小写无所谓
desired_caps['platformName'] = 'Android'
# 平台的版本,(5.4.3 可以写 5.4.3 5.4 5)
desired_caps['platformVersion'] = '7.1.2'
# 设备的名字,随便写,不能乱写
desired_caps['deviceName'] = 'samsung'
# 要打开的应用程序 包名
desired_caps['appPackage'] = 'com.android.settings'
# 要打开的界面 启动名 界面名
desired_caps['appActivity'] = '.Settings'

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 传入元素
e1 = driver.find_element(AppiumBy.XPATH,"//*[@text='WLAN']")
TouchAction(driver).tap(e1).perform()
# 传入坐标 要x= y= 显示指定
TouchAction(driver).tap(x=650,y=650).perform()

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time
from appium.webdriver.common.touch_action import TouchAction
# 启动一个字典 包装相应的启动参数
desired_caps = dict()
# 平台的名字,安卓还是IOS 大小写无所谓
desired_caps['platformName'] = 'Android'
# 平台的版本,(5.4.3 可以写 5.4.3 5.4 5)
desired_caps['platformVersion'] = '7.1.2'
# 设备的名字,随便写,不能乱写
desired_caps['deviceName'] = 'samsung'
# 要打开的应用程序 包名
desired_caps['appPackage'] = 'com.android.settings'
# 要打开的界面 启动名 界面名
desired_caps['appActivity'] = '.Settings'

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)


TouchAction(driver).press(x=650,y=650).perform()
time.sleep(2)

# 不会进去
TouchAction(driver).press(x=650,y=650).perform()
time.sleep(2)

# 得先按下的操作再抬起 不然会报错
TouchAction(driver).press(x=650,y=650).release().perform()
time.sleep(2)

driver.quit()

等待操作 暂停操作

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

el = driver.find_element(AppiumBy.XPATH,"//*[@text='WLAN']")
TouchAction(driver).tap(el).perform()
time.sleep(2)

el2 = driver.find_element(AppiumBy.XPATH,"//*[@text='WiredSSID']")
# 无等待
TouchAction(driver).press(el2).release().perform()
# 等待2秒钟 相当于长按的效果
TouchAction(driver).press(el2).wait(2000).release().perform()

 无停留效果

停留效果:

 

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

el = driver.find_element(AppiumBy.XPATH,"//*[@text='WLAN']")
TouchAction(driver).tap(el).perform()
time.sleep(2)

el2 = driver.find_element(AppiumBy.XPATH,"//*[@text='WiredSSID']")
TouchAction(driver).long_press(el2,duration=2000).perform()

 

 

 

移动操作

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time
from appium.webdriver.common.touch_action import TouchAction
# 启动一个字典 包装相应的启动参数
desired_caps = dict()
# 平台的名字,安卓还是IOS 大小写无所谓
desired_caps['platformName'] = 'Android'
# 平台的版本,(5.4.3 可以写 5.4.3 5.4 5)
desired_caps['platformVersion'] = '7.1.2'
# 设备的名字,随便写,不能乱写
desired_caps['deviceName'] = 'samsung'
# 要打开的应用程序 包名
desired_caps['appPackage'] = 'com.android.settings'
# 要打开的界面 启动名 界面名
desired_caps['appActivity'] = '.ChooseLockPattern'

# 连接appium服务器
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

TouchAction(driver).press(x=176,y=773).move_to(x=448,y=768).move_to(x=720,y=773).release().perform()
time.sleep(10)
driver.quit()

可以应用在图形手势密码时  

手机操作API

获取手机分辨率:

# 获取当前分辨率
print(driver.get_window_size())
# {'width': 900, 'height': 1600}
print(driver.get_window_size()['height'])

获取手机截图:

# 截图 文件路径
driver.get_screenshot_as_file("screen.png")

 获取和设置手机网络:

print(driver.network_connection) # 6
driver.set_network_connection(1)

 

做一个功能 当网络设置为data only的时候 提醒

导包:from appium.webdriver.connectiontype import ConnectionType

if driver.network_connection == ConnectionType.DATA_ONLY:
    print('1')
else:
    print('0')

发送键到设备

就是手机上的外部按键 !!

音量键 + 24 - 25

driver.press_keycode(24)
sleep(2)
driver.press_keycode(24)
sleep(2)
driver.press_keycode(25)
sleep(2)
driver.press_keycode(25)

打开通知栏:

driver.open_notifications()
sleep(2)
driver.press_keycode(4)

driver.quit()

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值