From c0c66c5a04f11af63e9950a84dbf5f26848c07d8 Mon Sep 17 00:00:00 2001 From: redth Date: Fri, 23 Sep 2022 13:32:08 -0400 Subject: [PATCH] Use ILogger to log more things! --- src/Core/Driver.cs | 10 +- src/Driver/AndroidDriver.cs | 150 ++++++++++++++++---------- src/Driver/AppDriver.cs | 17 +-- src/Driver/AppDriverBuilder.cs | 25 ++++- src/Driver/AutomationConfiguration.cs | 3 +- src/Driver/EmbeddedToolUtil.cs | 7 +- src/Driver/GrpcHost.cs | 29 ++++- src/Driver/MacDriver.cs | 42 ++------ src/Driver/ProcessRunner.cs | 17 +-- src/Driver/WindowsDriver.cs | 20 ++-- src/Driver/iOSDriver.cs | 34 +++--- src/Repl/Program.cs | 7 +- 12 files changed, 207 insertions(+), 154 deletions(-) diff --git a/src/Core/Driver.cs b/src/Core/Driver.cs index 5a5ebf4..effd232 100644 --- a/src/Core/Driver.cs +++ b/src/Core/Driver.cs @@ -1,12 +1,18 @@ -namespace Microsoft.Maui.Automation.Driver +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Microsoft.Maui.Automation.Driver { public abstract class Driver : IDriver { - public Driver(IAutomationConfiguration configuration) + public Driver(IAutomationConfiguration configuration, ILoggerFactory? loggerFactory = null) { Configuration = configuration; + LoggerFactory = loggerFactory ?? NullLoggerFactory.Instance; } + public readonly ILoggerFactory LoggerFactory; + public abstract string Name { get; } public IAutomationConfiguration Configuration { get; } diff --git a/src/Driver/AndroidDriver.cs b/src/Driver/AndroidDriver.cs index 7020290..592f62c 100644 --- a/src/Driver/AndroidDriver.cs +++ b/src/Driver/AndroidDriver.cs @@ -1,20 +1,21 @@ using AndroidSdk; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.Extensions.Logging; using Microsoft.Maui.Automation.Remote; -using System.Text.RegularExpressions; namespace Microsoft.Maui.Automation.Driver; public class AndroidDriver : Driver { - public AndroidDriver(IAutomationConfiguration configuration) : base(configuration) - { + public AndroidDriver(IAutomationConfiguration configuration, ILoggerFactory? loggerFactory = null) + : base(configuration, loggerFactory) + { + adbLogger = LoggerFactory.CreateLogger("adb"); + if (string.IsNullOrEmpty(configuration.AppId)) configuration.AppId = AppUtil.GetPackageId(configuration.AppFilename) ?? throw new Exception("AppId not found"); int port = 5000; - //var address = IPAddress.Any.ToString(); var adbDeviceSerial = configuration.Device; androidSdkManager = new AndroidSdkManager(); @@ -25,7 +26,9 @@ public AndroidDriver(IAutomationConfiguration configuration) : base(configuratio deviceInfo = new Lazy>(GetDeviceInfo); - var adbDevices = Adb.GetDevices(); + List adbDevices = new(); + + WrapAdbTool(() => adbDevices = Adb.GetDevices()); var foundDevice = false; // Use first if none were specified @@ -72,17 +75,20 @@ public AndroidDriver(IAutomationConfiguration configuration) : base(configuratio // If still not found, let's look for a non-running avd with this name if (!foundDevice) { - var avds = Emulator.ListAvds(); - foreach (var avd in avds) + WrapAdbTool(() => { - if (avd.Equals(adbDeviceSerial, StringComparison.OrdinalIgnoreCase)) + var avds = Emulator.ListAvds(); + foreach (var avd in avds) { - AdbDeviceName = avd; - emulatorProcess = Emulator.Start(avd); - emulatorProcess.WaitForBootComplete(TimeSpan.FromSeconds(120)); - adbDeviceSerial = emulatorProcess.Serial; + if (avd.Equals(adbDeviceSerial, StringComparison.OrdinalIgnoreCase)) + { + emulatorProcess = Emulator.Start(avd); + emulatorProcess.WaitForBootComplete(TimeSpan.FromSeconds(120)); + adbDeviceSerial = emulatorProcess.Serial; + break; + } } - } + }); } } @@ -94,11 +100,11 @@ public AndroidDriver(IAutomationConfiguration configuration) : base(configuratio Pm.AdbSerial = adbDeviceSerial; Name = $"Android ({AdbDeviceName})"; + + WrapAdbTool(() => + Adb.RunCommand("-s", $"\"{Device}\"", "reverse", $"tcp:{port}", $"tcp:{port}")); - var forwardResult = Adb.RunCommand("-s", $"\"{Device}\"", "reverse", $"tcp:{port}", $"tcp:{port}")?.GetAllOutput(); - System.Diagnostics.Debug.WriteLine(forwardResult); - - grpc = new GrpcHost(configuration); + grpc = new GrpcHost(configuration, LoggerFactory); } readonly GrpcHost grpc; @@ -107,10 +113,11 @@ public AndroidDriver(IAutomationConfiguration configuration) : base(configuratio protected readonly AndroidSdk.PackageManager Pm; protected readonly AndroidSdk.AvdManager Avd; protected readonly AndroidSdk.Emulator Emulator; + protected readonly ILogger adbLogger; readonly AndroidSdk.AndroidSdkManager androidSdkManager; - readonly AndroidSdk.Emulator.AndroidEmulatorProcess? emulatorProcess; + AndroidSdk.Emulator.AndroidEmulatorProcess? emulatorProcess; protected readonly string Device; @@ -124,7 +131,7 @@ public AndroidDriver(IAutomationConfiguration configuration) : base(configuratio public override Task Back() { - Adb.Shell($"input keyevent 4", Device); + Shell($"input keyevent 4"); return Task.CompletedTask; } @@ -133,33 +140,28 @@ public override Task ClearAppState() if (!IsAppInstalled()) return Task.CompletedTask; - Adb.Shell($"pm clear {AppId}", Device); + Shell($"pm clear {AppId}"); return Task.CompletedTask; } public override Task InstallApp() { - try - { - Adb.Install(new System.IO.FileInfo(Configuration.AppFilename ?? throw new FileNotFoundException()), Device); - } - catch (SdkToolFailedExitException adbException) - { - throw adbException; - } + WrapAdbTool(() => + Adb.Install(new System.IO.FileInfo(Configuration.AppFilename ?? throw new FileNotFoundException()), Device)); return Task.CompletedTask; } public override Task RemoveApp() - { - Adb.Uninstall(AppId, false, Device); + { + WrapAdbTool(() => + Adb.Uninstall(AppId, false, Device)); return Task.CompletedTask; } public override Task GetDeviceInfo() { - var density = new RegexParser(Shell("wm density"), @":\s+(?[0-9]+)").GetGroup("density", 1); - var rxSize = new RegexParser(Shell("wm size"), @":\s+(?[0-9]+)x(?[0-9]+)"); + var density = new RegexParser(Shell("wm density")?.FirstOrDefault() ?? ": 1", @":\s+(?[0-9]+)").GetGroup("density", 1); + var rxSize = new RegexParser(Shell("wm size")?.FirstOrDefault() ?? ": 0x0", @":\s+(?[0-9]+)x(?[0-9]+)"); return Task.FromResult(new DeviceInfo( (ulong)rxSize.GetGroup("width", 1), @@ -170,7 +172,7 @@ public override Task GetDeviceInfo() public override async Task InputText(Element element, string text) { // Hide keyboard first - Adb.Shell($"input keyevent 111", Device); + Shell($"input keyevent 111"); // Tap the element to focus it await Tap(element); @@ -182,8 +184,8 @@ public override async Task InputText(Element element, string text) // If you do it all at once, sometimes it gets jumbled up foreach (var c in text) { - Adb.Shell($"input text {c}", Device); - Thread.Sleep(100); + Shell($"input text {c}"); + await Task.Delay(100); } } @@ -212,26 +214,26 @@ public override Task KeyPress(char keyCode) // adb shell input keyevent $code if (code <= 0) throw new ArgumentOutOfRangeException(nameof(keyCode), "Unknown KeyCode"); - - Adb.Shell($"input keyevent {code}", Device); + + Shell($"input keyevent {code}"); return Task.CompletedTask; } public override Task LaunchApp() { // First force stop existing - Adb.Shell($"am force-stop {AppId}", Device); + Shell($"am force-stop {AppId}"); // Launch app's activity - Adb.Shell($"monkey -p {AppId} -c android.intent.category.LAUNCHER 1", Device); - //Adb.Shell($"monkey --pct-syskeys 0 -p {appId} 1", Device); + Shell($"monkey -p {AppId} -c android.intent.category.LAUNCHER 1"); + //Shell($"monkey --pct-syskeys 0 -p {appId} 1"); return Task.CompletedTask; } public override Task LongPress(int x, int y) { // Use a swipe that doesn't move as long press - Adb.Shell($"input swipe {x} {y} {x} {y} 3000", Device); + Shell($"input swipe {x} {y} {x} {y} 3000"); return Task.CompletedTask; } @@ -240,7 +242,7 @@ public override Task LongPress(Element element) public override Task OpenUri(string uri) { - Adb.Shell($"am start -d {uri}", Device); + Shell($"am start -d {uri}"); return Task.CompletedTask; } @@ -248,31 +250,33 @@ public override Task StopApp() { // Force the app to stop // am force-stop $appId" - Adb.Shell($"am force-stop {AppId}", Device); + Shell($"am force-stop {AppId}"); return Task.CompletedTask; } public override Task PushFile(string localFile, string destinationDirectory) - { - Adb.Push(new System.IO.FileInfo(localFile), new System.IO.DirectoryInfo(destinationDirectory), Device); + { + WrapAdbTool(() => + Adb.Push(new System.IO.FileInfo(localFile), new System.IO.DirectoryInfo(destinationDirectory), Device)); return Task.CompletedTask; } public override Task PullFile(string remoteFile, string localDirectory) - { - Adb.Pull(new System.IO.FileInfo(remoteFile), new System.IO.DirectoryInfo(localDirectory), Device); + { + WrapAdbTool(() => + Adb.Pull(new System.IO.FileInfo(remoteFile), new System.IO.DirectoryInfo(localDirectory), Device)); return Task.CompletedTask; } public override Task Swipe((int x, int y) start, (int x, int y) end) { - Adb.Shell($"input swipe {start.x} {start.y} {end.x} {end.y} 2000", Device); + Shell($"input swipe {start.x} {start.y} {end.x} {end.y} 2000"); return Task.CompletedTask; } public override Task Tap(int x, int y) { - Adb.Shell($"input tap {x} {y}", Device); + Shell($"input tap {x} {y}"); return Task.CompletedTask; } @@ -281,7 +285,7 @@ public override Task Tap(Element element) bool IsAppInstalled() - => androidSdkManager.PackageManager.ListPackages() + => Pm.ListPackages() .Any(p => p.PackageName?.Equals(AppId, StringComparison.OrdinalIgnoreCase) ?? false); @@ -315,7 +319,7 @@ public override async void Dispose() return null; try { - return Adb.GetDeviceName(serial); + return WrapAdbTool(() => Adb.GetDeviceName(serial))?.FirstOrDefault(); } catch { @@ -323,6 +327,44 @@ public override async void Dispose() } } - string Shell(string command) - => string.Join(Environment.NewLine, Adb.Shell(command, Device)); + IEnumerable Shell(string command) + => WrapAdbTool(() => Adb.Shell(command, Device)); + + IEnumerable WrapAdbTool(Func cmd) + => WrapAdbTool(() => new string[] { cmd() }); + + IEnumerable WrapAdbTool(Action cmd) + => WrapAdbTool(() => { + cmd(); + return Array.Empty(); + }); + + IEnumerable WrapAdbTool(Func cmd) + => WrapAdbTool(() => + cmd().StandardOutput); + + IEnumerable WrapAdbTool(Func> cmd) + { + try + { + var lines = cmd(); + + foreach (var o in lines) + adbLogger.LogInformation(o); + + return lines; + } + catch (SdkToolFailedExitException toolException) + { + foreach (var o in toolException.StdOut) + adbLogger.LogInformation(o); + + adbLogger.LogError(toolException.Message); + + foreach (var o in toolException.StdErr) + adbLogger.LogError(o); + + throw toolException; + } + } } diff --git a/src/Driver/AppDriver.cs b/src/Driver/AppDriver.cs index 9024265..2ab55fe 100644 --- a/src/Driver/AppDriver.cs +++ b/src/Driver/AppDriver.cs @@ -1,19 +1,20 @@ -using System; +using Microsoft.Extensions.Logging; +using System; namespace Microsoft.Maui.Automation.Driver; public class AppDriver : Driver { - public AppDriver(IAutomationConfiguration configuration) - : base(configuration) + public AppDriver(IAutomationConfiguration configuration, ILoggerFactory? loggerProvider = null) + : base(configuration, loggerProvider) { Driver = configuration.DevicePlatform switch { - Platform.Android => new AndroidDriver(configuration), - Platform.Ios => new iOSDriver(configuration), - Platform.Tvos => new iOSDriver(configuration), - Platform.Maccatalyst => new MacDriver(configuration), - Platform.Winappsdk => new WindowsDriver(configuration), + Platform.Android => new AndroidDriver(configuration, loggerProvider), + Platform.Ios => new iOSDriver(configuration, loggerProvider), + Platform.Tvos => new iOSDriver(configuration, loggerProvider), + Platform.Maccatalyst => new MacDriver(configuration, loggerProvider), + Platform.Winappsdk => new WindowsDriver(configuration, loggerProvider), _ => throw new NotSupportedException($"Unsupported Device Platform: '{configuration.DevicePlatform}'") }; } diff --git a/src/Driver/AppDriverBuilder.cs b/src/Driver/AppDriverBuilder.cs index 7d799ca..7cd2bd2 100644 --- a/src/Driver/AppDriverBuilder.cs +++ b/src/Driver/AppDriverBuilder.cs @@ -1,4 +1,7 @@ -namespace Microsoft.Maui.Automation.Driver; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Maui.Automation.Driver; public class AppDriverBuilder { @@ -22,17 +25,22 @@ public static AppDriverBuilder WithConfig(string configFile) public readonly IAutomationConfiguration Configuration; + readonly IHostBuilder HostBuilder; + IHost? Host; + public AppDriverBuilder() { + HostBuilder = Extensions.Hosting.Host.CreateDefaultBuilder(); Configuration = new AutomationConfiguration(); } public AppDriverBuilder(IAutomationConfiguration configuration) { + HostBuilder = Extensions.Hosting.Host.CreateDefaultBuilder(); Configuration = configuration; } - public AppDriverBuilder Configure(Action configuration) + public AppDriverBuilder ConfigureDriver(Action configuration) { configuration(Configuration); return this; @@ -68,6 +76,17 @@ public AppDriverBuilder AutomationPlatform(Platform automationPlatform) return this; } + public AppDriverBuilder ConfigureLogging(Action configure) + { + HostBuilder.ConfigureLogging(configure); + return this; + } + public IDriver Build() - => new AppDriver(Configuration); + { + Host = HostBuilder.Build(); + + return new AppDriver(Configuration, + Host.Services.GetService(typeof(ILoggerFactory)) as ILoggerFactory); + } } diff --git a/src/Driver/AutomationConfiguration.cs b/src/Driver/AutomationConfiguration.cs index d48ff3a..79bfed8 100644 --- a/src/Driver/AutomationConfiguration.cs +++ b/src/Driver/AutomationConfiguration.cs @@ -1,5 +1,4 @@ -using System; -using System.Net; +using System.Net; using System.Text.Json; using System.Text.Json.Serialization; diff --git a/src/Driver/EmbeddedToolUtil.cs b/src/Driver/EmbeddedToolUtil.cs index e90b6e6..0f8f69d 100644 --- a/src/Driver/EmbeddedToolUtil.cs +++ b/src/Driver/EmbeddedToolUtil.cs @@ -1,13 +1,8 @@ -using System; -using System.IO; -using System.IO.Compression; +using System.IO.Compression; using System.Reflection; -using System.Text; using System.Text.RegularExpressions; using System.Xml.Linq; -using System.Xml.XPath; using Claunia.PropertyList; -using Newtonsoft.Json.Linq; using SharpCompress.Readers; namespace Microsoft.Maui.Automation.Driver diff --git a/src/Driver/GrpcHost.cs b/src/Driver/GrpcHost.cs index 2693eaa..6332357 100644 --- a/src/Driver/GrpcHost.cs +++ b/src/Driver/GrpcHost.cs @@ -4,14 +4,17 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Maui.Automation.Driver; namespace Microsoft.Maui.Automation.Remote; public class GrpcHost { - public GrpcHost(IAutomationConfiguration configuration) + public GrpcHost(IAutomationConfiguration configuration, ILoggerFactory? loggerFactory) { + LoggerFactory = loggerFactory ?? NullLoggerFactory.Instance; + var builder = CreateHostBuilder(this, configuration); host = builder.Build(); @@ -23,19 +26,20 @@ public GrpcHost(IAutomationConfiguration configuration) public readonly GrpcRemoteAppClient Client; + public readonly ILoggerFactory LoggerFactory; readonly IWebHost host; public readonly IServiceProvider Services; - public ILogger Logger => Services.GetRequiredService>(); - public static IWebHostBuilder CreateHostBuilder(GrpcHost grpcHost, IAutomationConfiguration configuration) { var builder = new WebHostBuilder() .ConfigureLogging(log => { + log.ClearProviders(); + if (configuration.Get(ConfigurationKeys.GrpcHostLoggingEnabled, false)) - log.AddConsole(); + log.AddProvider(new LoggerFactoryInstanceLoggerProvider(grpcHost.LoggerFactory)); }) .UseKestrel(kestrel => { @@ -67,3 +71,20 @@ public static IWebHostBuilder CreateHostBuilder(GrpcHost grpcHost, IAutomationCo public Task Stop() => host?.StopAsync() ?? Task.CompletedTask; } + +internal class LoggerFactoryInstanceLoggerProvider : ILoggerProvider +{ + public LoggerFactoryInstanceLoggerProvider(ILoggerFactory loggerFactory) + { + LoggerFactory = loggerFactory; + } + + public readonly ILoggerFactory LoggerFactory; + + public ILogger CreateLogger(string categoryName) + => LoggerFactory.CreateLogger(categoryName); + + public void Dispose() + { + } +} \ No newline at end of file diff --git a/src/Driver/MacDriver.cs b/src/Driver/MacDriver.cs index d9463a1..e4133ea 100644 --- a/src/Driver/MacDriver.cs +++ b/src/Driver/MacDriver.cs @@ -1,26 +1,14 @@ -using AndroidSdk; -using Grpc.Net.Client; -using Microsoft.Maui.Automation.Remote; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.NetworkInformation; -using System.Text; -using System.Threading.Tasks; -using Idb; -using Grpc.Core; -using System.ComponentModel; -using System.IO; +using Microsoft.Maui.Automation.Remote; using System.Diagnostics; -using System.Drawing; - +using Microsoft.Extensions.Logging; + namespace Microsoft.Maui.Automation.Driver; public class MacDriver : Driver { - public MacDriver(IAutomationConfiguration configuration) : base(configuration) + public MacDriver(IAutomationConfiguration configuration, ILoggerFactory? loggerProvider) + : base(configuration, loggerProvider) { var port = configuration.AppAgentPort; var address = configuration.AppAgentAddress; @@ -32,20 +20,10 @@ public MacDriver(IAutomationConfiguration configuration) : base(configuration) Configuration.AppId = AppUtil.GetBundleIdentifier(Configuration.AppFilename) ?? throw new Exception("AppId not found"); - //var channel = GrpcChannel.ForAddress($"http://{address}:10882"); - //idb = new Idb.CompanionService.CompanionServiceClient(channel); - - //var connectResponse = idb.connect(new Idb.ConnectRequest()); - //Udid = connectResponse.Companion.Udid; - - grpc = new GrpcHost(configuration); + grpc = new GrpcHost(configuration, LoggerFactory); } - //public readonly string Udid; - readonly GrpcHost grpc; - - //readonly CompanionService.CompanionServiceClient idb; public override string Name { get; } @@ -102,13 +80,6 @@ public override Task PullFile(string remoteFile, string localDirectory) return Task.CompletedTask; } - - //public override async Task InputText(Element element, string text) - //{ - // await Tap(element); - // await idb.hid().SendStream(text.AsHidEvents().ToArray()); - //} - public override Task InputText(Element element, string text) => grpc.Client.PerformAction(Configuration.AutomationPlatform, Actions.InputText, element.Id, text); @@ -145,7 +116,6 @@ public override Task PerformAction(string action, string el public override Task Backdoor(string fullyQualifiedTypeName, string staticMethodName, string[] args) => grpc.Client.Backdoor(Configuration.AutomationPlatform, fullyQualifiedTypeName, staticMethodName, args); - public override async void Dispose() { if (grpc is not null) diff --git a/src/Driver/ProcessRunner.cs b/src/Driver/ProcessRunner.cs index c843d2c..620c194 100644 --- a/src/Driver/ProcessRunner.cs +++ b/src/Driver/ProcessRunner.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Diagnostics; namespace Microsoft.Maui.Automation.Driver; @@ -8,13 +9,15 @@ internal class ProcessRunner readonly List standardOutput; readonly List standardError; readonly Process process; + readonly ILogger logger; - public ProcessRunner(string executable, params string[] args) - : this(executable, args, System.Threading.CancellationToken.None) + public ProcessRunner(ILogger logger, string executable, params string[] args) + : this(logger, executable, args, System.Threading.CancellationToken.None) { } - public ProcessRunner(string executable, string[] args, System.Threading.CancellationToken cancelToken, bool redirectStandardInput = false) + public ProcessRunner(ILogger logger, string executable, string[] args, System.Threading.CancellationToken cancelToken, bool redirectStandardInput = false) { + this.logger = logger; standardOutput = new List(); standardError = new List(); @@ -32,16 +35,18 @@ public ProcessRunner(string executable, string[] args, System.Threading.Cancella process.OutputDataReceived += (s, e) => { - if (e.Data != null) + if (!string.IsNullOrWhiteSpace(e.Data) && !e.Data.Equals("\0")) { + logger.LogInformation(e.Data); standardOutput.Add(e.Data); OutputLine?.Invoke(this, e.Data); } }; process.ErrorDataReceived += (s, e) => { - if (e.Data != null) + if (!string.IsNullOrWhiteSpace(e.Data) && !e.Data.Equals("\0")) { + logger.LogError(e.Data); standardError.Add(e.Data); OutputLine?.Invoke(this, e.Data); } diff --git a/src/Driver/WindowsDriver.cs b/src/Driver/WindowsDriver.cs index 6b349be..781e51d 100644 --- a/src/Driver/WindowsDriver.cs +++ b/src/Driver/WindowsDriver.cs @@ -1,4 +1,4 @@ -using Google.Protobuf.WellKnownTypes; +using Microsoft.Extensions.Logging; using Microsoft.Maui.Automation.Remote; using OpenQA.Selenium.Appium.Interactions; using OpenQA.Selenium.Appium.Windows; @@ -13,8 +13,11 @@ public static class ConfigurationKeys public const string WinAppDriverExePath = "windows-winappdriver-exe-path"; } - public WindowsDriver(IAutomationConfiguration configuration) : base(configuration) - { + public WindowsDriver(IAutomationConfiguration configuration, ILoggerFactory? loggerProvider) + : base(configuration, loggerProvider) + { + appDriverLogger = LoggerFactory.CreateLogger("winappdriver"); + var appFile = configuration.AppFilename; if (string.IsNullOrEmpty(configuration.AppId)) @@ -39,26 +42,21 @@ public WindowsDriver(IAutomationConfiguration configuration) : base(configuratio throw new FileNotFoundException("Unable to locate WinAppDriver.exe, please install from: https://github.com/Microsoft/WinAppDriver"); appDriverProcess = null; - appDriverProcess = new ProcessRunner(appDriverPath, Array.Empty(), CancellationToken.None, true); - // appDriverProcess.OutputLine += AppDriverProcess_OutputLine; + appDriverProcess = new ProcessRunner(appDriverLogger, appDriverPath, Array.Empty(), CancellationToken.None, true); var appCapabilities = new DesiredCapabilities(); appCapabilities.SetCapability("app", configuration.AppId); appCapabilities.SetCapability("platformName", "Windows"); appCapabilities.SetCapability("platformVersion", "1.0"); - grpc = new GrpcHost(configuration); + grpc = new GrpcHost(configuration, LoggerFactory); Session = new Lazy>(() => new WindowsDriver(new Uri("http://127.0.0.1:4723"), appCapabilities)); } - - private void AppDriverProcess_OutputLine(object? sender, string e) - { - Console.WriteLine(e); - } readonly ProcessRunner? appDriverProcess; + readonly ILogger appDriverLogger; readonly Lazy> Session; diff --git a/src/Driver/iOSDriver.cs b/src/Driver/iOSDriver.cs index 5d89478..ff383be 100644 --- a/src/Driver/iOSDriver.cs +++ b/src/Driver/iOSDriver.cs @@ -1,26 +1,18 @@ -using AndroidSdk; -using Grpc.Net.Client; +using Grpc.Net.Client; using Microsoft.Maui.Automation.Remote; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.NetworkInformation; -using System.Text; -using System.Threading.Tasks; using Idb; -using Grpc.Core; -using System.ComponentModel; -using System.IO; using System.Diagnostics; -using System.Drawing; - +using Microsoft.Extensions.Logging; + namespace Microsoft.Maui.Automation.Driver; public class iOSDriver : Driver { - public iOSDriver(IAutomationConfiguration configuration) : base(configuration) - { + public iOSDriver(IAutomationConfiguration configuration, ILoggerFactory? loggerProvider) + : base(configuration, loggerProvider) + { + idbLogger = LoggerFactory.CreateLogger("idb"); + var port = configuration.AppAgentPort; var address = configuration.AppAgentAddress; @@ -34,12 +26,10 @@ public iOSDriver(IAutomationConfiguration configuration) : base(configuration) idbCompanionPath = UnpackIdb(); - idbCompanionProcess = new ProcessRunner(idbCompanionPath, $"--boot {configuration.Device}"); + idbCompanionProcess = new ProcessRunner(idbLogger, idbCompanionPath, $"--boot {configuration.Device}"); var bootResult = idbCompanionProcess.WaitForExit(); - Console.WriteLine(bootResult.GetAllOutput()); - - idbCompanionProcess = new ProcessRunner(idbCompanionPath, $"--udid {configuration.Device}"); + idbCompanionProcess = new ProcessRunner(idbLogger, idbCompanionPath, $"--udid {configuration.Device}"); // Sleep until idb exited or started while (true) @@ -63,7 +53,7 @@ public iOSDriver(IAutomationConfiguration configuration) : base(configuration) Name = $"iOS ({configuration.Device} [{Udid}])"; - grpc = new GrpcHost(configuration); + grpc = new GrpcHost(configuration, LoggerFactory); } public readonly string Udid; @@ -72,6 +62,8 @@ public iOSDriver(IAutomationConfiguration configuration) : base(configuration) readonly GrpcHost grpc; readonly string idbCompanionPath; readonly ProcessRunner idbCompanionProcess; + + readonly ILogger idbLogger; public override string Name { get; } diff --git a/src/Repl/Program.cs b/src/Repl/Program.cs index 927f6ed..19174b9 100644 --- a/src/Repl/Program.cs +++ b/src/Repl/Program.cs @@ -16,7 +16,12 @@ var driver = new AppDriverBuilder() .AppId("D05ADD49-B96D-49E5-979C-FA3A3F42F8E0_yn9kjvr01ms9j!App") .DevicePlatform(Platform.Winappsdk) - .Configure(c => c.Set(ConfigurationKeys.GrpcHostLoggingEnabled, true)) + .ConfigureLogging(log => + { + log.ClearProviders(); + log.AddConsole(); + }) + .ConfigureDriver(c => c.Set(ConfigurationKeys.GrpcHostLoggingEnabled, true)) .Build(); Task? readTask = null;