Skip to content

Commit

Permalink
Setup Android App With More Accurate settings (#20672)
Browse files Browse the repository at this point in the history
* Setup Android App With More Accurate settings

* - fix iOS reset

* - fix windows

* Update HelperExtensions.cs
  • Loading branch information
PureWeen authored and rmarinho committed Feb 27, 2024
1 parent 638f021 commit 502d4cc
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Microsoft.Maui;

namespace Maui.Controls.Sample.Platform
Expand All @@ -11,6 +12,7 @@ namespace Maui.Controls.Sample.Platform
[IntentFilter(
new[] { Microsoft.Maui.ApplicationModel.Platform.Intent.ActionAppAction },
Categories = new[] { Android.Content.Intent.CategoryDefault })]
[Register("com.microsoft.maui.uitests.MainActivity")]
public class MainActivity : MauiAppCompatActivity
{
}
Expand Down
6 changes: 6 additions & 0 deletions src/Controls/tests/UITests/UITest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using NUnit.Framework;
using UITest.Appium;
using UITest.Appium.NUnit;
using UITest.Core;
using VisualTestUtils;
Expand Down Expand Up @@ -85,6 +86,11 @@ public override IConfig GetTestConfig()
return config;
}

public override void Reset()
{
App.ResetApp();
}

public void VerifyScreenshot(string? name = null)
{
string deviceName = GetTestConfig().GetProperty<string>("DeviceName") ?? string.Empty;
Expand Down
36 changes: 34 additions & 2 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumLifecycleActions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using UITest.Core;
using OpenQA.Selenium.Appium.Android;
using UITest.Core;

namespace UITest.Appium
{
public class AppiumLifecycleActions : ICommandExecutionGroup
{
const string LaunchAppCommand = "launchApp";
const string BackgroundAppCommand = "backgroundApp";
const string ForegroundAppCommand = "foregroundApp";
const string ResetAppCommand = "resetApp";
const string CloseAppCommand = "closeApp";
const string BackCommand = "back";
Expand All @@ -15,6 +17,7 @@ public class AppiumLifecycleActions : ICommandExecutionGroup
readonly List<string> _commands = new()
{
LaunchAppCommand,
ForegroundAppCommand,
BackgroundAppCommand,
ResetAppCommand,
CloseAppCommand,
Expand All @@ -36,6 +39,7 @@ public CommandResponse Execute(string commandName, IDictionary<string, object> p
return commandName switch
{
LaunchAppCommand => LaunchApp(parameters),
ForegroundAppCommand => ForegroundApp(parameters),
BackgroundAppCommand => BackgroundApp(parameters),
ResetAppCommand => ResetApp(parameters),
CloseAppCommand => CloseApp(parameters),
Expand All @@ -54,6 +58,16 @@ CommandResponse LaunchApp(IDictionary<string, object> parameters)
return CommandResponse.SuccessEmptyResponse;
}

CommandResponse ForegroundApp(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;

_app.Driver.ActivateApp(_app.GetAppId());

return CommandResponse.SuccessEmptyResponse;
}

CommandResponse BackgroundApp(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
Expand All @@ -69,7 +83,25 @@ CommandResponse ResetApp(IDictionary<string, object> parameters)
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;

_app.Driver.ResetApp();
// Terminate App not supported on Mac
if (_app.GetTestDevice() == TestDevice.Mac)
{
_app.Driver.ResetApp();
}
else if (_app.GetTestDevice() == TestDevice.Windows)
{
CloseApp(parameters);
_app.Driver.LaunchApp();
}
else
{
_app.Driver.TerminateApp(_app.GetAppId());

if (_app.GetTestDevice() == TestDevice.iOS)
_app.Driver.ActivateApp(_app.GetAppId());
else
_app.Driver.LaunchApp();
}

return CommandResponse.SuccessEmptyResponse;
}
Expand Down
7 changes: 5 additions & 2 deletions src/TestUtils/src/UITest.Appium/AppiumAndroidApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ private static AppiumOptions GetOptions(IConfig config)
{
config.SetProperty("PlatformName", "Android");
config.SetProperty("AutomationName", "UIAutomator2");
var appId = config.GetProperty<string>("AppId");

var options = new AppiumOptions();

SetGeneralAppiumOptions(config, options);

var appId = config.GetProperty<string>("AppId");
if (!string.IsNullOrWhiteSpace(appId))
{
options.AddAdditionalAppiumOption(IOSMobileCapabilityType.BundleId, appId);
options.AddAdditionalAppiumOption(MobileCapabilityType.NoReset, "true");
options.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppPackage, appId);
options.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppActivity, $"{appId}.MainActivity");
}

return options;
Expand Down
3 changes: 3 additions & 0 deletions src/TestUtils/src/UITest.Appium/AppiumApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ protected static void SetGeneralAppiumOptions(IConfig config, AppiumOptions appi
if (config.GetProperty<bool>("FullReset"))
appiumOptions.AddAdditionalAppiumOption(MobileCapabilityType.FullReset, "true");

if (config.GetProperty<bool>("NoReset"))
appiumOptions.AddAdditionalAppiumOption(MobileCapabilityType.NoReset, "true");

var appPath = config.GetProperty<string>("AppPath");
if (!string.IsNullOrEmpty(appPath))
appiumOptions.App = appPath;
Expand Down
48 changes: 46 additions & 2 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void SendKeys(this IApp app, int keyCode, int metastate = 0)
ske.PressKeyCode(keyCode, metastate);
return;
}

throw new InvalidOperationException($"SendKeys is not supported on {aaa.Driver}");
}

Expand Down Expand Up @@ -452,6 +452,15 @@ public static void BackgroundApp(this IApp app)
app.CommandExecutor.Execute("backgroundApp", ImmutableDictionary<string, object>.Empty);
}

/// <summary>
/// If the application is already running then it will be brought to the foreground.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
public static void ForegroundApp(this IApp app)
{
app.CommandExecutor.Execute("foregroundApp", ImmutableDictionary<string, object>.Empty);
}

/// <summary>
/// Reset the currently running app for this session.
/// </summary>
Expand Down Expand Up @@ -541,6 +550,41 @@ public static void Back(this IApp app)
app.CommandExecutor.Execute("back", ImmutableDictionary<string, object>.Empty);
}

/// <summary>
/// Return the AppId of the running app. This is used inside any appium command that want the app id
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
public static string GetAppId(this IApp app)
{
if (app is not AppiumApp aaa)
{
throw new InvalidOperationException($"GetAppId is only supported on AppiumApp");
}

var appId = aaa.Config.GetProperty<string>("AppId");
if (appId is not null)
{
return appId;
}

throw new InvalidOperationException("AppId not found");
}

/// <summary>
/// Retrieve the target device this test is running against
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static TestDevice GetTestDevice(this IApp app)
{
if (app is not AppiumApp aaa)
{
throw new InvalidOperationException($"GetTestDevice is only supported on AppiumApp");
}

return aaa.Config.GetProperty<TestDevice>("TestDevice");
}

/// <summary>
/// Check if element has focused
Expand Down Expand Up @@ -609,4 +653,4 @@ static void WaitForNone(Func<IUIElement> query,
Wait(query, i => i == null, timeoutMessage, timeout, retryFrequency);
}
}
}
}
2 changes: 1 addition & 1 deletion src/TestUtils/src/UITest.NUnit/UITestContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void InitialSetup(IServerContext context)
InitialSetup(context, false);
}

public void Reset()
public virtual void Reset()
{
if (_context == null)
{
Expand Down

0 comments on commit 502d4cc

Please sign in to comment.