iOS自动化连接服务器时出现'to_capabilities'属性不存在的AttributeError
iOS Appium自动化测试:解决
AttributeError: 'NoneType' object has no attribute 'to_capabilities'错误 问题场景
在iOS自动化测试连接Appium服务器时触发错误:
AttributeError: 'NoneType' object has no attribute 'to_capabilities'
期望脚本成功运行,Appium服务器GUI显示执行日志。
原实现代码
@pytest.fixture def driver(request): print(os.getcwd()) global driver caps = {} caps = { 'automationName': 'XCUITest', 'platformName': 'iOS', 'deviceName': 'iPhone 6S', 'platformVersion': '16.6', 'udid': '******', 'autoAcceptAlerts': 'true', 'noReset': False, 'fullReset': False, 'app': os.getcwd() + '/app/apkname.ipa' } appium_server_url = "http://localhost:4723/wd/hub" driver = webdriver.Remote(appium_server_url, caps) yield driver
错误日志
tests/step_defs/framework/driver.py:45: in driver driver = webdriver.Remote(appium_server_url, caps) my_env/lib/python3.11/site-packages/appium/webdriver/webdriver.py:229: in __init__ super().__init__( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <[AttributeError("'WebDriver' object has no attribute 'session_id'") raised in repr()] WebDriver object at 0x10cc97710>, command_executor = <appium.webdriver.appium_connection.AppiumConnection object at 0x10cc97650>, keep_alive = True file_detector = None, options = None def __init__( self, command_executor="http://127.0.0.1:4444", keep_alive=True, file_detector=None, options: Union[BaseOptions, List[BaseOptions]] = None, ) -> None: """Create a new driver that will issue commands using the wire protocol. :Args: - command_executor - Either a string representing URL of the remote server or a custom remote_connection.RemoteConnection object. Defaults to 'http://127.0.0.1:4444/wd/hub'. - keep_alive - Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive. Defaults to True. - file_detector - Pass custom file detector object during instantiation. If None, then default LocalFileDetector() will be used. - options - instance of a driver options.Options class """ if isinstance(options, list): capabilities = create_matches(options) _ignore_local_proxy = False else: > capabilities = options.to_capabilities() E AttributeError: 'NoneType' object has no attribute 'to_capabilities' my_env/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py:185: AttributeError ================================================================================================================ warnings summary ================================================================================================================= my_env/lib/python3.11/site-packages/selenium/webdriver/remote/remote_connection.py:27
解决方案
错误原因
新版本Selenium(4.x+)和Appium Python Client的API发生变化,webdriver.Remote不再直接接受字典格式的capabilities作为第二个参数,而是要求通过options参数传入对应平台的Options实例。原代码直接传入字典,导致父类构造函数的options参数为None,触发options.to_capabilities()调用失败。
修改后的代码
import os import pytest from appium.webdriver.webdriver import WebDriver from appium.options.ios import XCUITestOptions @pytest.fixture def driver(request): print(os.getcwd()) # 初始化iOS测试配置项 options = XCUITestOptions() options.set_capability('automationName', 'XCUITest') options.set_capability('platformName', 'iOS') options.set_capability('deviceName', 'iPhone 6S') options.set_capability('platformVersion', '16.6') options.set_capability('udid', '******') options.set_capability('autoAcceptAlerts', True) # 使用布尔值替代字符串,避免类型问题 options.set_capability('noReset', False) options.set_capability('fullReset', False) # 用os.path.join安全拼接路径,兼容不同操作系统 options.set_capability('app', os.path.join(os.getcwd(), 'app', 'apkname.ipa')) appium_server_url = "http://localhost:4723/wd/hub" # 通过options参数传入配置 driver = WebDriver(appium_server_url, options=options) yield driver # 测试结束后关闭driver,释放资源 driver.quit()
关键修改点
- 导入
XCUITestOptions:Appium为iOS测试提供的专用配置类,替代原有的字典格式capabilities - 使用
set_capability方法逐一设置参数:符合新版本API要求 - 修正参数类型:
autoAcceptAlerts传入布尔值True而非字符串'true' - 安全路径拼接:用
os.path.join替代直接字符串拼接,提升跨平台兼容性 - 添加teardown操作:
driver.quit()确保测试会话正常关闭,避免资源泄漏
内容的提问来源于stack exchange,提问作者Pooja Naik
相关产品推荐
相关产品推荐

