Skip to main content

Appium 的 Python 客户端

项目描述

Appium Python 客户端

PyPI 版本 下载

构建状态

代码风格:黑色

一个扩展库,用于将WebDriver 协议和 Appium 命令添加到 Selenium Python 语言绑定,以便与移动测试框架Appium 一起使用。

注意

v1.0.0开始,仅支持 Python 3.7+。

v2.0.0 开始,基本的 selenium 客户端版本是 v4。该版本仅适用于 W3C WebDriver 协议格式。如果您想使用旧协议(MJSONWP),请使用 v1 Appium Python 客户端。

从 v1 到 v2 的快速迁移指南

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 客户端有三种安装和使用方式。

  1. PyPi安装,作为 'Appium-Python-Client'

    pip install Appium-Python-Client
    

    你可以从这里看到历史

  2. 通过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
    
  3. 通过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)

文档

发展

  • 代码风格:PEP-0008
    • Apply blackisortmypy作为 pre commit 钩子
    • 运行make命令进行开发。有关详细信息,请参阅make help输出
  • 文档字符串样式: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 并行

  1. 创建名为“iPhone X - 8100”和“iPhone X - 8101”的模拟器
  2. 通过 pip 安装测试库,pip install pytest pytest-xdist
  3. 运行测试
$ 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

项目详情