Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: AndroidDriver to use modern mobile: commands for Lock, IsLocked and Unlock #874

Merged
merged 5 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add try/except and fallback to the legacy one like https://github.com/appium/python-client/blob/e4f06abf1f6bc302bde373071d1e3c007a67c03a/appium/webdriver/extensions/hw_actions.py#L40-L47 does. Then, maybe the change doesn't need to be a "breaking" one since the existing one will do the same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "breaking" is more for the unlock method that now requires additional parameters

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, the todo is correct. We haven't decided when we completely remove them, though. I'm good either, just I brought up the idea here

}

/// <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