本章主题
设备基本操作
摇一摇
在设备上执行摇动动作
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
# 摇起来
self.driver.shake();
锁屏
锁屏操作,seconds设置锁屏时长
# 锁屏3s ==》 交叉事件
driver.lock(3)
print(driver.is_locked()) # 判断是否锁屏,是则返回false
解锁
解锁操作
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
time.sleep(2)
# 锁屏3s ==》 交叉事件
driver.lock(3)
print(driver.is_locked()) # 判断是否锁屏,是则返回false
# 解锁
driver.unlock()
time.sleep(3)
print(driver.is_locked())
- 组合使用 小tips
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
time.sleep(2)
# 锁屏3s ==》 交叉事件
driver.lock(3)
print(driver.is_locked()) # 判断是否锁屏,是啧返回false
# 解锁
driver.unlock()
time.sleep(3)
print(driver.is_locked())
判断是否锁屏
判断是否锁屏,锁屏状态返回True
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
time.sleep(2)
driver.lock(3)
print(driver.is_locked()) # 判断是否锁屏,是则返回false
查看屏幕状态
driver.orientation=‘LANDSCAPE’:切换横屏操作
driver.orientation=‘PORTRAIT’:切换竖屏操作
可以通过driver.orientation获取当前屏幕状态
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
# 横竖屏设置
driver.orientation = "LANDSCAPE" # 设置横屏
time.sleep(3)
driver.orientation = "PORTRAIT" # 设置竖屏
打开通知栏
打开通知栏
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
# 打开通知栏
driver.open_notifications()
print(driver.orientation)
获取网络状态
network_connection:获取网络状态,返回整型数字
0(None)、1(AirplaneMode)、2(Wifionly)、4(Dataonly)、6(Allnetworkon)
network_connection:获取网络状态,返回整型数字0(None)、1(AirplaneMode)、2(Wifionly)、4(Dataonly)、6(Allnetworkon)
# 网络配置
print(driver.network_connection) # 查看当前网络状态
driver.set_network_connection(1) # 飞行模式 0 1 2 4 6
time.sleep(5)
# set_network_connection(self,connection_type):设置网络状态,使用数字或导入ConnectionType类进行传参设置
driver.set_network_connection(ConnectionType.ALL_NETWORK_ON) # 全开
获取系统时间
获取设备上的时间
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
time = self.driver.device_time
time = self.driver.get_device_time()
time = self.driver.get_device_time("YYYY-MM-DD")
获取屏幕分辨率
get_window_size(self,windowHandle=‘current’):默认获取当前屏幕的分辨率,返回字典{‘width’:,‘height’:}
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
window = driver.get_window_size()
print(window)
保存截图
save_screenshot(self,filename):截屏操作,filename为保存的文件路径,图片格式设置为png格式
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.wondertek.paper",
"appActivity": "cn.thepaper.paper.ui.main.MainActivity",
"udid": "192.168.56.104:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.save_screenshot('./pengpai.png') # 传入存放路径
按键类基本操作
按键类操作用来模拟在手机设备上进行按键操作
模拟按键输入
press_keycode(self,keycode,metastate=None,flags=None):模拟按键输入
其中:
keycode:发送到设备的键值编码可以通过AndroidKeyCode进行查询对应数值
metastate:将被发送的元信息
flags:设置的按键事件标记
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.56.104:5555",
"noReset": True
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
time.sleep(2)
driver.find_element(By.XPATH, '//android.widget.TextView[@text="在设置中搜索"]').click()
time.sleep(1)
driver.press_keycode(29, 64, 59) # a 打开左边shift键的开关 按住左边shift键 == A 组合键
time.sleep(1)
driver.press_keycode(29, 128, 60) # a 打开右边的shift键开关 按住右边shift键 == A
time.sleep(1)
driver.press_keycode(29, 1048576) # a 打开大小写开关
time.sleep(1)
driver.press_keycode(29, 1) # 1 打开shift键
模拟按键输入2
keyevent(self,keycode,metastate=None):模拟按键输入
其中:
keycode:发送到设备的键值编码可以通过Android KeyCode进行查询对应数值
metastate:将被发送的元信息该方法和press_keycode()方法类似
推荐使用press_keycode
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.connectiontype import ConnectionType
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "mac虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.56.104:5555",
"noReset": True
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
time.sleep(2)
driver.keyevent(29) #输入a
driver.keyevent(30) #输入b
模拟长按事件
long_press_keycode(self,keycode,metastate=None,flags=None):模拟长按按键操作
参数不做描述,和之前的含义一样。一般用来模拟长按手机上某个按钮,比如长按电源键,长按home键
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
touch.long_press_keycode(10)
查看当前键盘状态
是否显示软键盘
如果显示则返回ture
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
# 判断是否显示键盘
driver.is_keyboard_shown()
隐藏键盘
隐藏软键盘
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
# 俺消失了。。。
self.driver.hide_keyboard()
点击/滑动操作
在进行app自动化的时候,经常会进行点击或滑动的操作,比如点击坐标,左右滑动,上下滑动等,Appium相应提供了解决方案
轻按设备
轻按屏幕启用设备
- demo1
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
actions = TouchAction(driver)
actions.tap(element)
actions.perform()
- demo2
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
actions = TouchAction(driver)
actions.tap(x=720, y=1910).perform() # 点击指定坐标
- 图示
模拟滑动操作
scroll(self,origin_el,destination_el,duration=None):模拟从元素origin_el滚动至元素destination_el
从element1 移动到element2
# -*- coding: utf-8 -*-
# @Time : 2022/1/17 9:33
# @Author : Limusen
# @File : demo_look_11
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
web_element_e1 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="帐号"]')
web_element_e2 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="存储"]')
# 从e1 移动到e2
driver.scroll(web_element_e1,web_element_e2)
- 图示
拖拽元素
drag_and_drop(self,origin_el,destination_el):将元素origin_el拖到目标元素destination_el
将element1 拖动到element2
# -*- coding: utf-8 -*-
# @Time : 2022/1/17 9:33
# @Author : Limusen
# @File : demo_look_11
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
web_element_e1 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="帐号"]')
web_element_e2 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="存储"]')
driver.drag_and_drop(web_element_e1,web_element_e2)
- 图示
滑动flick
flick(self,start_x,start_y,end_x,end_y):按住A点后快速滑动至B点,A点的坐标为start_x,start_y,B点的坐标为end_x,end_y
# -*- coding: utf-8 -*-
# @Time : 2022/1/17 9:33
# @Author : Limusen
# @File : demo_look_11
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
# 元素a的x坐标720 y坐标1910 移动到元素b,x坐标720 y坐标1000
driver.flick(720, 1910, 720, 1000)
- 图示
滑动swipe
swipe(self,start_x,start_y,end_x,end_y,duration=None):从A点滑动至B点,滑动时间为毫秒,A点的坐标为start_x,start_y,B点的坐标为end_x,end_y
# -*- coding: utf-8 -*-
# @Time : 2022/1/17 9:33
# @Author : Limusen
# @File : demo_look_11
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.swipe(720, 1910, 860, 1000,3000)
- 图示
TouchAction辅助类
TouchAction操作讲解:Appium的辅助类,主要针对手势操作,比如滑动、长按、拖动等。其原理是将一系列的动作放在一个链条中,然后将该链条传递给服务器。服务器接受到该链条后,解析各个动作,逐个执行。
TouchAction类支持的动作很多,常用的api如下:
1、press(self,el=None,x=None,y=None):按压一个元素或坐标,le为要点击的元素,x/y为坐标
2、long_press(self,el=None,x=None,y=None,duration=1000):长按压一个元素或坐标,默认长按时间1000ms
3、tap(self,element=None,x=None,y=None,count=1):对一个元素或控件执行点击操作
4、move_to(self,el=None,x=None,y=None):将指针从上一个点移动到指定的元素或点
5、wait(self,ms=0):等待时间,单位毫秒
6、release(self):释放,结束屏幕上的一系列动作的命令操作
7、perform(self):执行,将待执行的操作发送到服务器的命令操作
TouchAction – press
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
touch = TouchAction(driver) # 需要先定义一个TouchAction对象
touch.press(x=720, y=1910).perform()
- 图示
TouchAction – long_press
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
touch = TouchAction(driver)
#touch.press(x=720, y=1910).perform()
touch.long_press(x=720, y=1910).perform()
- 图示
TouchAction – tap 对指定元素进行点击
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import By
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
touch = TouchAction(driver)
touch.tap(x=720, y=1910).perform() # 点击指定坐标
- 图示
TouchAction – move_to
import time
from appium import webdriver
from appium.webdriver.webdriver import By
from appium.webdriver.common.touch_action import TouchAction
des = {
"platformName": "Android",
"platformVersion": "9",
"deviceName": "windwos虚拟机",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings",
"udid": "192.168.0.101:5555",
"noReset": "True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
driver.implicitly_wait(10)
element_01 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="屏幕锁定"]')
TouchAction(driver).press(element_01).release().perform()
time.sleep(2)
element_02 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="屏幕锁定"]')
TouchAction(driver).press(element_02).release().perform()
time.sleep(2)
element_03 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="图案"]')
TouchAction(driver).press(element_03).release().perform()
"""
339 1287
721 1287
721 1662
721 2043
339 1665
1101 1667
"""
touch_action = TouchAction(driver)
touch_action.press(x=339, y=1297).wait(1000).move_to(x=721, y=1287).wait(1000).move_to(x=721, y=1662).wait(
1000).move_to(x=721, y=2043).wait(1000).move_to(x=339, y=1665).wait(1000).move_to(x=1101, y=1667).wait(
1000).release().perform()
- 图示
总结
-
本章总结
本章节主要是讲述了如何对设备进行一些常用的操作,比如键盘,点击,touch等,方便我们之后的操作练习… 如果对大家有帮助的话不妨点个赞 谢谢
代码地址
https://gitee.com/todayisgoodday/PythonAppnium
博客园地址
https://www.cnblogs.com/yushengaqingzhijiao/category/2024559.html