Skip to content

Commit 502d4cc

Browse files
PureWeenrmarinho
authored andcommitted
Setup Android App With More Accurate settings (#20672)
* Setup Android App With More Accurate settings * - fix iOS reset * - fix windows * Update HelperExtensions.cs
1 parent 638f021 commit 502d4cc

File tree

7 files changed

+97
-7
lines changed

7 files changed

+97
-7
lines changed

src/Controls/samples/Controls.Sample.UITests/Platforms/Android/MainActivity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Android.App;
22
using Android.Content.PM;
3+
using Android.Runtime;
34
using Microsoft.Maui;
45

56
namespace Maui.Controls.Sample.Platform
@@ -11,6 +12,7 @@ namespace Maui.Controls.Sample.Platform
1112
[IntentFilter(
1213
new[] { Microsoft.Maui.ApplicationModel.Platform.Intent.ActionAppAction },
1314
Categories = new[] { Android.Content.Intent.CategoryDefault })]
15+
[Register("com.microsoft.maui.uitests.MainActivity")]
1416
public class MainActivity : MauiAppCompatActivity
1517
{
1618
}

src/Controls/tests/UITests/UITest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Reflection;
22
using NUnit.Framework;
3+
using UITest.Appium;
34
using UITest.Appium.NUnit;
45
using UITest.Core;
56
using VisualTestUtils;
@@ -85,6 +86,11 @@ public override IConfig GetTestConfig()
8586
return config;
8687
}
8788

