From c15e797349d54fded075c7c8ec607e887e341d07 Mon Sep 17 00:00:00 2001 From: Dor Blayzer <59066376+Dor-bl@users.noreply.github.com> Date: Wed, 4 Dec 2024 23:21:18 +0200 Subject: [PATCH] BREAKING CHANGE: `AndroidDriver` to use modern `mobile:` commands for Lock, IsLocked and Unlock (#874) * Update dependabot.yml (#142) try to fix duplicated PRs * Refactor AndroidDriver to use modern `mobile:` commands for Lock, Unlock and IsUnlocked * Revert "Update dependabot.yml (#142)" This reverts commit 6d867e41897fc52a9594e2c2d63869d2c77ef5d4. --- .../Android/AndroidCommandExecutionHelper.cs | 6 -- .../Appium/Android/AndroidDriver.cs | 64 ++++++++++++++++--- test/integration/Android/LockDeviceTest.cs | 2 +- 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/src/Appium.Net/Appium/Android/AndroidCommandExecutionHelper.cs b/src/Appium.Net/Appium/Android/AndroidCommandExecutionHelper.cs index 039cd17d..eb831c1a 100644 --- a/src/Appium.Net/Appium/Android/AndroidCommandExecutionHelper.cs +++ b/src/Appium.Net/Appium/Android/AndroidCommandExecutionHelper.cs @@ -170,12 +170,6 @@ public static float GetDisplayDensity(IExecuteMethod executeMethod) => Convert.T #endregion - public static bool IsLocked(IExecuteMethod executeMethod) => - (bool) executeMethod.Execute(AppiumDriverCommand.IsLocked).Value; - - public static void Unlock(IExecuteMethod executeMethod) => - executeMethod.Execute(AppiumDriverCommand.UnlockDevice); - public static Dictionary GetSettings(IExecuteMethod executeMethod) => (Dictionary) executeMethod.Execute(AppiumDriverCommand.GetSettings).Value; diff --git a/src/Appium.Net/Appium/Android/AndroidDriver.cs b/src/Appium.Net/Appium/Android/AndroidDriver.cs index 50737e99..2201c373 100644 --- a/src/Appium.Net/Appium/Android/AndroidDriver.cs +++ b/src/Appium.Net/Appium/Android/AndroidDriver.cs @@ -287,21 +287,67 @@ public IList GetPerformanceDataTypes() => #region Device Interactions - /** - * This method locks a device. - */ - public void Lock() => AppiumCommandExecutionHelper.Lock(this, 0); + /// + /// Locks the device. Optionally, unlocks it after a specified number of seconds. + /// + /// + /// The number of seconds after which the device will be automatically unlocked. + /// Set to 0 or leave it empty to require manual unlock. + /// + /// Thrown if the command execution fails. + public void Lock(int? seconds = null) + { + var parameters = new Dictionary(); + + if (seconds.HasValue && seconds.Value > 0) + { + parameters["seconds"] = seconds.Value; + } + + ExecuteScript("mobile: lock", parameters); + } /// /// Check if the device is locked /// /// true if device is locked, false otherwise - public bool IsLocked() => AndroidCommandExecutionHelper.IsLocked(this); + public bool IsLocked() => (bool)ExecuteScript("mobile: isLocked"); + + /// + /// Unlocks the device if it is locked. No operation if the device's screen is not locked. + /// + /// The unlock key. See the documentation on appium:unlockKey capability for more details. + /// The unlock type. See the documentation on appium:unlockType capability for more details. + /// Optional unlock strategy. See the documentation on appium:unlockStrategy capability for more details. + /// Optional unlock timeout in milliseconds. See the documentation on appium:unlockSuccessTimeout capability for more details. + /// Thrown when required arguments are null or empty. + /// Thrown if the command execution fails. + public void Unlock(string key, string type, string? strategy = null, int? timeoutMs = null) + { + if (string.IsNullOrWhiteSpace(key)) + throw new ArgumentException("Unlock key is required and cannot be null or whitespace.", nameof(key)); + + if (string.IsNullOrWhiteSpace(type)) + throw new ArgumentException("Unlock type is required and cannot be null or whitespace.", nameof(type)); + + var parameters = new Dictionary + { + { "key", key }, + { "type", type } + }; - /** - * This method unlocks a device. - */ - public void Unlock() => AndroidCommandExecutionHelper.Unlock(this); + if (strategy != null && !string.IsNullOrWhiteSpace(strategy)) + { + parameters["strategy"] = strategy; + } + + if (timeoutMs.HasValue) + { + parameters["timeoutMs"] = timeoutMs.Value; + } + + ExecuteScript("mobile: unlock", parameters); + } #endregion diff --git a/test/integration/Android/LockDeviceTest.cs b/test/integration/Android/LockDeviceTest.cs index babbebd0..83b042de 100644 --- a/test/integration/Android/LockDeviceTest.cs +++ b/test/integration/Android/LockDeviceTest.cs @@ -34,7 +34,7 @@ public void LockTest() { _driver.Lock(); Assert.That(_driver.IsLocked(), Is.EqualTo(true)); - _driver.Unlock(); + _driver.Unlock("12345","password"); Assert.That(_driver.IsLocked(), Is.EqualTo(false)); } }