Skip to content

Commit c15e797

Browse files
authored
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 6d867e4.
1 parent 66952a9 commit c15e797

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

src/Appium.Net/Appium/Android/AndroidCommandExecutionHelper.cs

-6
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,6 @@ public static float GetDisplayDensity(IExecuteMethod executeMethod) => Convert.T
170170

171171
#endregion
172172

173-
public static bool IsLocked(IExecuteMethod executeMethod) =>
174-
(bool) executeMethod.Execute(AppiumDriverCommand.IsLocked).Value;
175-
176-
public static void Unlock(IExecuteMethod executeMethod) =>
177-
executeMethod.Execute(AppiumDriverCommand.UnlockDevice);
178-
179173
public static Dictionary<string, object> GetSettings(IExecuteMethod executeMethod) =>
180174
(Dictionary<string, object>) executeMethod.Execute(AppiumDriverCommand.GetSettings).Value;
181175

src/Appium.Net/Appium/Android/AndroidDriver.cs

+55-9
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,67 @@ public IList<string> GetPerformanceDataTypes() =>
287287

288288
#region Device Interactions
289289

290-
/**
291-
* This method locks a device.
292-
*/
293-
public void Lock() => AppiumCommandExecutionHelper.Lock(this, 0);
290+
/// <summary>
291+
/// Locks the device. Optionally, unlocks it after a specified number of seconds.
292+
/// </summary>
293+
/// <param name="seconds">
294+
/// The number of seconds after which the device will be automatically unlocked.
295+
/// Set to 0 or leave it empty to require manual unlock.
296+
/// </param>
297+
/// <exception cref="WebDriverException">Thrown if the command execution fails.</exception>
298+
public void Lock(int? seconds = null)
299+
{
300+
var parameters = new Dictionary<string, object>();
301+
302+
if (seconds.HasValue && seconds.Value > 0)
303+
{
304+
parameters["seconds"] = seconds.Value;
305+
}
306+
307+
ExecuteScript("mobile: lock", parameters);
308+
}
294309

295310
/// <summary>
296311
/// Check if the device is locked
297312
/// </summary>
298313
/// <returns>true if device is locked, false otherwise</returns>
299-
public bool IsLocked() => AndroidCommandExecutionHelper.IsLocked(this);
314+
public bool IsLocked() => (bool)ExecuteScript("mobile: isLocked");
315+
316+
/// <summary>
317+
/// Unlocks the device if it is locked. No operation if the device's screen is not locked.
318+
/// </summary>
319+
/// <param name="key">The unlock key. See the documentation on appium:unlockKey capability for more details.</param>
320+
/// <param name="type">The unlock type. See the documentation on appium:unlockType capability for more details.</param>
321+
/// <param name="strategy">Optional unlock strategy. See the documentation on appium:unlockStrategy capability for more details.</param>
322+
/// <param name="timeoutMs">Optional unlock timeout in milliseconds. See the documentation on appium:unlockSuccessTimeout capability for more details.</param>
323+
/// <exception cref="ArgumentException">Thrown when required arguments are null or empty.</exception>
324+
/// <exception cref="WebDriverException">Thrown if the command execution fails.</exception>
325+
public void Unlock(string key, string type, string? strategy = null, int? timeoutMs = null)
326+
{
327+
if (string.IsNullOrWhiteSpace(key))
328+
throw new ArgumentException("Unlock key is required and cannot be null or whitespace.", nameof(key));
329+
330+
if (string.IsNullOrWhiteSpace(type))
331+
throw new ArgumentException("Unlock type is required and cannot be null or whitespace.", nameof(type));
332+
333+
var parameters = new Dictionary<string, object>
334+
{
335+
{ "key", key },
336+
{ "type", type }
337+
};
300338

301-
/**
302-
* This method unlocks a device.
303-
*/
304-
public void Unlock() => AndroidCommandExecutionHelper.Unlock(this);
339+
if (strategy != null && !string.IsNullOrWhiteSpace(strategy))
340+
{
341+
parameters["strategy"] = strategy;
342+
}
343+
344+
if (timeoutMs.HasValue)
345+
{
346+
parameters["timeoutMs"] = timeoutMs.Value;
347+
}
348+
349+
ExecuteScript("mobile: unlock", parameters);
350+
}
305351

306352
#endregion
307353

test/integration/Android/LockDeviceTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void LockTest()
3434
{
3535
_driver.Lock();
3636
Assert.That(_driver.IsLocked(), Is.EqualTo(true));
37-
_driver.Unlock();
37+
_driver.Unlock("12345","password");
3838
Assert.That(_driver.IsLocked(), Is.EqualTo(false));
3939
}
4040
}

0 commit comments

Comments
 (0)