基本操作篇
以下源码中 driver 皆为session对象
启动会话
from appium import webdriver
desired_caps = {}
desired_caps["automationName"] = "UiAutomator2"
desired_caps["chromedriverExecutable"] = "D:\\code\\softspace\\66-68\\chromedriver.exe"
desired_caps["platformName"] = "Android" # 系统名称
desired_caps["platformVersion"] = "7.1.2" # 系统版本
desired_caps["deviceName"] = "Android Emulator" # 设备名称
desired_caps["appPackage"] = 'app_package' # 应用包名
desired_caps["appActivity"] = 'app_activity' # 应用入口页面
desired_caps["noReset"] = True # 不重路应用的状态
driver = webdriver.Remote(desired_capabilities=desired_caps) # 实例化会话
关闭会话
driver.quit()
API
app的活跃页面(当前页面)
activity = driver.current_activity # 常用于断言
当前页结构源码
source = driver.page_source()
重置数据
driver.reset()
页面跳转
driver.start_activity(app_package, app_activity, **opts)
# 应用包,应用页面,其他参数(Keyword Args)
"""
Keyword Args:
app_wait_package (str): Begin automation after this package starts.
app_wait_activity (str): Begin automation after this activity starts.
intent_action (str): Intent to start.
intent_category (str): Intent category to start.
intent_flags (str): Flags to send to the intent.
optional_intent_arguments (str): Optional arguments to the intent.
dont_stop_app_on_reset (str): Should the app be stopped on reset?
"""
检测是否已安装某app
driver.is_app_installed(app_package)
# 安卓系统传包名即可
安装app
driver.install_app(app_path, **option)
"""
Args:
app_path (str): the local or remote path to the application to install
Keyword Args:
replace (bool): [Android only] whether to reinstall/upgrade the package if it is
already present on the device under test. True by default
timeout (int): [Android only] how much time to wait for the installation to complete.
60000ms by default.
allowTestPackages (bool): [Android only] whether to allow installation of packages marked
as test in the manifest. False by default
useSdcard (bool): [Android only] whether to use the SD card to install the app. False by default
grantPermissions (bool): [Android only] whether to automatically grant application permissions
on Android 6+ after the installation completes. False by default
"""
关闭app
driver.close_app()
元素操作
得到元素对象的属性
driver.find_element(MB.ANDROID_UIAUTOMATOR, 'new UiSelector().text("")').get_attribute(name) # name为属性名
清除输入框
该方法为元素对象方法
driver.find_element(MB.ANDROID_UIAUTOMATOR, 'new UiSelector().text("")').clear()
页面操作
滑动
滑动可与MultiAction类结合实现放大、缩小等多指操作
from appium import webdriver
driver = webdriver.Remote()
# 滑动
driver.swipe(start_x, start_y, end_x, end_y, duration=None) # 开始X,开始Y,结束X,结束Y,执行时间ms
以下为实现源码。tips:利用TouchAction&MultiAction实现zoom和pinch可见link
# 可以看见滑动操作是由TouchAction&MultiAction实现
from appium.webdriver.common.touchaction import TouchAction
from appium.webdriver.common.multiaction import MultiAction
action = TouchAction(self)
action \
.press(x=start_x, y=start_y) \
.wait(ms=duration) \
.move_to(x=end_x, y=end_y) \
.release()
action.perform()
return self
多指点击
该方法实现也是由TouchAction&MultiAction实现
最多支持五个手指操作
from appium import webdriver
driver = webdriver.Remote()
driver.tap(positions, duration=None)
# positions = [(x, y), (x,y), (x,y), (x,y), (x,y)] 点击坐标点,几个手指就几个元组
# duration执行时间 ms
事件操作
硬件事件
列举几个常用命令
键名 | 描述 | 键值 |
---|---|---|
KEYCODE_CALL | 拨号键 | 5 |
KEYCODE_ENDCALL | 挂机键 | 6 |
KEYCODE_HOME | HOME键 | 3 |
KEYCODE_MENU | 菜单键 | 82 |
KEYCODE_BACK | 返回键 | 4 |
KEYCODE_SEARCH | 搜索键 | 84 |
KEYCODE_CAMERA | 拍照键 | 27 |
KEYCODE_FOCUS | 对焦键 | 80 |
KEYCODE_POWER | 电源键 | 26 |
KEYCODE_NOTIFICATION | 通知键 | 83 |
KEYCODE_MUTE | 话筒静音键 | 91 |
KEYCODE_VOLUME_MUTE | 扬声器静音键 | 164 |
KEYCODE_VOLUME_UP | 声音+ | 24 |
KEYCODE_VOLUME_DOWN | 声音- | 25 |
官方文档https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_CALENDAR
driver.press_keycode(keycode, metastate=None, flags=None)
# keycode为键值
截图
appium的webdriver继承了selenium的webdriver方法,所以此处截图沿用selenium中的截图方法。
from appium import webdriver
driver = webdriver.Remote()
driver.save_screenshot(filename) # 最常用这种方法,filename为存放路径
driver.get_screenshot_as_png() # 返回二进制数据
driver.get_screenshot_as_base64() # 返回 base64格式数据 多结合allure.attach使用
driver.get_screenshot_as_file(filename) # filename为存放路径
切到后台
将当前的应用切换至后台
from appium import webdriver
driver = webdriver.Remote()
driver.background_app(seconds) # -1为永久置于后台,s