1
创建新的会话
- desired_caps = desired_caps = {
- platformName: Android,
- platformVersion: 7.0,
- deviceName: Android Emulator,
- automationName: UiAutomator2,
- app: PATH(/path/to/app)
- }
- self.driver = webdriver.Remote(http://127.0.0.1:4723/wd/hub, desired_caps)
- 2
结束会话
- self.driver.quit()
- 3
获取会话功能
- desired_caps = self.driver.desired_capabilities()
- 4
回退 (仅限 Web 上下文)
- self.driver.back()
- 5
截图
- screenshotBase64 = self.driver.get_screenshot_as_base64()
- 6
超时
设置超时
self.driver.set_page_load_timeout(5000)
设置隐式等待超时
self.driver.implicitly_wait(5000)
设置脚本超时
设置的时间量,
以毫秒为单位,
通过执行异步脚本,
执行异步允许运行它们都将立即中止之前 (仅限于 Web 上下文)
- self.driver.set_script_timeout(5000)
- 7
方向
获取定位
orientation = self.driver.orientation()
设置方向
- driver.orientation = "LANDSCAPE"
- 8
地理物质
获取地理位置
location = self.driver.location()
设置地理位置
- self.driver.set_location(49, 123, 10)
- 9
日志
获取可用的日志类型
log_types = driver.log_types()
获取日志
- logs = driver.get_log(driver)
- 10
设置
更新设备设置
# Python 不支持
检索设备设置
- # Python 不支持
- 11
活动
开始活动
self.driver.start_activity("com.example", "ActivityName")
获取当前活动
activity = self.driver.current_activity()
获取当前包
- package = self.driver.current_package()
- 12
应用
安装应用程序
self.driver.install_app(/Users/johndoe/path/to/app.apk)
应用程序已安装
self.driver.is_app_installed(com.example.AppName)
启动应用程序
self.driver.launch_app()
背景应用程序
将当前正在运行的应用程序发送到后台
self.driver.background_app(10)
关闭应用程序
self.driver.close_app()
重置应用程序
self.driver.reset()
删除应用程序
self.driver.remove_app(com.example.AppName)
获取应用程序字符串
appStrings = self.driver.app_strings("en", "/path/to/file")
获取测试覆盖率数据
- self.driver.end_test_coverage("Intent", "/path")
- 13
文件
推送文件
self.driver.push_file(/path/to/device/foo.bar, QXJlIHlvdXIgYmVlcnMgb2theT8=)
拉文件
file_base64 = self.driver.pull_file(/path/to/device/foo.bar)
拉文件夹
- folder_base64 = self.driver.pull_folder(/path/to/device/foo.bar)
- 14
动作
摇晃
self.driver.shake()
锁定
self.driver.lock()
解锁
self.driver.unlock()
设备是否被锁定
# Python 不支持
旋转
- # Python 不支持
- 15
按键
按键代码
self.driver.press_keycode(10)
长按键代码
self.driver.long_press_keycode(10)
隐藏键盘
self.driver.hide_keyboard()
显示键盘
- # Python 不支持
- 16
网络
切换飞行模式
# Python 不支持
切换数据服务
# Python 不支持
切换 WiFi
# Python 不支持
切换位置服务的状态
self.driver.toggle_location_services()
模拟短信 (仅适用于仿真器)
# Python 不支持
拨打 GSM 电话 (仅限 Emulator)
# Python 不支持
设置 GSM 信号强度 (仅限仿真器)
# Python 不支持
设置 GSM 语音状态 (仅适用于仿真器)
- # Python 不支持
- 17
性能数据
获取性能数据
返回支持读取的系统状态信息, 如 CPU, 内存, 网络流量和电池
# Python 不支持
获取性能数据类型
- # Python 不支持
- 18
模拟器
执行 Touch ID
模拟 Touch ID 事件 (仅适用于 iOS 模拟器)
- self.driver.touch_id(false)
- # 模拟失败的触摸
- self.driver.touch_id(true)
- # 模拟通过的触摸
切换正在注册的模拟器以接受 Touch ID(仅适用于 iOS 模拟器)
- self.driver.toggle_touch_id_enrollment()
- 19
系统
打开 Android 通知 (仅适用于仿真器)
self.driver.open_notifications()
获取系统栏
检索状态和导航栏的可见性和边界信息
# Python 不支持
获取系统时间
- time = self.driver.device_time()
- 20
查找元素
- el = self.driver.find_element_by_accessibility_id(SomeAccessibilityID)
- 21
操作
点击
- el = self.driver.find_element_by_accessibility_id(SomeId)
- el.click()
输入
self.driver.find_element_by_accessibility_id(SomeAccessibilityID).send_keys(Hello world!)
清除元素的值
- self.driver.find_element_by_accessibility_id(SomeAccessibilityID).clear()
- 22
属性
获取元素文本
- el = self.driver.find_element_by_accessibility_id(SomeAccessibilityID)
- text = el.text
获取标签名称
tagName = self.driver.find_element_by_accessibility_id(SomeAccessibilityID).tag_name
获取元素属性
tagName = self.driver.find_element_by_accessibility_id(SomeAccessibilityID).get_attribute(content-desc)
元素被选中
确定是否选择了表单或表单类元素 (复选框, 选择等)
self.driver.find_element_by_accessibility_id(SomeAccessibilityID).is_selected()
元素已启用
确定元素当前是否启用
self.driver.find_element_by_accessibility_id(SomeAccessibilityID).is_enabled()
获取元素位置
确定元素在页面或屏幕上的位置
location = self.driver.find_element_by_accessibility_id(SomeAccessibilityID).location
获取元素大小
以像素为单位确定元素的大小
size = self.driver.find_element_by_accessibility_id(SomeAccessibilityID).size
获取元素矩形
获取元素的尺寸和坐标
# Python 不支持
获取元素 CSS 值
查询 Web 元素的计算 CSS 属性的值
cssProperty = self.driver.find_element_by_accessibility_id(SomeId).value_of_css_property("style")
在视图中获取元素位置
一旦将元素滚动到视图中, 确定元素在屏幕上的位置 (主要是内部命令并且不被所有客户端支持)
- # Python 不支持
- 23
提交表单
提交一个 FORM 元素
- el = self.driver.find_element_by_accessibility_id(SomeAccessibilityID)
- el.submit();
- 24
获取活动元素
- element = driver.switch_to.active_element
- 25
元素是否相等
- # Python 不支持
- 26
上下文
获取当前上下文
context = driver.current_context
或者
context = driver.context
获取所有上下文
contexts = driver.contexts
设置当前上下文
- webview = driver.contexts[1]
- driver.switch_to.context(webview)
- # ...
- driver.switch_to.context(NATIVE_APP)
- 27
鼠标
将鼠标移至
- actions = ActionChains(driver)
- actions.move_to(element, 10, 10)
- actions.perform()
在当前鼠标坐标点击任意鼠标按钮
- actions = ActionChains(driver)
- actions.move_to_element(element)
- actions.click()
- actions.perform()
双击当前鼠标坐标 (由 moveto 设置)
- actions = ActionChains(driver)
- actions.move_to_element(element)
- actions.double_click()
- actions.perform()
按钮关闭
在当前的鼠标坐标上单击并按住鼠标左键
- actions = ActionChains(driver)
- actions.move_to_element(element)
- actions.click_and_hold()
- actions.perform()
扣紧
释放先前保持的鼠标按钮
- actions = ActionChains(driver)
- actions.move_to_element(element)
- actions.click_and_hold()
- actions.move_to_element(element, 10, 10)
- action.release()
- actions.perform()
- 28
触摸
单击轻触设备
- actions = TouchActions(driver)
- actions.tap(element)
- actions.perform()
双击
使用手指动作事件双击触摸屏
- actions = TouchActions(driver)
- actions.double_tap(element)
- actions.perform()
移动
手指在屏幕上移动
- actions = TouchActions(driver)
- actions.tap_and_hold(element)
- actions.move(50, 50)
- actions.perform()
触摸下来
手指在屏幕上
- actions = TouchActions(driver)
- actions.tap_and_hold(element)
- actions.move(50, 50)
- actions.perform()
润色 (做小的修改)
手指在屏幕上
- actions = TouchActions(driver)
- actions.tap_and_hold(20, 20)
- actions.release(50, 50)
- actions.perform()
长按
使用手指运动事件长按触摸屏
- actions = TouchActions(driver)
- actions.long_press(element)
- actions.perform()
滚动
使用基于手指的动作事件在触摸屏上滚动
- actions = TouchActions(driver)
- actions.scroll_from_element(element, 10, 100)
- actions.scroll(10, 100)
- actions.perform()
拂去
使用手指运动事件轻击触摸屏
- actions = TouchActions(driver)
- actions.flick_element(element, 1, 10, 10)
- actions.perform()
多点触摸执行
执行多点触摸动作序列
- from appium.webdriver.common.touch_action import TouchAction
- from appium.webdriver.common.multi_action import MultiAction
- a1 = TouchAction()
- a1.press(10, 20)
- a1.move_to(10, 200)
- a1.release()
- a2 = TouchAction()
- a2.press(10, 10)
- a2.move_to(10, 100)
- a2.release()
- ma = MultiAction(self.driver)
- ma.add(a1, a2)
- ma.perform()
触摸执行
执行一个触摸动作序列
- 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()
- 29
窗口
切换到窗口
将焦点更改为另一个窗口 (仅限 Web 上下文)
self.driver.switch_to.window("handle")
关闭窗口
关闭当前窗口 (仅限 Web 上下文)
self.driver.close()
获取窗口句柄
检索当前窗口句柄 (仅限 Web 上下文)
window_handle = self.driver.current_window_handle()
获取所有窗口句柄
检索可用于会话的所有窗口句柄的列表 (仅限 Web 上下文)
window_handles = self.driver.window_handles()
获取标题
获取当前页面标题 (仅限 Web 上下文)
title = self.driver.title()
获取窗口大小
获取指定窗口的大小 (仅限 Web 上下文)
- handle_one_size = self.driver.get_window_size()
- handle_two_size = self.driver.get_window_size("handleName")
设置窗口大小
更改指定窗口的大小 (仅限 Web 上下文)
self.driver.set_window_size(10, 10)
获取窗口位置
获取指定窗口的位置 (仅限 Web 上下文)
- handle_one_position = self.driver.get_window_position()
- handle_two_position = self.driver.get_window_position("handleName")
设置窗口位置
更改指定窗口的位置 (仅限 Web 上下文)
self.driver.set_window_position(10, 10)
最大化窗口
最大化指定的窗口 (仅限 Web 上下文)
- self.driver.maximize_window()
- 30
导航
导航到 URL
导航到新的 URL(仅限 Web 上下文)
self.driver.get("http://appium.io/")
获取 URL
检索当前页面的 URL(仅限 Web 上下文)
url = self.driver.current_url()
回去
如果可能, 在浏览器历史记录中向后浏览 (仅限 Web 上下文)
self.driver.back()
前进
如果可能, 在浏览器历史记录中向前浏览 (仅限 Web 上下文)
self.driver.forward()
刷新
刷新当前页面 (仅限 Web 上下文)
- self.driver.refresh()
- 31
- Cookie
获取所有 Cookie
检索当前页面可见的所有 cookie(仅限 Web 上下文)
cookies = self.driver.get_cookies()
设置 Cookie
设置一个 cookie(仅限 Web 上下文)
self.driver.add_cookie({name: foo, value: bar})
删除 Cookie
删除具有给定名称的 cookie(仅限 Web 上下文)
self.driver.delete_cookie("cookie_name")
删除所有 Cookie
删除当前页面可见的所有 cookie(仅限 Web 上下文)
- self.driver.delete_all_cookies()
- 32
- Frame
切换到 frame
将焦点更改为页面上的其他 frame(仅限 Web 上下文)
self.driver.switch_to.frame(3)
切换到父 frame
将焦点更改为父上下文 (仅限 Web 上下文)
- self.driver.switch_to.parent()
- 33
执行异步 JavaScript
将 JavaScript 片段注入页面以在当前选定框架的上下文中执行 (仅限 Web 上下文)
- self.driver.execute_async_script(document.title)
- 34
执行 JavaScript
将 JavaScript 片段注入页面以在当前选定框架的上下文中执行 (仅限 Web 上下文)
self.driver.execute_script(document.title)
来源: http://www.bubuko.com/infodetail-2519607.html