private static async Task ConfigureForAndroidTests(TestConfiguration config, int?androidSdk, string headBin, CancellationToken cancellationToken) { // Ensure WebDrivers are installed await SdkManager.InstallWebDriver(cancellationToken).ConfigureAwait(false); // Ensure latest CmdLine tools are installed await SdkManager.InstallLatestCommandLineTools(cancellationToken).ConfigureAwait(false); var sdkVersion = ApkHelper.GetAndroidSdkVersion(androidSdk, headBin); Logger.WriteLine($"Targeting Android Sdk: {sdkVersion}", LogLevel.Minimal); var appActivity = ApkHelper.GetAppActivity(headBin); if (!config.Capabilities.ContainsKey("appActivity")) { config.Capabilities.Add("appActivity", appActivity); } var emulatorName = $"{AvdManager.DefaultUITestEmulatorName}{sdkVersion}"; // Check for connected device if (await Adb.DeviceIsConnected(cancellationToken)) { var androidDevice = (await Adb.ListDevices(cancellationToken).ConfigureAwait(false)).First(); config.DeviceName = androidDevice.Name; config.UDID = androidDevice.Id; config.OSVersion = $"{androidDevice.SdkVersion}"; } else { // Ensure SDK Installed await SdkManager.EnsureSdkIsInstalled(sdkVersion, cancellationToken).ConfigureAwait(false); // Ensure Emulator Exists if (!(await Emulator.ListEmulators(cancellationToken)).Any(x => x == emulatorName)) { await AvdManager.InstallEmulator(emulatorName, sdkVersion, cancellationToken); } // Let Appium Start and control the Emulator config.DeviceName = emulatorName; config.OSVersion = $"{sdkVersion}"; if (!config.Capabilities.ContainsKey("avd")) { config.Capabilities.Add("avd", emulatorName); } } }
private async Task GenerateTestConfig(string headBin, string uiTestBin, string platform, CancellationToken cancellationToken) { var binDir = new DirectoryInfo(headBin); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true, AllowTrailingCommas = true, IgnoreNullValues = true, PropertyNameCaseInsensitive = true, ReadCommentHandling = JsonCommentHandling.Skip }; var appPath = platform switch { "Android" => binDir.GetFiles().First(x => x.Name.EndsWith("-Signed.apk")).FullName, "iOS" => binDir.GetDirectories().First(x => x.Name.EndsWith(".app")).FullName, null => throw new ArgumentNullException("No platform was specified"), _ => throw new PlatformNotSupportedException($"The {platform} is not supported") }; var config = new TestConfiguration(); var testConfig = Path.Combine(uiTestBin, ConfigFileName); if (!string.IsNullOrEmpty(ConfigurationPath)) { if (!File.Exists(ConfigurationPath)) { throw new FileNotFoundException($"Could not locate the specified uitest configuration at: '{ConfigurationPath}'"); } config = JsonSerializer.Deserialize <TestConfiguration>(File.ReadAllText(ConfigurationPath), options); } else if (File.Exists(testConfig)) { config = JsonSerializer.Deserialize <TestConfiguration>(File.ReadAllText(testConfig), options); } if (config.Capabilities is null) { config.Capabilities = new Dictionary <string, string>(); } if (config.Settings is null) { config.Settings = new Dictionary <string, string>(); } config.Platform = platform; config.AppPath = appPath; if (string.IsNullOrEmpty(config.ScreenshotsPath)) { config.ScreenshotsPath = Path.Combine(BaseWorkingDirectory, "screenshots"); } switch (platform) { case "Android": // Ensure WebDrivers are installed await SdkManager.InstallWebDriver(cancellationToken).ConfigureAwait(false); // Ensure latest CmdLine tools are installed await SdkManager.InstallLatestCommandLineTools(cancellationToken).ConfigureAwait(false); // Check for connected device if (!await Adb.DeviceIsConnected(cancellationToken)) { // Ensure SDK Installed await SdkManager.EnsureSdkIsInstalled(sdkVersion, cancellationToken).ConfigureAwait(false); // Ensure Emulator Exists if (!(await Emulator.ListEmulators(cancellationToken)).Any(x => x == AvdManager.DefaultUITestEmulatorName)) { await AvdManager.InstallEmulator(sdkVersion, cancellationToken); } // Start Emulator await Emulator.StartEmulator(AvdManager.DefaultUITestEmulatorName, cancellationToken).ConfigureAwait(false); } var emulator = (await Adb.ListDevices(cancellationToken).ConfigureAwait(false)).First(); config.DeviceName = emulator.Name; config.UDID = emulator.Id; config.OSVersion = $"{emulator.SdkVersion}"; break; case "iOS": AppleSimulator.ShutdownAllSimulators(); var device = AppleSimulator.GetSimulator(); if (device is null) { throw new NullReferenceException("Unable to locate the Device"); } config.DeviceName = device.Name; config.UDID = device.Udid; config.OSVersion = device.OSVersion; break; } var jsonOutput = JsonSerializer.Serialize(config, options); File.WriteAllText(testConfig, jsonOutput); if (DisplayGeneratedConfig) { Logger.WriteLine(jsonOutput, LogLevel.Normal); } }