基于Appium搭建移动端自动化测试环境
模拟器环境搭建
- 下载桌面客户端
- 安装appium-doctor检测环境
npm install -g appium-doctor
// 检测环境
/usr/local/Cellar/node/13.12.0/bin/appium-doctor --ios
- 安装Python库依赖
pip3 install Appium-Python-Client
- 安装Carthage
brew install carthage
模拟器自动化运行项目
- 编写一个测试项目安装到模拟器
- 启动Appium桌面端,启动服务
- 编写Python自动化脚本,启动目标APP
#!/usr/local/bin/python3
# -*- coding:utf-8 -*-
from appium import webdriver
import time
desired_caps = dict()
# 平台是iOS
desired_caps["platformName"] = "iOS"
# 模拟器版本
desired_caps["platformVersion"] = "13.4"
# 模拟器设备名称
desired_caps["deviceName"] = "iPhone 11"
# 测试的app bundle id
desired_caps["app"] = "com.westone.AppiumDemo"
// 或者填路径
desired_caps["app"] = "/Users/30san/Desktop/appium_test/demo.app"
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
time.sleep(5)
# 退出
driver.quit()
安卓配置
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "6.0"
desired_caps["deviceName"] = "192.168.56.101:5555"
desired_caps["app"] = "/Users/30san/Desktop/appium_test/app-debug.apk"
desired_caps["allowTestPackages"] = True
真机环境配置及运行
- 配置WebDriverAgent
在Appium安装目录下找到WebDriverAgent,配置证书,然后将WebDriverAgentRunner安装到手机.
/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-webdriveragent
配置如下图三个targets的证书信息。
cmd + u将安装WebDriverAgentRunner到手机,Xcode打印如下信息代表环境配置成功
2020-09-15 09:38:48.633834+0800 WebDriverAgentRunner-Runner[14203:11031158] Built at Sep 15 2020 09:37:27
2020-09-15 09:38:48.665024+0800 WebDriverAgentRunner-Runner[14203:11031158] ServerURLHere->http://172.16.6.73:8100<-ServerURLHere
2020-09-15 09:38:48.665527+0800 WebDriverAgentRunner-Runner[14203:11031380] Using singleton test manager
在真机中自动化运行项目。流程和模拟器一致,只需修改Python脚本配置。
#!/usr/local/bin/python3
# -*- coding:utf-8 -*-
from appium import webdriver
import time
desired_caps = dict()
#真机设置
desired_caps["platformName"] = "iOS"
desired_caps["platformVersion"] = "13.4"
desired_caps["deviceName"] = "iPhone 11"
desired_caps["app"] = "com.westone.AppiumDemo"
desired_caps["udid"] = "00008030-000C55283A02802E"
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
time.sleep(5)
# 退出
driver.quit()
基础常用操作
- 安卓常用API
获取当前包名
driver.current_package
获取当前页面的activity
driver.current_activity
打开通知栏
driver.open_notifications()
获取手机网络类型
driver.network_connection
设置手机网络为WiFi
driver.set_network_connection(2)
发送按键到设备
driver.press_keycode(3) // 按下home键
- iOS常用API
查找元素
find_element_by_ios_class_chain
- 通用常用API
判断应用是否安装
driver.is_app_installed("com.example.appiumtest")
将应用放置到后台5秒,然后在回到前台
driver.background_app(5)
元素查找
find_element_by_id
find_element_by_class_name
find_element_by_xpath
元素操作
按钮点击 click()
输入框输入 send_keys("content")
输入框清空 clear()
获取文本内容 element.text
获取元素位置和大小 location size
获取元素的属性值 get_attribute
元素等待(隐示等待、显示等待)
隐示等待:所有定位元素的超时时间设置为同一个值的时候 获取driver对象之后设置等待时间
设置等待时间10秒,10秒内找到了立即进行后续操作,未找到就报错找不到该元素NoSuchElementException
driver.implicitly_wait(10)
显示等待:针对每一个元素设置不同的超时时间
wait = WebDriverWait(driver, 5)
wait.until(lambda x: x.find_element_by_class_name("XCUIElementTypeStaticText")).click()
滑动
driver.swipe(100, 400, 100, 100, 2)
driver.execute_script("mobile:scroll", {"direction": "down"})
从一个元素滑动到另一个元素
driver.scroll()
从一个元素滑动到另一个元素没有惯性滑动距离
driver.drag_and_drop()
高级手势TouchAction
1、创建action对象
2、通过对象执行的手势
3、通过platform执行
点击按钮
TouchAction(driver).tap(button).perform()
点击某一个坐标点
TouchAction(driver).tap(x=100, y=100).perform()
长按
TouchAction(driver).long_press(x=100, y=100).perform()
TouchAction(driver).long_press(button).perform()
按下
TouchAction(driver).press().perform()
抬起
TouchAction(driver).release().perform()
移动
TouchAction(driver).move_to().perform()
屏幕截图
driver.get_screenshot_as_file("/Users/30san/Desktop/appium_test/screen.png")
获取屏幕大小
driver.get_window_size()
Demo示例
将手机系统语言设置为中文。
- 启动appium
- iOS 查找目标元素
Android查找元素
1、启动模拟器
2、在Android SDK目录下找到uiautomatorviewer,双击启动该工具
/Users/30san/Library/Android/sdk/tools/bin/uiautomatorviewer
3、查找指定元素
- 编写Python脚本
#!/usr/local/bin/python3
# -*- coding:utf-8 -*-
from appium import webdriver
import time
desired_caps = dict()
desired_caps["platformName"] = "iOS"
desired_caps["platformVersion"] = "13.4"
desired_caps["deviceName"] = "iPhone 11"
desired_caps["app"] = "com.apple.Preferences"
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
# 隐试等待
driver.implicitly_wait(10)
time.sleep(5)
driver.find_element_by_xpath("//XCUIElementTypeCell[@name='General']").click()
driver.find_element_by_xpath("//XCUIElementTypeStaticText[@name='Language & Region']").click()
driver.find_element_by_xpath("//XCUIElementTypeCell[@name='iPhone Language']").click()
driver.find_element_by_xpath("//XCUIElementTypeStaticText[@name='简体中文']").click()
driver.find_element_by_ios_class_chain("**/XCUIElementTypeButton[`label == 'Change to Chinese, Simplified'`]").click()
time.sleep(10)
#关闭APP
driver.close_app()
# 退出
driver.quit()