89+
public override void Reset()
90+
{
91+
App.ResetApp();
92+
}
93+
8894
public void VerifyScreenshot(string? name = null)
8995
{
9096
string deviceName = GetTestConfig().GetProperty<string>("DeviceName") ?? string.Empty;

src/TestUtils/src/UITest.Appium/Actions/AppiumLifecycleActions.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using UITest.Core;
1+
using OpenQA.Selenium.Appium.Android;
2+
using UITest.Core;
23

34
namespace UITest.Appium
45
{
56
public class AppiumLifecycleActions : ICommandExecutionGroup
67
{
78
const string LaunchAppCommand = "launchApp";
89
const string BackgroundAppCommand = "backgroundApp";
10+
const string ForegroundAppCommand = "foregroundApp";
911
const string ResetAppCommand = "resetApp";
1012
const string CloseAppCommand = "closeApp";
1113
const string BackCommand = "back";
@@ -15,6 +17,7 @@ public class AppiumLifecycleActions : ICommandExecutionGroup
1517
readonly List<string> _commands = new()
1618
{
1719
LaunchAppCommand,
20+
ForegroundAppCommand,
1821
BackgroundAppCommand,
1922
ResetAppCommand,
2023
CloseAppCommand,
@@ -36,6 +39,7 @@ public CommandResponse Execute(string commandName, IDictionary<string, object> p
3639
return commandName switch
3740
{
3841
LaunchAppCommand => LaunchApp(parameters),
42+
ForegroundAppCommand => ForegroundApp(parameters),
3943
BackgroundAppCommand => BackgroundApp(parameters),
4044
ResetAppCommand => ResetApp(parameters),
4145
CloseAppCommand => CloseApp(parameters),
@@ -54,6 +58,16 @@ CommandResponse LaunchApp(IDictionary<string, object> parameters)
5458
return CommandResponse.SuccessEmptyResponse;
5559
}
5660

61+
CommandResponse ForegroundApp(IDictionary<string, object> parameters)
62+
{
63+
if (_app?.Driver is null)
64+
return CommandResponse.FailedEmptyResponse;
65+
66+
_app.Driver.ActivateApp(_app.GetAppId());
67+
68+
return CommandResponse.SuccessEmptyResponse;
69+
}
70+
5771
CommandResponse BackgroundApp(IDictionary<string, object> parameters)
5872
{
5973
if (_app?.Driver is null)
@@ -69,7 +83,25 @@ CommandResponse ResetApp(IDictionary<string, object> parameters)
6983
if (_app?.Driver is null)
7084
return CommandResponse.FailedEmptyResponse;
7185

72-
_app.Driver.ResetApp();
86+
// Terminate App not supported on Mac
87+
if (_app.GetTestDevice() == TestDevice.Mac)
88+
{
89+
_app.Driver.ResetApp();
90+
}
91+
else if (_app.GetTestDevice() == TestDevice.Windows)
92+
{
93+
CloseApp(parameters);
94+
_app.Driver.LaunchApp();
95+
}
96+
else
97+
{
98+
_app.Driver.TerminateApp(_app.GetAppId());
99+
100+
if (_app.GetTestDevice() == TestDevice.iOS)
101+
_app.Driver.ActivateApp(_app.GetAppId());
102+
else
103+
_app.Driver.LaunchApp();
104+
}
73105

74106
return CommandResponse.SuccessEmptyResponse;
75107
}

src/TestUtils/src/UITest.Appium/AppiumAndroidApp.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ private static AppiumOptions GetOptions(IConfig config)
7171
{
7272
config.SetProperty("PlatformName", "Android");
7373
config.SetProperty("AutomationName", "UIAutomator2");
74+
var appId = config.GetProperty<string>("AppId");
7475

7576
var options = new AppiumOptions();
77+
7678
SetGeneralAppiumOptions(config, options);
7779

78-
var appId = config.GetProperty<string>("AppId");
7980
if (!string.IsNullOrWhiteSpace(appId))
8081
{
81-
options.AddAdditionalAppiumOption(IOSMobileCapabilityType.BundleId, appId);
82+
options.AddAdditionalAppiumOption(MobileCapabilityType.NoReset, "true");
83+
options.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppPackage, appId);
84+
options.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppActivity, $"{appId}.MainActivity");
8285
}
8386

8487
return options;

src/TestUtils/src/UITest.Appium/AppiumApp.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ protected static void SetGeneralAppiumOptions(IConfig config, AppiumOptions appi
113113
if (config.GetProperty<bool>("FullReset"))
114114
appiumOptions.AddAdditionalAppiumOption(MobileCapabilityType.FullReset, "true");
115115

116+
if (config.GetProperty<bool>("NoReset"))
117+
appiumOptions.AddAdditionalAppiumOption(MobileCapabilityType.NoReset, "true");
118+
116119
var appPath = config.GetProperty<string>("AppPath");
117120
if (!string.IsNullOrEmpty(appPath))
118121
appiumOptions.App = appPath;

src/TestUtils/src/UITest.Appium/HelperExtensions.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static void SendKeys(this IApp app, int keyCode, int metastate = 0)
9595
ske.PressKeyCode(keyCode, metastate);
9696
return;
9797
}
98-
98+
9999
throw new InvalidOperationException($"SendKeys is not supported on {aaa.Driver}");
100100
}
101101

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

455+
/// <summary>
456+
/// If the application is already running then it will be brought to the foreground.
457+
/// </summary>
458+
/// <param name="app">Represents the main gateway to interact with an app.</param>
459+
public static void ForegroundApp(this IApp app)
460+
{
461+
app.CommandExecutor.Execute("foregroundApp", ImmutableDictionary<string, object>.Empty);
462+
}
463+
455464
/// <summary>
456465
/// Reset the currently running app for this session.
457466
/// </summary>
@@ -541,6 +550,41 @@ public static void Back(this IApp app)
541550
app.CommandExecutor.Execute("back", ImmutableDictionary<string, object>.Empty);
542551
}
543552

553+
/// <summary>
554+
/// Return the AppId of the running app. This is used inside any appium command that want the app id
555+
/// </summary>
556+
/// <param name="app">Represents the main gateway to interact with an app.</param>
557+
public static string GetAppId(this IApp app)
558+
{
559+
if (app is not AppiumApp aaa)
560+
{
561+
throw new InvalidOperationException($"GetAppId is only supported on AppiumApp");
562+
}
563+
564+
var appId = aaa.Config.GetProperty<string>("AppId");
565+
if (appId is not null)
566+
{
567+
return appId;
568+
}
569+
570+
throw new InvalidOperationException("AppId not found");
571+
}
572+
573+
/// <summary>
574+
/// Retrieve the target device this test is running against
575+
/// </summary>
576+
/// <param name="app">Represents the main gateway to interact with an app.</param>
577+
/// <returns></returns>
578+
/// <exception cref="InvalidOperationException"></exception>
579+
public static TestDevice GetTestDevice(this IApp app)
580+
{
581+
if (app is not AppiumApp aaa)
582+
{
583+
throw new InvalidOperationException($"GetTestDevice is only supported on AppiumApp");
584+
}
585+
586+
return aaa.Config.GetProperty<TestDevice>("TestDevice");
587+
}
544588

545589
/// <summary>
546590
/// Check if element has focused
@@ -609,4 +653,4 @@ static void WaitForNone(Func<IUIElement> query,
609653
Wait(query, i => i == null, timeoutMessage, timeout, retryFrequency);
610654
}
611655
}
612-
}
656+
}

src/TestUtils/src/UITest.NUnit/UITestContextBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void InitialSetup(IServerContext context)
4343
InitialSetup(context, false);
4444
}
4545

46-
public void Reset()
46+
public virtual void Reset()
4747
{
4848
if (_context == null)
4949
{

0 commit comments

Comments
 (0)