From b4a88281afced5cee44f22edebc8250823cf00d8 Mon Sep 17 00:00:00 2001 From: Dor-bl <59066376+Dor-bl@users.noreply.github.com> Date: Sun, 19 Jul 2026 21:44:56 +0000 Subject: [PATCH 1/3] refactor: remove legacy fallback for network commands Remove `try/except UnknownMethodException` fallback logic from `network_connection`, `set_network_connection`, `toggle_wifi`, and `set_network_speed` methods in the `Network` extension, relying solely on their corresponding `mobile:` scripts. Also removes the inheritance from `CanRememberExtensionPresence`, unneeded imports, and clears `_add_commands` as these endpoints are now completely deprecated. Updates unit tests to remove mocks for legacy endpoints. --- .../webdriver/extensions/android/network.py | 75 +++++-------------- test/unit/webdriver/network_test.py | 10 --- 2 files changed, 20 insertions(+), 65 deletions(-) diff --git a/appium/webdriver/extensions/android/network.py b/appium/webdriver/extensions/android/network.py index 6054e29d9..770e745a7 100644 --- a/appium/webdriver/extensions/android/network.py +++ b/appium/webdriver/extensions/android/network.py @@ -12,15 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from selenium.common.exceptions import UnknownMethodException from typing_extensions import Self from appium.common.helper import extract_const_attributes from appium.common.logger import logger from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands from appium.protocols.webdriver.can_execute_scripts import CanExecuteScripts -from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence -from appium.webdriver.mobilecommand import MobileCommand as Command class NetSpeed: @@ -41,7 +38,7 @@ class NetworkMask: AIRPLANE_MODE = 0b001 -class Network(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence): +class Network(CanExecuteCommands, CanExecuteScripts): @property def network_connection(self) -> int: """Returns an integer bitmask specifying the network connection type. @@ -53,16 +50,12 @@ def network_connection(self) -> int: since API level 31. """ ext_name = 'mobile: getConnectivity' - try: - result_map = self.assert_extension_exists(ext_name).execute_script(ext_name) - return ( - (NetworkMask.WIFI if result_map['wifi'] else 0) - | (NetworkMask.DATA if result_map['data'] else 0) - | (NetworkMask.AIRPLANE_MODE if result_map['airplaneMode'] else 0) - ) - except UnknownMethodException: - # TODO: Remove the fallback - return self.mark_extension_absence(ext_name).execute(Command.GET_NETWORK_CONNECTION, {})['value'] + result_map = self.execute_script(ext_name) + return ( + (NetworkMask.WIFI if result_map['wifi'] else 0) + | (NetworkMask.DATA if result_map['data'] else 0) + | (NetworkMask.AIRPLANE_MODE if result_map['airplaneMode'] else 0) + ) def set_network_connection(self, connection_type: int) -> int: """Sets the network connection type. Android only. @@ -95,20 +88,14 @@ def set_network_connection(self, connection_type: int) -> int: int: Set network connection type """ ext_name = 'mobile: setConnectivity' - try: - return self.assert_extension_exists(ext_name).execute_script( - ext_name, - { - 'wifi': bool(connection_type & NetworkMask.WIFI), - 'data': bool(connection_type & NetworkMask.DATA), - 'airplaneMode': bool(connection_type & NetworkMask.AIRPLANE_MODE), - }, - ) - except UnknownMethodException: - # TODO: Remove the fallback - return self.mark_extension_absence(ext_name).execute( - Command.SET_NETWORK_CONNECTION, {'parameters': {'type': connection_type}} - )['value'] + return self.execute_script( + ext_name, + { + 'wifi': bool(connection_type & NetworkMask.WIFI), + 'data': bool(connection_type & NetworkMask.DATA), + 'airplaneMode': bool(connection_type & NetworkMask.AIRPLANE_MODE), + }, + ) def toggle_wifi(self) -> Self: """Toggle the wifi on the device, Android only. @@ -119,12 +106,9 @@ def toggle_wifi(self) -> Self: Union['WebDriver', 'Network']: Self instance """ ext_name = 'mobile: setConnectivity' - try: - self.assert_extension_exists(ext_name).execute_script( - ext_name, {'wifi': not (self.network_connection & NetworkMask.WIFI)} - ) - except UnknownMethodException: - self.mark_extension_absence(ext_name).execute(Command.TOGGLE_WIFI, {}) + self.execute_script( + ext_name, {'wifi': not (self.network_connection & NetworkMask.WIFI)} + ) return self def set_network_speed(self, speed_type: str) -> Self: @@ -149,27 +133,8 @@ def set_network_speed(self, speed_type: str) -> Self: f'(e.g. {NetSpeed.__name__}.LTE)' ) ext_name = 'mobile: networkSpeed' - try: - self.assert_extension_exists(ext_name).execute_script(ext_name, {'speed': speed_type}) - except UnknownMethodException: - # TODO: Remove the fallback - self.mark_extension_absence(ext_name).execute(Command.SET_NETWORK_SPEED, {'netspeed': speed_type}) + self.execute_script(ext_name, {'speed': speed_type}) return self def _add_commands(self) -> None: - self.command_executor.add_command(Command.TOGGLE_WIFI, 'POST', '/session/$sessionId/appium/device/toggle_wifi') - self.command_executor.add_command( - Command.GET_NETWORK_CONNECTION, - 'GET', - '/session/$sessionId/network_connection', - ) - self.command_executor.add_command( - Command.SET_NETWORK_CONNECTION, - 'POST', - '/session/$sessionId/network_connection', - ) - self.command_executor.add_command( - Command.SET_NETWORK_SPEED, - 'POST', - '/session/$sessionId/appium/device/network_speed', - ) + pass diff --git a/test/unit/webdriver/network_test.py b/test/unit/webdriver/network_test.py index 0408d93ac..c544f5ddf 100644 --- a/test/unit/webdriver/network_test.py +++ b/test/unit/webdriver/network_test.py @@ -24,7 +24,6 @@ class TestWebDriverNetwork: @httpretty.activate def test_network_connection(self): driver = android_w3c_driver() - httpretty.register_uri(httpretty.GET, appium_command('/session/1234567890/network_connection'), body='{"value": 2}') httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/execute/sync'), @@ -35,7 +34,6 @@ def test_network_connection(self): @httpretty.activate def test_set_network_connection(self): driver = android_w3c_driver() - httpretty.register_uri(httpretty.POST, appium_command('/session/1234567890/network_connection'), body='{"value": ""}') httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/execute/sync'), @@ -49,10 +47,6 @@ def test_set_network_connection(self): @httpretty.activate def test_set_network_speed(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, - appium_command('/session/1234567890/appium/device/network_speed'), - ) httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/execute/sync'), @@ -65,10 +59,6 @@ def test_set_network_speed(self): @httpretty.activate def test_toggle_wifi(self): driver = android_w3c_driver() - httpretty.register_uri( - httpretty.POST, - appium_command('/session/1234567890/appium/device/toggle_wifi'), - ) httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/execute/sync'), From 09a0eab23ead4aae5f43037032590fd918d1f9ea Mon Sep 17 00:00:00 2001 From: Dor-bl <59066376+Dor-bl@users.noreply.github.com> Date: Sun, 19 Jul 2026 21:47:01 +0000 Subject: [PATCH 2/3] refactor: remove legacy fallback for network commands Remove `try/except UnknownMethodException` fallback logic from `network_connection`, `set_network_connection`, `toggle_wifi`, and `set_network_speed` methods in the `Network` extension, relying solely on their corresponding `mobile:` scripts. Also removes the inheritance from `CanRememberExtensionPresence`, unneeded imports, and clears `_add_commands` as these endpoints are now completely deprecated. Updates unit tests to remove mocks for legacy endpoints. Fixed ruff format issue. --- appium/webdriver/extensions/android/network.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/appium/webdriver/extensions/android/network.py b/appium/webdriver/extensions/android/network.py index 770e745a7..0f9d407fc 100644 --- a/appium/webdriver/extensions/android/network.py +++ b/appium/webdriver/extensions/android/network.py @@ -106,9 +106,7 @@ def toggle_wifi(self) -> Self: Union['WebDriver', 'Network']: Self instance """ ext_name = 'mobile: setConnectivity' - self.execute_script( - ext_name, {'wifi': not (self.network_connection & NetworkMask.WIFI)} - ) + self.execute_script(ext_name, {'wifi': not (self.network_connection & NetworkMask.WIFI)}) return self def set_network_speed(self, speed_type: str) -> Self: From 05c70d23cd0c927b85e793ef728f778c906111e1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 20 Jul 2026 21:04:58 +0000 Subject: [PATCH 3/3] fix: return int bitmask from set_network_connection execute_script's raw result doesn't match the method's documented int return type; return self.network_connection after setting instead. --- appium/webdriver/extensions/android/network.py | 3 ++- test/unit/webdriver/network_test.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/appium/webdriver/extensions/android/network.py b/appium/webdriver/extensions/android/network.py index 0f9d407fc..f3de82478 100644 --- a/appium/webdriver/extensions/android/network.py +++ b/appium/webdriver/extensions/android/network.py @@ -88,7 +88,7 @@ def set_network_connection(self, connection_type: int) -> int: int: Set network connection type """ ext_name = 'mobile: setConnectivity' - return self.execute_script( + self.execute_script( ext_name, { 'wifi': bool(connection_type & NetworkMask.WIFI), @@ -96,6 +96,7 @@ def set_network_connection(self, connection_type: int) -> int: 'airplaneMode': bool(connection_type & NetworkMask.AIRPLANE_MODE), }, ) + return self.network_connection def toggle_wifi(self) -> Self: """Toggle the wifi on the device, Android only. diff --git a/test/unit/webdriver/network_test.py b/test/unit/webdriver/network_test.py index c544f5ddf..cc1938dd3 100644 --- a/test/unit/webdriver/network_test.py +++ b/test/unit/webdriver/network_test.py @@ -39,9 +39,10 @@ def test_set_network_connection(self): appium_command('/session/1234567890/execute/sync'), body='{"value": {"wifi": true, "data": false, "airplaneMode": false}}', ) - driver.set_network_connection(2) + assert driver.set_network_connection(2) == 2 - d = get_httpretty_request_body(httpretty.last_request()) + d = get_httpretty_request_body(httpretty.latest_requests()[-4]) + assert d['script'] == 'mobile: setConnectivity' assert d['args'][0]['wifi'] is True @httpretty.activate