Skip to content

Commit 21bc74c

Browse files
committed
convert add_command to instance method
1 parent 6dd16a2 commit 21bc74c

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

py/selenium/webdriver/remote/remote_connection.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,13 @@ def __init__(
303303

304304
extra_commands = {}
305305

306-
@classmethod
307-
def add_command(cls, name, method, url):
306+
def add_command(self, name, method, url):
308307
"""Register a new command."""
309-
cls.extra_commands[name] = (method, url)
308+
self._commands[name] = (method, url)
309+
310+
def get_command(self, name: str):
311+
"""Retrieve a command if it exists."""
312+
return self._commands.get(name)
310313

311314
def execute(self, command, params):
312315
"""Send a command to the remote server.

py/test/unit/selenium/webdriver/remote/remote_connection_tests.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,27 @@
2727

2828
@pytest.fixture
2929
def remote_connection():
30+
"""Fixture to create a RemoteConnection instance."""
3031
return RemoteConnection("http://localhost:4444")
3132

3233

33-
def test_add_command():
34-
RemoteConnection.add_command("CUSTOM_COMMAND", "PUT", "/session/$sessionId/custom")
35-
assert RemoteConnection.extra_commands["CUSTOM_COMMAND"] == ("PUT", "/session/$sessionId/custom")
34+
def test_add_command(remote_connection):
35+
"""Test adding a custom command to the connection."""
36+
remote_connection.add_command("CUSTOM_COMMAND", "PUT", "/session/$sessionId/custom")
37+
assert remote_connection.get_command("CUSTOM_COMMAND") == ("PUT", "/session/$sessionId/custom")
3638

3739

3840
@patch("selenium.webdriver.remote.remote_connection.RemoteConnection._request")
3941
def test_execute_custom_command(mock_request, remote_connection):
40-
RemoteConnection.add_command("CUSTOM_COMMAND", "PUT", "/session/$sessionId/custom")
41-
mock_request.return_value = {"status": 0, "value": "OK"}
42+
"""Test executing a custom command through the connection."""
43+
remote_connection.add_command("CUSTOM_COMMAND", "PUT", "/session/$sessionId/custom")
44+
mock_request.return_value = {"status": 200, "value": "OK"}
4245

4346
params = {"sessionId": "12345"}
4447
response = remote_connection.execute("CUSTOM_COMMAND", params)
4548

4649
mock_request.assert_called_once_with("PUT", "http://localhost:4444/session/12345/custom", body="{}")
47-
assert response == {"status": 0, "value": "OK"}
50+
assert response == {"status": 200, "value": "OK"}
4851

4952

5053
def test_get_remote_connection_headers_defaults():
@@ -266,17 +269,17 @@ def mock_no_proxy_settings(monkeypatch):
266269

267270
@patch("selenium.webdriver.remote.remote_connection.RemoteConnection.get_remote_connection_headers")
268271
def test_override_user_agent_in_headers(mock_get_remote_connection_headers, remote_connection):
269-
RemoteConnection.user_agent = "rspec/1.0 (python 3.8)"
272+
RemoteConnection.user_agent = "custom-agent/1.0 (python 3.8)"
270273

271274
mock_get_remote_connection_headers.return_value = {
272275
"Accept": "application/json",
273276
"Content-Type": "application/json;charset=UTF-8",
274-
"User-Agent": "rspec/1.0 (python 3.8)",
277+
"User-Agent": "custom-agent/1.0 (python 3.8)",
275278
}
276279

277280
headers = RemoteConnection.get_remote_connection_headers(parse.urlparse("http://remote"))
278281

279-
assert headers.get("User-Agent") == "rspec/1.0 (python 3.8)"
282+
assert headers.get("User-Agent") == "custom-agent/1.0 (python 3.8)"
280283
assert headers.get("Accept") == "application/json"
281284
assert headers.get("Content-Type") == "application/json;charset=UTF-8"
282285

@@ -285,7 +288,7 @@ def test_override_user_agent_in_headers(mock_get_remote_connection_headers, remo
285288
def test_register_extra_headers(mock_request, remote_connection):
286289
RemoteConnection.extra_headers = {"Foo": "bar"}
287290

288-
mock_request.return_value = {"status": 0, "value": "OK"}
291+
mock_request.return_value = {"status": 200, "value": "OK"}
289292
remote_connection.execute("newSession", {})
290293

291294
mock_request.assert_called_once_with("POST", "http://localhost:4444/session", body="{}")

0 commit comments

Comments
 (0)