Python+Appium【第四章】设备操作

阅读 27

2022-01-17

本章主题

设备基本操作

摇一摇


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();

锁屏

# 锁屏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())

判断是否锁屏

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

查看屏幕状态


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)

# 网络配置
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")

获取屏幕分辨率

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)

保存截图

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')  # 传入存放路径

按键类基本操作

按键类操作用来模拟在手机设备上进行按键操作

模拟按键输入

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

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

模拟长按事件

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)

查看当前键盘状态

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() # 点击指定坐标
  • 图示
    在这里插入图片描述
    在这里插入图片描述

模拟滑动操作

# -*- 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)

  • 图示
    在这里插入图片描述

拖拽元素

# -*- 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


# -*- 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

# -*- 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如下:

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

精彩评论(0)

0 0 举报