一、前期准备
1、JAVA环境
(1)首先下载安装
(2)配置环境变量,jdk8为例
变量值为安装路径
修改path
(3)cmd中输入java —version验证
2、Android Studio安装与配置
(1)正常下载安装即可
(2)不行的话这里教程
3、Appium Server
下载了直接安装就好
4、Appium inspector
同样也是直接安装就好
5、ADB
(1)下载了Android Studio后会有一个SDK文件夹,在这个文件夹上修改环境变量
(2)验证cmd,输入adb devices即可
6、node.js
(1)下载安装
(2)全部默认直奔环境变量
需要在安装目录新增两个文件夹
(3)编辑用户变量
(4)验证
输入npm -v node -v验证是否安装成功
7、Pycharm
这还要讲?安装好了执行pip install Appium-Python-Client
二、详细流程
情况一:使用模拟机
(1)先启动AndroidStudio虚拟机
(2)通过执行下面代码查看信息
查版本号:adb shell getprop ro.build.version.release 查设备名(模拟机显示SDK):adb devices -l 查打开的应用信息:adb shell dumpsys window | findstr mCurrentFocus
(3)准备一个字典,输入刚才的信息
{ "platformName": "Android", "platformVersion": "10", "deviceName": "", "appPackage": "com.google.android.apps.messaging", "appActivity": ".ui.ConversationListActivity", "resetKeyboard": "true", }
(4)先运行Appium Server
首先点击编辑配置检查
同时修改主机为127.0.0.1,启动!!!
(5)打开Appium inspector
将准备好的字典输入到这里
启动!!!
(6)录制脚本
嘎嘎点就行
这里就可以看到完整的代码
放到Pycharm就可以运行了,使用的是webdriver,所以xpath应该也可以用,更精准
示例,打开短信输入1
# { # "platformName": "Android", # "platformVersion": "10", # "deviceName": "", # "appPackage": "com.google.android.apps.messaging", # "appActivity": ".ui.ConversationListActivity", # "resetKeyboard": "true", # } # This sample code supports Appium Python client >=2.3.0 # pip install Appium-Python-Client # Then you can paste this into a file and simply run with Python from appium import webdriver from appium.options.common.base import AppiumOptions from appium.webdriver.common.appiumby import AppiumBy # For W3C actions from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.actions import interaction from selenium.webdriver.common.actions.action_builder import ActionBuilder from selenium.webdriver.common.actions.pointer_input import PointerInput options = AppiumOptions() options.load_capabilities({ "platformName": "Android", "appium:platformVersion": "10", "appium:deviceName": "", "appium:appPackage": "com.google.android.apps.messaging", "appium:appActivity": ".ui.ConversationListActivity", "appium:resetKeyboard": "true", "appium:ensureWebviewsHavePages": True, "appium:nativeWebScreenshot": True, "appium:newCommandTimeout": 3600, "appium:connectHardwareKeyboard": True }) driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=options) actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(885, 1659) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.pause(0.1) actions.w3c_actions.pointer_action.release() actions.perform() actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(338, 268) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.pause(0.1) actions.w3c_actions.pointer_action.release() actions.perform() actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(70, 1707) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.pause(0.1) actions.w3c_actions.pointer_action.release() actions.perform() actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(66, 1237) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.pause(0.1) actions.w3c_actions.pointer_action.release() actions.perform() actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(1006, 1718) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.pause(0.1) actions.w3c_actions.pointer_action.release() actions.perform() driver.quit()
情况二:使用真机
(1)和模拟机一样的流程,不同的点是需要USB链接电脑
三、事件操作
1、长按事件
def long_press(x, y, duration=1.0): actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(x, y) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.pause(duration) # 持续按住时间 actions.w3c_actions.pointer_action.release() actions.perform()
2、滑动事件
def swipe(start_x, start_y, end_x, end_y, duration=0.5): actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(start_x, start_y) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.move_to_location(end_x, end_y) # 滑动到目标点 actions.w3c_actions.pointer_action.pause(duration) # 滑动时长 actions.w3c_actions.pointer_action.release() actions.perform()
3、拖拽事件
def drag_and_drop(start_x, start_y, end_x, end_y, hold_time=0.5): actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(start_x, start_y) actions.w3c_actions.pointer_action.pointer_down() # 按住起点 actions.w3c_actions.pointer_action.pause(hold_time) # 模拟按住一段时间 actions.w3c_actions.pointer_action.move_to_location(end_x, end_y) # 拖动到目标点 actions.w3c_actions.pointer_action.release() actions.perform()
4、捏合/开事件
# 放大 def pinch_out(center_x, center_y, distance=100): actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) # 第一个手指 actions.w3c_actions.pointer_action.move_to_location(center_x, center_y) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.move_to_location(center_x + distance, center_y + distance) # 第二个手指 actions.w3c_actions.pointer_action.move_to_location(center_x, center_y) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.move_to_location(center_x - distance, center_y - distance) actions.w3c_actions.pointer_action.release() actions.perform() # 缩小 def pinch_in(center_x, center_y, distance=100): actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) # 第一个手指 actions.w3c_actions.pointer_action.move_to_location(center_x + distance, center_y + distance) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.move_to_location(center_x, center_y) # 第二个手指 actions.w3c_actions.pointer_action.move_to_location(center_x - distance, center_y - distance) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.move_to_location(center_x, center_y) actions.w3c_actions.pointer_action.release() actions.perform()
5、双击事件
def double_tap(x, y, pause_time=0.1): actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) # 第一次点击 actions.w3c_actions.pointer_action.move_to_location(x, y) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.release() actions.perform() time.sleep(pause_time) # 快速暂停,模拟双击 # 第二次点击 actions.w3c_actions.pointer_action.move_to_location(x, y) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.release() actions.perform()
6、点击事件
def tap_screen(x, y, pause_time=0.2): actions = ActionChains(driver) actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) actions.w3c_actions.pointer_action.move_to_location(x, y) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.pause(0.1) # 点击时间 actions.w3c_actions.pointer_action.release() actions.perform() time.sleep(pause_time)
四、Xpath定位
from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy # 假设已经启动 Appium 会话 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options=options) # 定位单个元素 element = driver.find_element(AppiumBy.XPATH, "//android.widget.TextView[@text='Login']") # 对定位到的元素进行操作 element.click()
五、遇到的问题
1、Android Studio新增机器会报AVD错误
解决:
(1)先去C盘目录C:\Users\Administrator\.android下找到avd文件夹,修改为AVD,复制到D盘
(2)在环境变量的系统变量中新增
(3)在设置中记得勾选
2、Android Studio下载gradle失败
(1)先找到他报错时给出的网站,到网站中将压缩包下载下来
(2)找到这个路径
(3)进入到详细的地址:C:\Users\Administrator\.gradle\wrapper\dists\gradle-8.7-bin\bhs2wmbdwecv87pi65oeuq5iu
(4)全部删掉,把压缩包放进去
3、Appium inspector启动失败
可能是远程路径没配,他默认是/,修改成/wd/hub
4、使用真机每次启动都会重置app
修改参数为如下:
"platformName": "Android", "appium:platformVersion": "14", "appium:deviceName": "MAA_AN10", "appium:appPackage": "com.tencent.mobileqq", "appium:appActivity": "com.tencent.mobileqq.activity.SplashActivity", "appium:resetKeyboard": True, "appium:ensureWebviewsHavePages": True, "appium:nativeWebScreenshot": True, "appium:newCommandTimeout": 3600, "appium:connectHardwareKeyboard": True, 'noReset': True, # 不重置应用 'fullReset': False, # 不重置缓存 'dontStopAppOnReset': True, # 应用不关闭 "unicodeKeyboard": True # 支持中文输入
本文参考来源: