Skip to content

Commit

Permalink
Setup Android App With More Accurate settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Feb 19, 2024
1 parent db94a9b commit b0cd756
Show file tree
Hide file tree
Showing 7 changed files with 59 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
3 changes: 2 additions & 1 deletion src/Controls/tests/UITests/Tests/CoreGalleryBasePageTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests
Expand All @@ -23,7 +24,7 @@ protected override void FixtureSetup()
TestContext.Error.WriteLine($">>>>> {DateTime.Now} The FixtureSetup threw an exception. Attempt {retries}/{SetupMaxRetries}.{Environment.NewLine}Exception details: {e}");
if (retries++ < SetupMaxRetries)
{
Reset();
App.ResetApp();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/tests/UITests/Tests/Issues/_IssuesUITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override void FixtureSetup()
TestContext.Error.WriteLine($">>>>> {DateTime.Now} The FixtureSetup threw an exception. Attempt {retries}/{SetupMaxRetries}.{Environment.NewLine}Exception details: {e}");
if (retries++ < SetupMaxRetries)
{
Reset();
App.ResetApp();
}
else
{
Expand Down
19 changes: 17 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,8 @@ CommandResponse ResetApp(IDictionary<string, object> parameters)
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;

_app.Driver.ResetApp();
_app.Driver.TerminateApp(_app.GetAppId());
_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
30 changes: 29 additions & 1 deletion 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,25 @@ 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>
/// Check if element has focused
Expand Down

0 comments on commit b0cd756

Please sign in to comment.