手机操作API
1、获取手机分辨率
driver.get_window_size()
返回值:
字典类型
有两个key,分别是width和height
宽和高的值是int类型
2、手机截图
参数:
filename:指定路径下,指定格式的图片
driver.get_screenshot_as_file(filename)
代码:
from appium import webdriver
import timedesired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 获取手机的分辨率
print(driver.get_window_size())
# 获取手机的分辨率属性
print(driver.get_window_size()['width'])
# 获取手机的分辨率属性类型
print(type(driver.get_window_size()['width']))# 手机截图
driver.get_screenshot_as_file("C:/Users/j3027/Desktop/112.png")
3、获取和设置手机网络
获取手机网络
driver.network_connection
设置手机网络
参数:
connectionType:网络类型
driver.set_network_connection(connectionType)
注意点:
网络的类型,建议使用系统提供的类型
from appium.webdriver.connectiontype import connectionType
代码:
from appium import webdriver
from appium.webdriver.connectiontype import ConnectionType
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 获取当前手机网络类型
print(driver.network_connection)
# 设置手机网络
driver.set_network_connection(1)
#判断手机网络类型
if driver.network_connection == ConnectionType.DATA_ONLY:print(driver.network_connection)
else:driver.set_network_connection(ConnectionType.DATA_ONLY)print(4)
4、发送键到设备
参数:
keycode:发送给设备的关键代码
metastate:关于被发送的关键代码的元信息,一般为默认值
driver.press_keycode(keycode,metastate = None)
手机操作键编号:
举例:
from appium import webdriver
import timedesired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置手机音量键并退出设置界面
driver.press_keycode(24,metastate = None)
time.sleep(2)
driver.press_keycode(24,metastate = None)
time.sleep(2)
driver.press_keycode(25,metastate = None)
time.sleep(2)
driver.press_keycode(25,metastate = None)
time.sleep(2)
driver.press_keycode(4,metastate = None)
driver.press_keycode(4,metastate = None)
5、操作手机通知栏
打开手机通知栏:
driver.open_notification()
关闭通知栏
现实中通过手指向上滑动或者点击返回键关闭示例:打开手机通知栏并关闭
代码:
from appium import webdriver
import time
from appium.webdriver.common.touch_action import TouchActiondesired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 打开手机通知栏后并关闭
# 打开通知栏
driver.open_notifications()
# 打开后停顿3秒
time.sleep(3)
# 关闭通知栏
driver.press_keycode(4,metastate = None)
time.sleep(3)
driver.quit()
作为一个软件测试的过来人,我想尽自己最大的努力,帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源,现在免费分享给大家,有需要的小伙伴可以关注【公众号:开心螺蛳粉】自提!
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
行动吧,在路上总比一直观望的要好,未来的你肯定会感谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入群:1150305204,里面有各种测试开发资料和技术可以一起交流哦。