Skip to content

Commit

Permalink
BREAKING CHANGE: AndroidDriver to use modern mobile: commands for…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
Dor-bl authored Dec 4, 2024
1 parent 66952a9 commit c15e797
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> GetSettings(IExecuteMethod executeMethod) =>
(Dictionary<string, object>) executeMethod.Execute(AppiumDriverCommand.GetSettings).Value;

Expand Down
64 changes: 55 additions & 9 deletions src/Appium.Net/Appium/Android/AndroidDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,67 @@ public IList<string> GetPerformanceDataTypes() =>

#region Device Interactions

/**
* This method locks a device.
*/
public void Lock() => AppiumCommandExecutionHelper.Lock(this, 0);
/// <summary>
/// Locks the device. Optionally, unlocks it after a specified number of seconds.
/// </summary>
/// <param name="seconds">
/// The number of seconds after which the device will be automatically unlocked.
/// Set to 0 or leave it empty to require manual unlock.
/// </param>
/// <exception cref="WebDriverException">Thrown if the command execution fails.</exception>
public void Lock(int? seconds = null)
{
var parameters = new Dictionary<string, object>();

if (seconds.HasValue && seconds.Value > 0)
{
parameters["seconds"] = seconds.Value;
}

ExecuteScript("mobile: lock", parameters);
}

/// <summary>
/// Check if the device is locked
/// </summary>
/// <returns>true if device is locked, false otherwise</returns>
public bool IsLocked() => AndroidCommandExecutionHelper.IsLocked(this);
public bool IsLocked() => (bool)ExecuteScript("mobile: isLocked");

/// <summary>
/// Unlocks the device if it is locked. No operation if the device's screen is not locked.
/// </summary>
/// <param name="key">The unlock key. See the documentation on appium:unlockKey capability for more details.</param>
/// <param name="type">The unlock type. See the documentation on appium:unlockType capability for more details.</param>
/// <param name="strategy">Optional unlock strategy. See the documentation on appium:unlockStrategy capability for more details.</param>
/// <param name="timeoutMs">Optional unlock timeout in milliseconds. See the documentation on appium:unlockSuccessTimeout capability for more details.</param>
/// <exception cref="ArgumentException">Thrown when required arguments are null or empty.</exception>
/// <exception cref="WebDriverException">Thrown if the command execution fails.</exception>
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<string, object>
{
{ "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

Expand Down
2 changes: 1 addition & 1 deletion test/integration/Android/LockDeviceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down

0 comments on commit c15e797

Please sign in to comment.