Skip to content

fix: use the client config headers and user agent in AppiumConnection - #6

Draft
MattL1989 wants to merge 8 commits into
masterfrom
fix/appium-connection-client-config-headers
Draft

MattL1989 wants to merge 8 commits into
masterfrom
fix/appium-connection-client-config-headers

Conversation

@MattL1989

Copy link
Copy Markdown
Owner

Problem

extra_headers and user_agent set on an AppiumClientConfig (or Selenium ClientConfig) are never sent when the connection is an AppiumConnection, which is the default for webdriver.Remote:

cfg = AppiumClientConfig(URL, extra_headers={'X-Tenant': 'team-a'}, user_agent='my-agent/1.0')
driver = webdriver.Remote(URL, options=UiAutomator2Options(), client_config=cfg)
# every request: X-Tenant missing, User-Agent: appium/6.0.9 (selenium/4.51.0 ...)

A plain Selenium RemoteConnection with the same config sends both.

Why it happens. Since Selenium 4.51, the minimum version this client requires:

  • RemoteConnection keeps each connection's user_agent and extra_headers on the instance.
  • get_remote_connection_headers is bound with a _classorinstancemethod descriptor so that it reads those instance values.

AppiumConnection overrides that method as a plain @classmethod. As a result, its super() call always resolves the headers on the class and never sees the connection's own config.

Second effect of the same override. The X-Idempotency-Key for a new-session request was written into the class-level AppiumConnection.extra_headers dict, then removed again by the next request. That dict is shared by every connection and thread. When two sessions are created at the same time, they can share the same key or lose it. The key exists to let the server de-duplicate a repeated new-session request.

Fix

  • Bind AppiumConnection.get_remote_connection_headers the same way Selenium does: to the instance when called on one, otherwise to the class.
  • Add the idempotency key only to the headers returned for the new-session request, instead of storing it on the class. Any stale key is still removed from other requests, case-insensitively.
  • Calling AppiumConnection.get_remote_connection_headers(...) on the class keeps working. So do subclasses that override it as a classmethod and call super().

Behaviour change: a user_agent set in the client config now replaces the default appium/... user agent, as it does for Selenium's RemoteConnection. Without one, the Appium user agent is sent as before.

Tests

  • New tests:
    • a connection created with a client config sends that config's extra headers and user agent;
    • the default Appium user agent is kept when none is configured;
    • the idempotency key is generated per new-session request and is not stored on the class.
  • The new tests fail without the fix.
  • test_remove_headers_case_insensitive asserted that the class-level dict was cleaned up. It now checks that the returned headers contain no stale key. A tearDown resets the class attribute the tests modify.
  • Verified end to end with httpretty: X-Tenant and the custom User-Agent are now sent on the new-session request and on later commands.
  • ruff check, ruff format --check, mypy appium and the full unit test suite pass.

Generated by Claude Code

MattL1989 and others added 8 commits October 31, 2026 13:34
waitForQuiescence is a boolean XCUITest capability and the getter returns
bool, but the setter was annotated as accepting timedelta | float, copied
from the timeout options. Type checkers therefore accepted a timedelta,
which cannot be serialized when the session is created.

Annotate the setter as bool and drop the unused timedelta import.


Claude-Session: https://claude.ai/code/session_01K6zVKi1DtwmmBpy16MQyfY

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Automatically generated by python-semantic-release
The flutter_element_wait_timeout and flutter_server_launch_timeout getters
are annotated and documented to return a timedelta, and their setters accept
one, but the getters returned the raw capability value in milliseconds. So
setting timedelta(seconds=10) and reading it back gave 10000.

Convert the stored milliseconds to a timedelta, like the other timeout
options do.


Claude-Session: https://claude.ai/code/session_01K6zVKi1DtwmmBpy16MQyfY

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Automatically generated by python-semantic-release
Automatically generated by python-semantic-release
AppiumConnection.get_remote_connection_headers was a plain classmethod, so
the super() call always resolved Selenium's headers on the class and never
on the connection instance. Since Selenium 4.51 a connection keeps the
user_agent and extra_headers from its client config on the instance, so
AppiumClientConfig(extra_headers=..., user_agent=...) had no effect for
AppiumConnection, while RemoteConnection sent them.

Bind the method to the instance when called on one, like Selenium does.
The idempotency key is still added only to the headers returned for a new
session request, as in appium#1338, and the method no longer rewrites the
class-level extra_headers dict on other requests. Calling the method on
the class keeps working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MattL1989
MattL1989 force-pushed the fix/appium-connection-client-config-headers branch from c36258b to d051830 Compare October 31, 2026 14:49
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.

3 participants