Skip to content

fix: decide direct connect from the command executor's client config - #12

Draft
MattL1989 wants to merge 1 commit into
masterfrom
fix/direct-connect-executor-config
Draft

MattL1989 wants to merge 1 commit into
masterfrom
fix/direct-connect-executor-config

Conversation

@MattL1989

Copy link
Copy Markdown
Owner

Problem

WebDriver.__init__ decides whether to use direct connect with if client_config and client_config.direct_connection:. That client_config is the one returned by _get_remote_connection_and_client_config, which causes two problems.

1. A custom connection's direct_connection is ignored. When a custom AppiumConnection is passed as command_executor, the helper returns client_config=None, because the connection already has its own config. Direct connect is then skipped, even though that config enables it:

config = AppiumClientConfig(remote_server_addr=URL, direct_connection=True)
driver = webdriver.Remote(AppiumConnection(client_config=config), options=options)
# the server returns directConnect* capabilities, but the driver keeps talking to URL

2. A Selenium ClientConfig raises AttributeError.

driver = webdriver.Remote(URL, options=options, client_config=ClientConfig(remote_server_addr=URL))
# AttributeError: 'ClientConfig' object has no attribute 'direct_connection'

The error is raised after the server has created the session, so that session is left running on the server.

Fix

  • Read the client config from the command executor (getattr(self.command_executor, 'client_config', None)). This is the config the driver actually uses, whether it came from client_config or from a custom connection.
  • Switch to the direct connect endpoint only when that config is an AppiumClientConfig with direct_connection enabled. Its keep_alive is used for the new connection, as before.

Behaviour change: a custom AppiumConnection whose AppiumClientConfig has direct_connection=True now switches to the direct connect endpoint when the server returns the directConnect* capabilities. Before, this setting was ignored. For the new endpoint, _update_command_executor creates a plain AppiumConnection with a copy of the config, as it already does today, so a subclass of AppiumConnection is not kept after the switch.

Tests

  • New test_create_session_register_uridirect_with_custom_connection: a custom AppiumConnection with direct_connection=True and keep_alive=False. The driver now talks to the direct connect endpoint and keeps keep_alive=False. Without the fix, the driver stays on http://127.0.0.1:4723.
  • New test_create_session_with_selenium_client_config: a Selenium ClientConfig creates the driver without error and stays on the original endpoint. Without the fix, it raises the AttributeError above.
  • The existing direct connect tests, which pass an AppiumClientConfig through client_config, pass unchanged.
  • ruff check, ruff format --check, mypy appium and the full unit test suite pass.

🤖 Generated with Claude Code

`WebDriver.__init__` checked `client_config.direct_connection` on the
`client_config` argument as returned by
`_get_remote_connection_and_client_config`. That caused two problems:

- When a custom `AppiumConnection` is passed as `command_executor`, the
  helper returns `client_config=None`, so `direct_connection=True` on the
  connection's own `AppiumClientConfig` was silently ignored.
- When a Selenium `ClientConfig` is passed as `client_config`, the check
  raised `AttributeError: 'ClientConfig' object has no attribute
  'direct_connection'`. This happened after the server had created the
  session, so the session was left running.

Read the client config from the command executor instead, which is the
config the driver actually uses, and switch to the direct connect
endpoint only when it is an `AppiumClientConfig` with
`direct_connection` enabled. Its `keep_alive` is still used for the new
connection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant