Skip to content

Commit

Permalink
Merge pull request #16 from colombod/magic_command
Browse files Browse the repository at this point in the history
use magic command
  • Loading branch information
Redth authored Oct 7, 2022
2 parents b602c1d + 0f6a48e commit b4a83bc
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 75 deletions.
209 changes: 136 additions & 73 deletions src/Interactive/AutomationExtension.cs
Original file line number Diff line number Diff line change
@@ -1,87 +1,150 @@
using System.CommandLine;
using System.CommandLine.Binding;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.Maui.Automation.Driver;

namespace Microsoft.Maui.Automation.Interactive;

public class AutomationExtensions : IKernelExtension
{
Driver.AppDriver? driver;


public Task OnLoadAsync(Kernel kernel)
{
if (kernel is CSharpKernel csharpKernel)
{
var platformOption = new Option<Platform>(
new[] {"--platform"},
"The device platform to test on.")
{
Arity = ArgumentArity.ExactlyOne
};
var appIdOption = new Option<string>(
new[] {"--app-id"},
"The application id (bundle id, package id, etc) to test.")
{
Arity = ArgumentArity.ExactlyOne
};
var appOption = new Option<string>(
new[] {"--app"},
"The application file (.app, .apk, etc.) to test.")
{
Arity = ArgumentArity.ExactlyOne
};
var automationPlatformOption = new Option<Platform>(
new[] {"--automation-platform"},
"The automation platform to interact with, if different than the device platform (ie: Maui).")
{
Arity = ArgumentArity.ZeroOrOne
};
var deviceOption = new Option<string>(
new[] {"--device"},
getDefaultValue: () => string.Empty,
description:
"Device ID to test on (eg: ADB Serial for an android device, UDID of emulator for iOS/tvOS/ipadOS).")
{
Arity = ArgumentArity.ZeroOrOne
};

var platformOption = new Option<Platform>(
new[] { "--platform" },
"The device platform to test on.")
{
Arity = ArgumentArity.ExactlyOne
};
var appIdOption = new Option<string>(
new[] { "--app-id" },
"The application id (bundle id, package id, etc) to test.")
{
Arity = ArgumentArity.ExactlyOne
};
var appOption = new Option<string>(
new[] { "--app" },
"The application file (.app, .apk, etc.) to test.")
{
Arity = ArgumentArity.ExactlyOne
};
var automationPlatformOption = new Option<Platform>(
new[] { "--automation-platform" },
"The automation platform to interact with, if different than the device platform (ie: Maui).")
{
Arity = ArgumentArity.ZeroOrOne
};
var deviceOption = new Option<string>(
new[] { "--device" },
getDefaultValue: () => string.Empty,
description: "Device ID to test on (eg: ADB Serial for an android device, UDID of emulator for iOS/tvOS/ipadOS).")
{
Arity = ArgumentArity.ZeroOrOne
};


var testOption = new Command("#!uitest", "Starts a UI Testing session")
{
platformOption,
automationPlatformOption,
appIdOption,
appOption,
deviceOption
};

testOption.SetHandler(
async (Platform platform, Platform automationPlatform, string appId, string app, string device) =>
{

if (driver is not null)
{
await driver.StopApp();
await driver.ClearAppState();
driver.Dispose();
driver = null;
}

driver = new Driver.AppDriver(
new Driver.AutomationConfiguration(
appId, app, platform, device, automationPlatform));

await driver.InstallApp();

//KernelInvocationContext.Current.Display(SvgClock.DrawSvgClock(hour, minute, second));
},
platformOption,
automationPlatformOption,
appIdOption,
appOption,
deviceOption);

kernel.AddDirective(testOption);

return Task.CompletedTask;
}

deviceOption.AddCompletions((context) =>
{
var platform = context.ParseResult.GetValueForOption(automationPlatformOption);
var devices = new List<string>();
switch (platform)
{
case Platform.Maui:
break;
case Platform.Ios:
break;
case Platform.Maccatalyst:
break;
case Platform.Macos:
break;
case Platform.Tvos:
break;
case Platform.Android:
break;
case Platform.Winappsdk:
break;
default:
throw new ArgumentOutOfRangeException();
}
// get list of valid devices string and return it
return devices;
});


var nameOption = new Option<string>(
new[] { "--name" },
getDefaultValue: () => "device",
description:
"Name for the local variable.")
{
Arity = ArgumentArity.ZeroOrOne
};


var testOption = new Command("#!uitest", "Starts a UI Testing session")
{
platformOption,
automationPlatformOption,
appIdOption,
appOption,
deviceOption,
nameOption
};

testOption.SetHandler(
async (Platform platform, Platform automationPlatform, string appId, string app, string device, string name) =>
{
// if there is an already used dirver with such name dispose
if (csharpKernel.TryGetValue(name, out AppDriver oldDriver) && oldDriver is {})
{
await DisposeAppDriver(oldDriver);
}

var driver = new AppDriver(
new AutomationConfiguration(
appId, app, platform, device, automationPlatform));

csharpKernel.RegisterForDisposal(async() =>
{
await DisposeAppDriver(driver);
});

await driver.InstallApp();

await csharpKernel.SetValueAsync(name, driver);

//KernelInvocationContext.Current.Display(SvgClock.DrawSvgClock(hour, minute, second));
},
platformOption,
automationPlatformOption,
appIdOption,
appOption,
deviceOption,
nameOption);

csharpKernel.AddDirective(testOption);
}

return Task.CompletedTask;

async Task DisposeAppDriver(AppDriver driver)
{
try
{
await driver.StopApp();
await driver.ClearAppState();
driver.Dispose();
}
catch
{

}
}
}
}

4 changes: 2 additions & 2 deletions src/Interactive/Interactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<None Remove="Microsoft.DotNet.Interactive.CSharp" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.Interactive" Version="1.0.0-beta.22452.2" />
<PackageReference Include="Microsoft.DotNet.Interactive.CSharp" Version="1.0.0-beta.22452.2" />
<PackageReference Include="Microsoft.DotNet.Interactive" Version="1.0.0-beta.22504.6" />
<PackageReference Include="Microsoft.DotNet.Interactive.CSharp" Version="1.0.0-beta.22504.6" />
</ItemGroup>
<ItemGroup>
<None Include="$(OutputPath)/Microsoft.Maui.Automation.Interactive.dll" Pack="true" PackagePath="interactive-extensions/dotnet" />
Expand Down

0 comments on commit b4a83bc

Please sign in to comment.