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] Implement themes change in Appium actions on Windows #25863

Merged
merged 3 commits into from
Nov 26, 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
@@ -1,6 +1,6 @@
namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.None, 2, "UI theme change during the runtime", PlatformAffected.Android | PlatformAffected.iOS)]
[Issue(IssueTracker.None, 2, "UI theme change during the runtime", PlatformAffected.Android | PlatformAffected.iOS | PlatformAffected.UWP)]
public partial class ThemeChange : ContentPage
{
public ThemeChange()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if ANDROID || IOS
#if ANDROID || IOS // Using AppThemeBinding and changing theme not working on Windows
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Diagnostics;
using UITest.Core;

namespace UITest.Appium
{
public class AppiumWindowsThemeChangeAction : ICommandExecutionGroup
{
const string SetLightTheme = "setLightTheme";
const string SetDarkTheme = "setDarkTheme";

readonly List<string> _commands = new()
{
SetLightTheme,
SetDarkTheme
};

public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
if (commandName == SetLightTheme)
{
ExecuteCommand($"start C://Windows/Resources/Themes/aero.theme");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the most interesting part.

return CommandResponse.SuccessEmptyResponse;
}
else if (commandName == SetDarkTheme)
{
ExecuteCommand($"start C://Windows/Resources/Themes/dark.theme");
return CommandResponse.SuccessEmptyResponse;
}

return CommandResponse.FailedEmptyResponse;
}

public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}

private static void ExecuteCommand(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();
Copy link
Contributor

@MartyIX MartyIX Nov 26, 2024

Choose a reason for hiding this comment

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

}

private static string GetShell()
{
return "cmd.exe";
}

private static string GetShellArgument(string shell, string command)
{
return $"/C {command}";
}
}
}
1 change: 1 addition & 0 deletions src/TestUtils/src/UITest.Appium/AppiumWindowsApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public AppiumWindowsApp(Uri remoteAddress, IConfig config)
: base(new WindowsDriver(remoteAddress, GetOptions(config)), config)
{
_commandExecutor.AddCommandGroup(new AppiumWindowsStepperActions(this));
_commandExecutor.AddCommandGroup(new AppiumWindowsThemeChangeAction());
}

public override ApplicationState AppState
Expand Down
4 changes: 2 additions & 2 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ public static TestDevice GetTestDevice(this IApp app)
/// <param name="app">Represents the main gateway to interact with an app.</param>
public static void SetLightTheme(this IApp app)
{
if (app is not AppiumAndroidApp && app is not AppiumIOSApp)
if (app is AppiumCatalystApp)
{
throw new InvalidOperationException($"SetLightTheme is not supported");
}
Expand All @@ -1465,7 +1465,7 @@ public static void SetLightTheme(this IApp app)
/// <param name="app">Represents the main gateway to interact with an app.</param>
public static void SetDarkTheme(this IApp app)
{
if (app is not AppiumAndroidApp && app is not AppiumIOSApp)
if (app is AppiumCatalystApp)
{
throw new InvalidOperationException($"SetDarkTheme is not supported");
}
Expand Down
Loading