Appium
1、基于python的测试框架
- 编程语言:python 客户端
- IDE:pycharm
- 客户端安装包:appium_python_client
- 多版本隔离工具:venv
- 国内依赖源:http://pypi.douban.com/simple/
- 测试框架:pytest, unittest, nose 推荐 pytest
2、代码等待
隐式等待:
1、定义:服务端在设置的时间内不断查找元素,直到查找到元素或达到等待时间为止,作用于全局
2、代码:driver.implicitly_wait(10)
显式等待:
1、定义:客户端(即用例端)固定等待设置的时间,作用于特定代码快
2、代码:
from time import sleep
sleep(20)
**备注:**隐式等待和 显式等待可以结合使用,全局使用隐式等待,在特定为止使用显式等待
3、 常用定位方式
- List item
- id
- accessbilityID
- XPath
-className 不常用
-image
-UIautomator2 安卓
-XCUITest IOS
4、常见定位方法
findElementByXXX()
findElementBy(By,value) #PO 设计模式使用
5、常用自动化API
- click
- sendkeys
- swipe
- TouchActions
TouchActions
1、定义:对元素执行tap操作
Perform a tap action on the element
2、使用方法:
from appium.webdriver.common.touch_action import TouchAction
// ...
actions = TouchAction(driver)
actions.tap_and_hold(20, 20)
actions.move_to(10, 100)
actions.release()
actions.perform()
3、链式操作:
举例:执行长按-拖动-释放长按-提交操作
TouchAction(driver).long_press().move_to().release().perform()
swipe
1、定义:从一个点到另一个点滑动,持续时间可选
Swipe from one point to another point, for an optional duration
2、方法:driver.swipe()
3、适用性:
针对分辨率不同的手机不适用,不能使用绝对位置,应该使用相对位置,后续如用兼容性测试应对swipe方法进行封装
6、 pytest
命令行安装pytest: pip install -U pytest
查看pytest 版本: pytest --version
This is pytest version 5.3.5。。。
基于pytest的脚本:
from appium import webdriver
class TestDemo:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android' # 设备系统
desired_caps['platformVersion'] = '6.0.1' # 设备系统版本
desired_caps['deviceName'] = '127.0.0.1:62001' # 设备名称
desired_caps['appPackage'] = '包名' # 测试app包名
desired_caps['appActivity'] = 'app启动参数'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 启动app
self.driver.implicitly_wait(20) #设置隐式等待
def test_lanuch(self):
driver = self.driver
# 等待10秒
driver.implicitly_wait(10)
# #同意协议
el1 = driver.find_element_by_id("tv.danmaku.bili:id/agree")
el1.click()
# 定位游戏手柄
el2 = driver.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.support.v4.widget.DrawerLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.LinearLayout/android.view.ViewGroup/android.support.v7.widget.LinearLayoutCompat/android.widget.FrameLayout[1]/android.widget.ImageView[2]")
el2.click()
def teardown(self):
self.driver.quit()
运行前启动
启动手机/模拟器
启动APPium
直接在控制台启动APPium Server