Skip to content

Commit

Permalink
some omre things
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Aug 22, 2024
1 parent 7dd8960 commit f9b784f
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;

namespace DeviceTestingKitApp;

[Register("com.companyname.devicetestingkitapp.MainActivity")]
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
Expand Down
9 changes: 9 additions & 0 deletions sample/test/DeviceTestingKitApp.UITests.Android/_Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Runtime.CompilerServices;

namespace DeviceTestingKitApp.UITests;

public static partial class _Config
{
[ModuleInitializer]
public static void Run() => Current = "android";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

<Import Project="..\DeviceTestingKitApp.UITests\DeviceTestingKitApp.UITests.targets" />

<ItemGroup>
<ProjectReference Include="..\..\..\src\DeviceRunners.Appium\DeviceRunners.Appium.csproj" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions sample/test/DeviceTestingKitApp.UITests/AppiumServerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using DeviceRunners.Appium;

using OpenQA.Selenium.Appium.Enums;

using Xunit;
using Xunit.Abstractions;

namespace DeviceTestingKitApp.UITests;

public class AppiumServerTests : BaseUITests
{
public AppiumServerTests(UITestsFixture fixture, ITestOutputHelper output)
: base(fixture, output)
{
}

[Fact]
public void IsReady()
{
var id = Driver.SessionId;

Assert.NotNull(id);
Assert.NotEmpty(id.ToString());

Assert.Equal(AppState.RunningInForeground, Driver.GetAppState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\..\src\DeviceRunners.Appium\DeviceRunners.Appium.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)**\*.cs" Exclude="$(MSBuildThisFileDirectory)bin\**;$(MSBuildThisFileDirectory)obj\**" />
</ItemGroup>
Expand Down
11 changes: 6 additions & 5 deletions sample/test/DeviceTestingKitApp.UITests/UITestsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public class UITestsFixture : IDisposable
public UITestsFixture(IMessageSink diagnosticMessageSink)
{
AppiumTest = AppiumTestBuilder.Create()
.UseServiceAddress("127.0.0.1", 4723)
.AddLogger(new MessageSinkLogger(diagnosticMessageSink))
.AddWindowsApp("windows_msix", "com.companyname.devicetestingkitapp_9zz4h110yvjzm!App")
.Build();
.UseServiceAddress("127.0.0.1", 4723)
.AddLogger(new MessageSinkLogger(diagnosticMessageSink))
.AddWindowsApp("windows_msix", "com.companyname.devicetestingkitapp_9zz4h110yvjzm!App")
.AddAndroidApp("android", "com.companyname.devicetestingkitapp", ".MainActivity")
//.AddAndroidApp("android", "D:\\GitHub\\DeviceRunners\\sample\\src\\DeviceTestingKitApp\\bin\\Release\\net8.0-android\\com.companyname.devicetestingkitapp-Signed.apk")
.Build();
}

public void Dispose()
Expand All @@ -36,7 +38,6 @@ public void Log(string message) =>
}
}


[CollectionDefinition(CollectionName)]
public class UITestsCollection : ICollectionFixture<UITestsFixture>
{
Expand Down
10 changes: 5 additions & 5 deletions src/DeviceRunners.Appium/AppiumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AppiumTest : IDisposable

private readonly ConcurrentDictionary<string, AppiumTestApp> _apps = new();

bool disposed;
bool _disposed;

public AppiumTest(AppiumServiceManagerOptions serviceOptions, IReadOnlyDictionary<string, AppiumDriverManagerOptions> driverOptions, IAppiumDiagnosticLogger logger)
{
Expand All @@ -23,15 +23,15 @@ public AppiumServiceManager ServiceManager
{
get
{
ObjectDisposedException.ThrowIf(disposed, typeof(AppiumTest));
ObjectDisposedException.ThrowIf(_disposed, typeof(AppiumTest));

return _serviceManager;
}
}

public AppiumTestApp GetApp(string appKey, bool restartDriver = true)
{
ObjectDisposedException.ThrowIf(disposed, typeof(AppiumTest));
ObjectDisposedException.ThrowIf(_disposed, typeof(AppiumTest));

return _apps.AddOrUpdate(
appKey,
Expand All @@ -46,10 +46,10 @@ public AppiumTestApp GetApp(string appKey, bool restartDriver = true)

public void Dispose()
{
if (disposed)
if (_disposed)
return;

disposed = true;
_disposed = true;

foreach (var app in _apps.Values)
{
Expand Down
4 changes: 3 additions & 1 deletion src/DeviceRunners.Appium/AppiumTestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public class AppiumTestBuilder

public static AppiumTestBuilder Create() => new AppiumTestBuilder();

public AppiumTestBuilder UseServiceAddress(string hostAddress = AppiumServiceManagerOptions.DefaultHostAddress, int port = AppiumServiceManagerOptions.DefaultHostPort)
public AppiumTestBuilder UseServiceAddress(
string hostAddress = AppiumServiceManagerOptions.DefaultHostAddress,
int port = AppiumServiceManagerOptions.DefaultHostPort)
{
_serviceOptions.HostAddress = hostAddress;
_serviceOptions.HostPort = port;
Expand Down
22 changes: 22 additions & 0 deletions src/DeviceRunners.Appium/Extensions/AppiumTestBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;

namespace DeviceRunners.Appium;

public static class AppiumDriverExtensions
{
public static AppState GetAppState(this AppiumDriver driver)
{
var automationName = driver.GetAutomationName()?.ToLowerInvariant();

return automationName switch
{
"windows" => AppState.NotInstalled,
"uiautomator2" => driver.GetAppState(driver.Capabilities.GetCapability(AndroidMobileCapabilityType.AppPackage)?.ToString()),
_ => throw new ArgumentException($"Unknown automation name: '{automationName}'."),
};
}

public static string? GetAutomationName(this AppiumDriver driver) =>
driver.Capabilities.GetCapability("automationName")?.ToString();
}
6 changes: 5 additions & 1 deletion src/DeviceRunners.Appium/Managers/AppiumServiceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ private AppiumLocalService BuildAppiumServer()
{
_logger?.Log("Starting Appium server...");

var args = new OpenQA.Selenium.Appium.Service.Options.OptionCollector()
.AddArguments(new KeyValuePair<string, string>("--log-no-colors", null!));

var builder = new AppiumServiceBuilder()
.WithIPAddress(_options.HostAddress)
.UsingPort(_options.HostPort);
.UsingPort(_options.HostPort)
.WithArguments(args);

if (!string.IsNullOrEmpty(_options.LogFile))
builder.WithLogFile(new FileInfo(_options.LogFile));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;

namespace DeviceRunners.Appium;

public class AndroidAppiumTestAppBuilder : AppiumTestAppBuilder
{
public AndroidAppiumTestAppBuilder()
{
AppiumOptions.AutomationName = "UIAutomator2";
AppiumOptions.PlatformName = "Android";
}

public AndroidAppiumTestAppBuilder UseAppPackageFilePath(string filePath)
{
AppiumOptions.App = filePath;
return this;
}

public AndroidAppiumTestAppBuilder UseAppPackageName(string packageName, string activityName)
{
AppiumOptions.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppPackage, packageName);
AppiumOptions.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppActivity, activityName);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;

namespace DeviceRunners.Appium;

public static class AndroidAppiumTestBuilderExtensions
{
public static AppiumTestBuilder AddAndroidApp(this AppiumTestBuilder builder, string appKey, string app, string? activityName = null) =>
builder.AddAndroidApp(appKey, appBuilder =>
{
switch (Path.GetExtension(app)?.ToLowerInvariant())
{
case ".apk":
case ".apks":
appBuilder.UseAppPackageFilePath(app);
break;
case ".aab":
throw new ArgumentException(
"App packages with the .aab extension is not supported by Appium. Only .apk and .apks are supported.",
nameof(app));
default:
appBuilder.UseAppPackageName(app, activityName ?? ".MainActivity");
break;
}
});

public static AppiumTestBuilder AddAndroidApp(this AppiumTestBuilder builder, string appKey, Action<AndroidAppiumTestAppBuilder> appBuilderAction) =>
builder.AddApp<AndroidAppiumTestAppBuilder, DriverFactory>(appKey, appBuilderAction);

class DriverFactory : IAppiumDriverFactory
{
public AppiumDriver CreateDriver(AppiumDriverManagerOptions options, AppiumServiceManager appium) =>
new AndroidDriver(appium.Service.ServiceUrl, options.Options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenQA.Selenium.Appium;

namespace DeviceRunners.Appium;

public abstract class AppiumTestAppBuilder
{
public AppiumOptions AppiumOptions { get; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace DeviceRunners.Appium;

public static class AppiumTestBuilderExtensions
{
internal static AppiumTestBuilder AddApp<TAppiumTestAppBuilder, TAppiumDriverFactory>(this AppiumTestBuilder builder, string appKey, Action<TAppiumTestAppBuilder> appBuilderAction)
where TAppiumTestAppBuilder : AppiumTestAppBuilder, new()
where TAppiumDriverFactory : IAppiumDriverFactory, new()
{
var appBuilder = new TAppiumTestAppBuilder();

appBuilderAction?.Invoke(appBuilder);

return builder.AddApp(appKey, new AppiumDriverManagerOptions
{
Options = appBuilder.AppiumOptions,
DriverFactory = new TAppiumDriverFactory(),
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using OpenQA.Selenium.Appium;

namespace DeviceRunners.Appium;

public class WindowsAppiumTestAppBuilder : AppiumTestAppBuilder
{
public WindowsAppiumTestAppBuilder()
{
AppiumOptions.AutomationName = "windows";
AppiumOptions.PlatformName = "Windows";
AppiumOptions.DeviceName = "WindowsPC";
}

public WindowsAppiumTestAppBuilder UseApp(string executablePathOrAppId)
{
AppiumOptions.App = executablePathOrAppId;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ namespace DeviceRunners.Appium;
public static class WindowsAppiumTestBuilderExtensions
{
public static AppiumTestBuilder AddWindowsApp(this AppiumTestBuilder builder, string appKey, string app) =>
builder.AddApp(appKey, new AppiumDriverManagerOptions
{
Options = new AppiumOptions
{
AutomationName = "windows",
PlatformName = "Windows",
DeviceName = "WindowsPC",
App = app,
},
DriverFactory = new WindowsDriverFactory(),
});
builder.AddWindowsApp(appKey, appBuilder => appBuilder.UseApp(app));

class WindowsDriverFactory : IAppiumDriverFactory
public static AppiumTestBuilder AddWindowsApp(this AppiumTestBuilder builder, string appKey, Action<WindowsAppiumTestAppBuilder> appBuilderAction) =>
builder.AddApp<WindowsAppiumTestAppBuilder, DriverFactory>(appKey, appBuilderAction);

class DriverFactory : IAppiumDriverFactory
{
public AppiumDriver CreateDriver(AppiumDriverManagerOptions options, AppiumServiceManager appium) =>
new WindowsDriver(appium.Service.ServiceUrl, options.Options);
Expand Down

0 comments on commit f9b784f

Please sign in to comment.