Skip to content

fix: install extension commands on the driver instance - #11

Draft
MattL1989 wants to merge 1 commit into
masterfrom
fix/extensions-per-instance
Draft

MattL1989 wants to merge 1 commit into
masterfrom
fix/extensions-per-instance

Conversation

@MattL1989

Copy link
Copy Markdown
Owner

Problem

WebDriver.__init__ installs each extension method with setattr(WebDriver, ...). The method lives on the class and is bound to the most recently created driver:

a = webdriver.Remote(URL, options=options, extensions=[Custom])  # session A
b = webdriver.Remote(URL, options=options, extensions=[Custom])  # session B
c = webdriver.Remote(URL, options=options)                       # no extensions

a.custom_method()  # sends GET /session/B/... instead of /session/A/...
c.custom_method()  # exists, although c was created without extensions
a.delete_extensions()
b.custom_method()  # AttributeError: removed for every driver

An extension can also override a built-in method, such as get_status. Then every driver uses the override, and delete_extensions() deletes WebDriver.get_status itself. After that, driver.get_status() raises AttributeError on every driver until the process restarts.

Fix

  • Set each extension method on the driver instance (setattr(self, ...)), not on the class.
  • delete_extensions() removes only the attributes this driver set (those in self.__dict__), so methods defined on the class stay untouched.
  • Updated the ExtensionBase docstring comment, which said the commands "remain in the module".

Behaviour change: a driver created without an extension no longer gets that extension's methods from another driver. Code that relied on this has to pass extensions= to each driver that needs them. An extension can also no longer replace a driver property, such as contexts or orientation. Creating the driver then raises AttributeError, because properties can't be overridden on an instance.

Tests

  • New test_add_command_per_driver: two drivers with the same extension each call their own session. A third driver without extensions doesn't have the method. After delete_extensions() on the first driver, the second one still works. Without the fix, driver_a.test_command() returns 'session-b'.
  • New test_delete_extensions_keeps_overridden_method: an extension overrides get_status on one driver only. After delete_extensions(), both drivers use the built-in get_status again. Without the fix, the other driver gets 'custom status' instead of the server status.
  • The existing extension tests pass unchanged. Their delete_extensions() calls still remove the method from that driver.
  • ruff check, ruff format --check, mypy appium and the full unit test suite pass.

🤖 Generated with Claude Code

`WebDriver.__init__` installed each extension method with
`setattr(WebDriver, ...)`, so the method lived on the class and was bound
to the extension instance of the most recently created driver:

- with two drivers created with the same extension, calling the method on
  the first driver sent the request to the second driver's session;
- a driver created without extensions still had the method;
- `delete_extensions()` removed the method for every driver, and
  permanently removed any built-in method that an extension overrode
  (for example `get_status`).

Set the extension methods on the driver instance instead, and make
`delete_extensions()` remove only the attributes this driver set, so the
class methods stay untouched.

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