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

[Testing] Added ToggleSystemAnimations method to Appium helper methods #26096

Merged
merged 3 commits into from
Nov 28, 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 @@ -9,6 +9,7 @@ public class AppiumAndroidSpecificActions : ICommandExecutionGroup
const string ToggleWifiCommand = "toggleWifi";
const string ToggleDataCommand = "toggleData";
const string GetPerformanceDataCommand = "getPerformanceData";
const string ToggleSystemAnimationsCommand = "toggleSystemAnimations";
const string GetSystemBarsCommand = "getSystemBars";

readonly AppiumApp _appiumApp;
Expand All @@ -19,6 +20,7 @@ public class AppiumAndroidSpecificActions : ICommandExecutionGroup
ToggleWifiCommand,
ToggleDataCommand,
GetPerformanceDataCommand,
ToggleSystemAnimationsCommand,
GetSystemBarsCommand,
};

Expand All @@ -40,6 +42,7 @@ public CommandResponse Execute(string commandName, IDictionary<string, object> p
ToggleWifiCommand => ToggleWifi(parameters),
ToggleDataCommand => ToggleData(parameters),
GetPerformanceDataCommand => GetPerformanceData(parameters),
ToggleSystemAnimationsCommand => ToggleSystemAnimations(parameters),
GetSystemBarsCommand => GetSystemBars(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
Expand Down Expand Up @@ -103,6 +106,35 @@ CommandResponse GetPerformanceData(IDictionary<string, object> parameters)
return CommandResponse.FailedEmptyResponse;
}

CommandResponse ToggleSystemAnimations(IDictionary<string, object> parameters)
{
try
{
bool enableSystemAnimations = (bool)parameters["enableSystemAnimations"];

if (enableSystemAnimations)
{
ShellHelper.ExecuteAdbCommand($"adb shell settings put global window_animation_scale 0");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ShellHelper.ExecuteAdbCommand($"adb shell settings put global transition_animation_scale 0");
ShellHelper.ExecuteAdbCommand($"adb shell settings put global animator_duration_scale 0");

return CommandResponse.SuccessEmptyResponse;
}
else
{
ShellHelper.ExecuteAdbCommand($"adb shell settings put global window_animation_scale 1");
ShellHelper.ExecuteAdbCommand($"adb shell settings put global transition_animation_scale 1");
ShellHelper.ExecuteAdbCommand($"adb shell settings put global animator_duration_scale 1");

return CommandResponse.SuccessEmptyResponse;
}
}
catch
{
return CommandResponse.FailedEmptyResponse;
}
}

CommandResponse GetSystemBars(IDictionary<string, object> parameters)
{
if (_appiumApp.Driver is AndroidDriver androidDriver)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using UITest.Core;
using UITest.Core;

namespace UITest.Appium
{
Expand All @@ -18,12 +17,12 @@ public CommandResponse Execute(string commandName, IDictionary<string, object> p
{
if (commandName == SetLightTheme)
{
ExecuteAdbCommand($"adb shell cmd uimode night no");
ShellHelper.ExecuteAdbCommand($"adb shell cmd uimode night no");
return CommandResponse.SuccessEmptyResponse;
}
else if (commandName == SetDarkTheme)
{
ExecuteAdbCommand($"adb shell cmd uimode night yes");
ShellHelper.ExecuteAdbCommand($"adb shell cmd uimode night yes");
return CommandResponse.SuccessEmptyResponse;
}

Expand All @@ -34,40 +33,5 @@ public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}

private static void ExecuteAdbCommand(string command)
{
var shell = GetShell();
var shellArgument = GetShellArgument(shell, command);

var processInfo = new ProcessStartInfo(shell, shellArgument)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};

var process = new Process { StartInfo = processInfo };

process.Start();
process.WaitForExit();
}

private static string GetShell()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
return "cmd.exe";
else
return "/bin/bash";
}

private static string GetShellArgument(string shell, string command)
{
if (shell == "cmd.exe")
return $"/C {command}";
else
return $"-c \"{command}\"";
}
}
}
}
21 changes: 21 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,27 @@ public static void ToggleWifi(this IApp app)
}

/// <summary>
/// Switch the System animations state.
/// Optimize and accelerate tests, eliminating animations entirely when Appium is executing tests, as they serve no practical purpose in this context.
/// Functionality that's only available on Android.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="enableSystemAnimations">Enable/disable the system animations.</param>
/// <exception cref="InvalidOperationException">ToggleSystemAnimations is only supported on <see cref="AppiumAndroidApp"/>.</exception>
public static void ToggleSystemAnimations(this IApp app, bool enableSystemAnimations)
{
if (app is not AppiumAndroidApp)
{
throw new InvalidOperationException($"ToggleSystemAnimations is only supported on AppiumAndroidApp");
}

app.CommandExecutor.Execute("toggleSystemAnimations", new Dictionary<string, object>()
{
{ "enableSystemAnimations", enableSystemAnimations },
});
}

/// <summary>
/// Switch the state of data service.
/// Functionality that's only available on Android.
/// This API does not work for Android API level 21+ because it requires system or carrier privileged permission,
Expand Down
42 changes: 42 additions & 0 deletions src/TestUtils/src/UITest.Appium/ShellHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Diagnostics;

namespace UITest.Appium
{
public static class ShellHelper
{
public static void ExecuteAdbCommand(string command)
{
var shell = GetShell();
var shellArgument = GetShellArgument(shell, command);

var processInfo = new ProcessStartInfo(shell, shellArgument)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};

var process = new Process { StartInfo = processInfo };

process.Start();
process.WaitForExit();
}

internal static string GetShell()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
return "cmd.exe";
else
return "/bin/bash";
}

internal static string GetShellArgument(string shell, string command)
{
if (shell == "cmd.exe")
return $"/C {command}";
else
return $"-c \"{command}\"";
}
}
}
Loading