Appium 的 Python 客户端
项目描述
Appium Python 客户端
一个扩展库,用于将WebDriver 协议和 Appium 命令添加到 Selenium Python 语言绑定,以便与移动测试框架Appium 一起使用。
注意
从v1.0.0开始,仅支持 Python 3.7+。
从v2.0.0 开始,基本的 selenium 客户端版本是 v4。该版本仅适用于 W3C WebDriver 协议格式。如果您想使用旧协议(MJSONWP),请使用 v1 Appium Python 客户端。
从 v1 到 v2 的快速迁移指南
- 增强
- 将基本 Selenium Python 绑定版本更新为 v4
- 由于 Selenium v4 和Appium
forceMjsonwp
Python 客户端 v2 只需要 W3C WebDriver 协议,因此已删除
- 由于 Selenium v4 和Appium
- 方法
ActionHelpers#scroll
,ActionHelpers#drag_and_drop
,ActionHelpers#tap
,ActionHelpers#swipe
和ActionHelpers#flick
现在调用 W3C 动作作为它的后端- 请检查每个行为。他们的行为可能略有不同。
- 添加
strict_ssl
以放松 SSL 错误,例如自签名错误
- 将基本 Selenium Python 绑定版本更新为 v4
- 已弃用
MultiAction
并TouchAction
已弃用。请使用 W3C WebDriver 操作。- 例如
- appium/webdriver/extensions/action_helpers.py
- https://www.selenium.dev/documentation/support_packages/mouse_and_keyboard_actions_in_detail/
- https://www.youtube.com/watch?v=oAJ7jwMNFVU
- https://appiumpro.com/editions/30-ios-specific-touch-action-methods
- https://appiumpro.com/editions/29-automating-complex-gestures-with-the-w3c-actions-api
- 例如
launch_app
,close_app
并且reset
已被弃用。请阅读问题#15807了解更多详情
MultiAction/TouchAction 到 W3C 动作
在 UIA2 上,可以使用指针操作而不是 Selenium Python 客户touch
端中的默认指针操作来处理某些元素。mouse
例如,下面的动作生成器是用touch
指针动作替换默认的。
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
actions = ActionChains(driver)
# override as 'touch' pointer action
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(2)
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
actions.w3c_actions.pointer_action.release()
actions.perform()
获取 Appium Python 客户端
Appium Python 客户端有三种安装和使用方式。
-
从PyPi安装,作为 'Appium-Python-Client'。
pip install Appium-Python-Client
你可以从这里看到历史
-
通过PyPi从源代码安装。从'Appium-Python-Client'下载并解压缩源 tarball (Appium-Python-Client-XXtar.gz)。
tar -xvf Appium-Python-Client-X.X.tar.gz cd Appium-Python-Client-X.X python setup.py install
-
通过GitHub从源代码安装。
git clone git@github.com:appium/python-client.git cd python-client python setup.py install
用法
Appium Python 客户端完全符合 WebDriver 协议,包括几个帮助程序,使 Python 中的移动测试更容易。
现在要使用新功能,并使用函数的超集,而不是webdriver
在测试代码中包含 Selenium 模块,而是使用 Appium 中的模块。
from appium import webdriver
从那里开始,您的大部分测试代码将无需更改即可工作。
作为以下代码示例的基础,以下设置UnitTest 环境:
# Android environment
from appium import webdriver
# Options are only available since client version 2.3.0
# If you use an older client then switch to desired_capabilities
# instead: https://github.com/appium/python-client/pull/720
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
options = UiAutomator2Options()
options.platformVersion = '10'
options.udid = '123456789ABC'
options.app = PATH('../../../apps/test-app.apk')
# Appium1 points to http://127.0.0.1:4723/wd/hub by default
self.driver = webdriver.Remote('http://127.0.0.1:4723', options=options)
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='item')
el.click()
# iOS environment
from appium import webdriver
# Options are only available since client version 2.3.0
# If you use an older client then switch to desired_capabilities
# instead: https://github.com/appium/python-client/pull/720
from appium.options.ios import XCUITestOptions
from appium.webdriver.common.appiumby import AppiumBy
options = XCUITestOptions()
options.platformVersion = '13.4'
options.udid = '123456789ABC'
options.app = PATH('../../apps/UICatalog.app.zip')
# Appium1 points to http://127.0.0.1:4723/wd/hub by default
self.driver = webdriver.Remote('http://127.0.0.1:4723', options=options)
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='item')
el.click()
直接连接 URL
如果您的 Selenium/Appium 服务器使用以下键装饰新的会话功能响应:
directConnectProtocol
directConnectHost
directConnectPort
directConnectPath
然后 python 客户端将其端点切换到由这些键的值指定的端点。
from appium import webdriver
# Options are only available since client version 2.3.0
# If you use an older client then switch to desired_capabilities
# instead: https://github.com/appium/python-client/pull/720
from appium.options.ios import XCUITestOptions
# load_capabilities API could be used to
# load options mapping stored in a dictionary
options = XCUITestOptions().load_capabilities({
'platformVersion': '13.4',
'deviceName': 'iPhone Simulator',
'app': PATH('../../apps/UICatalog.app.zip'),
})
self.driver = webdriver.Remote(
# Appium1 points to http://127.0.0.1:4723/wd/hub by default
'http://127.0.0.1:4723',
options=options,
direct_connection=True
)
放松 SSL 验证
strict_ssl
选项允许您将命令发送到无效的证书主机,例如自签名主机。
from appium import webdriver
# Options are only available since client version 2.3.0
# If you use an older client then switch to desired_capabilities
# instead: https://github.com/appium/python-client/pull/720
from appium.options.common import AppiumOptions
options = AppiumOptions()
options.platform_name = 'mac'
options.automation_name = 'safari'
# set_capability API allows to provide any custom option
# calls to it could be chained
options.set_capability('browser_name', 'safari')
# Appium1 points to http://127.0.0.1:4723/wd/hub by default
self.driver = webdriver.Remote('http://127.0.0.1:4723', options=options, strict_ssl=False)
文档
- https://appium.github.io/python-client-sphinx/是详细文档
- 功能测试也可能有助于查看具体示例。
发展
- 代码风格:PEP-0008
- Apply
black
,isort
并mypy
作为 pre commit 钩子 - 运行
make
命令进行开发。有关详细信息,请参阅make help
输出
- Apply
- 文档字符串样式:Google 样式
gitchangelog
生成CHANGELOG.rst
设置
pip install --user pipenv
python -m pipenv lock --clear
- 如果遇到
Locking Failed! unknown locale: UTF-8
错误,请参考pypa/pipenv#187来解决它。
- 如果遇到
python -m pipenv install --dev --system
pre-commit install
运行测试
tox
您可以通过在本地运行在 CI 上运行的所有测试。
$ tox
您还可以运行如下特定测试。
单元
$ pytest test/unit
运行pytest-xdist
$ pytest -n 2 test/unit
功能性
$ pytest test/functional/ios/search_context/find_by_ios_class_chain_tests.py
iOS 并行
- 创建名为“iPhone X - 8100”和“iPhone X - 8101”的模拟器
- 通过 pip 安装测试库,
pip install pytest pytest-xdist
- 运行测试
$ pytest -n 2 test/functional/ios/search_context/find_by_ios_class_chain_tests.py
发布
请按照以下步骤操作。
$ pip install twine
$ pip install git+git://github.com/vaab/gitchangelog.git # Getting via GitHub repository is necessary for Python 3.7
# Type the new version number and 'yes' if you can publish it
# You can test the command with DRY_RUN
$ DRY_RUN=1 ./release.sh
$ ./release.sh # release
执照
阿帕奇许可证 v2