Skip to content

Commit

Permalink
Tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Oct 10, 2022
1 parent 0f92f4a commit cc75311
Showing 1 changed file with 170 additions and 171 deletions.
341 changes: 170 additions & 171 deletions src/Interactive/AutomationExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,178 +14,177 @@ public class AutomationExtensions : IKernelExtension
{
public Task OnLoadAsync(Kernel kernel)
{
if (kernel is CSharpKernel csharpKernel)
{


var platformOption = new Option<Platform>(
new[] { "--platform" },
() => Platform.Maui,
"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.ZeroOrOne
};
var appOption = new Option<FileInfo>(
new[] { "--app" },
"The application file (.app, .apk, etc.) to test.")
{
Arity = ArgumentArity.ExactlyOne
};
var automationPlatformOption = new Option<Platform>(
new[] { "--automation-platform" },
() => Platform.Maui,
"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
};

platformOption.AddCompletions("Android", "iOS", "MacCatalsyt", "tvOS", "WinAppSDK");

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


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


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

testCommand.SetHandler<Platform, Platform, string, FileInfo, string, string>(
async (Platform platform, Platform automationPlatform, string appId, FileInfo 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.Start();

await csharpKernel.SetValueAsync(name, driver);
},
platformOption,
automationPlatformOption,
appIdOption,
appOption,
deviceOption,
nameOption);

csharpKernel.AddDirective(testCommand);

if (KernelInvocationContext.Current is { } context)
{
PocketView view = div(
code("Maui.UITesting"),
" is loaded. It adds commands for UI Testing/Automation.",
code("#!uitest -h")
);

context.Display(view);
}
}
if (kernel is CSharpKernel csharpKernel)
{
var platformOption = new Option<Platform>(
new[] { "--platform" },
() => Platform.Maui,
"The device platform to test on.")
{
Arity = ArgumentArity.ExactlyOne
};
platformOption.AddCompletions("Android", "iOS", "MacCatalsyt", "tvOS", "WinAppSDK");

var appIdOption = new Option<string>(
new[] { "--app-id" },
"The application id (bundle id, package id, etc) to test.")
{
Arity = ArgumentArity.ZeroOrOne
};

var appOption = new Option<FileInfo>(
new[] { "--app" },
"The application file (.app, .apk, etc.) to test.")
{
Arity = ArgumentArity.ExactlyOne
};
var automationPlatformOption = new Option<Platform>(
new[] { "--automation-platform" },
() => Platform.Maui,
"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
};

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


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


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

testCommand.SetHandler<Platform, Platform, string, FileInfo, string, string>(
async (Platform platform, Platform automationPlatform, string appId, FileInfo 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.Start();

await csharpKernel.SetValueAsync(name, driver);
},
platformOption,
automationPlatformOption,
appIdOption,
appOption,
deviceOption,
nameOption);

csharpKernel.AddDirective(testCommand);

if (KernelInvocationContext.Current is { } context)
{
PocketView view = div(
code("Maui.UITesting"),
" is loaded. It adds commands for UI Testing/Automation.",
code("#!uitest -h")
);

context.Display(view);
}
}

return Task.CompletedTask;
}
return Task.CompletedTask;
}

Task DisposeAppDriver(AppDriver driver)
{
try
{
driver.Dispose();
}
catch
{ }

return Task.CompletedTask;
}

void AddAndroidDevices(List<string> devices)
{
var adb = new Adb();
var adbDevices = adb.GetDevices();
devices.AddRange(adbDevices.Select(d => d.Serial));

var avd = new AvdManager();
var avds = avd.ListAvds();
devices.AddRange(avds.Select(a => a.Name));
}

void AddAppleDevices(string platform, List<string> devices)
{
var appleDevices = Xcode.GetDevices(platform).Select(d => d.Name);
if (appleDevices.Any())
devices.AddRange(appleDevices);
}
Task DisposeAppDriver(AppDriver driver)
{
try
{
driver.Dispose();
}
catch
{ }

return Task.CompletedTask;
}

void AddAndroidDevices(List<string> devices)
{
var adb = new Adb();
var adbDevices = adb.GetDevices();
devices.AddRange(adbDevices.Select(d => d.Serial));

var avd = new AvdManager();
var avds = avd.ListAvds();
devices.AddRange(avds.Select(a => a.Name));
}

void AddAppleDevices(string platform, List<string> devices)
{
var appleDevices = Xcode.GetDevices(platform).Select(d => d.Name);
if (appleDevices.Any())
devices.AddRange(appleDevices);
}
}

0 comments on commit cc75311

Please sign in to comment.