示例#1
0
        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);
                }
            }
        }
        public override int Execute([NotNull] CommandContext context, [NotNull] AvdDeleteCommandSettings settings)
        {
            try
            {
                var avd = new AvdManager(settings?.Home);

                avd.Delete(settings.Name);
            }
            catch (SdkToolFailedExitException sdkEx)
            {
                Program.WriteException(sdkEx);
                return(1);
            }
            return(0);
        }
        public void InstallsEmulator()
        {
            var devices = Emulator.ListEmulators();

            if (devices.Any(x => x == AvdManager.DefaultUITestEmulatorName))
            {
                AvdManager.DeleteEmulator();
            }

            devices = Emulator.ListEmulators();

            Assert.DoesNotContain(AvdManager.DefaultUITestEmulatorName, devices);
            AvdManager.InstallEmulator();
            devices = Emulator.ListEmulators();

            Assert.Contains(AvdManager.DefaultUITestEmulatorName, devices);
        }
        public override int Execute([NotNull] CommandContext context, [NotNull] AvdTargetsCommandSettings settings)
        {
            try
            {
                var avd = new AvdManager(settings?.Home);

                var targets = avd.ListTargets();

                OutputHelper.Output(targets, settings?.Format,
                                    new[] { "Name", "Id", "Numeric Id", "API Level", "Type", "Revision" },
                                    i => new[] { i.Name, i.Id, i.NumericId?.ToString() ?? string.Empty, i.ApiLevel.ToString(), i.Type, i.Revision.ToString() });
            }
            catch (SdkToolFailedExitException sdkEx)
            {
                Program.WriteException(sdkEx);
                return(1);
            }
            return(0);
        }
示例#5
0
        public override int Execute([NotNull] CommandContext context, [NotNull] AvdListCommandSettings settings)
        {
            try
            {
                var avd = new AvdManager(settings?.Home);

                var avds = avd.ListAvds();

                OutputHelper.Output(avds, settings?.Format,
                                    new[] { "Name", "Target", "Device", "Based On", "Path" },
                                    i => new[] { i.Name, i.Target, i.Device, i.BasedOn, i.Path });
            }
            catch (SdkToolFailedExitException sdkEx)
            {
                Program.WriteException(sdkEx);
                return(1);
            }
            return(0);
        }
        public override int Execute([NotNull] CommandContext context, [NotNull] AvdDevicesCommandSettings settings)
        {
            try
            {
                var avd = new AvdManager(settings?.Home);

                var devices = avd.ListDevices();

                OutputHelper.Output(devices, settings?.Format,
                                    new[] { "Name", "Id", "NumericId", "Oem" },
                                    i => new[] { i.Name, i.Id, i.NumericId?.ToString() ?? string.Empty, i.Oem });
            }
            catch (SdkToolFailedExitException sdkEx)
            {
                Program.WriteException(sdkEx);
                return(1);
            }
            return(0);
        }
        public override int Execute([NotNull] CommandContext context, [NotNull] AvdCreateCommandSettings settings)
        {
            try
            {
                var avd = new AvdManager(settings?.Home);

                avd.Create(
                    settings.Name,
                    settings.SdkId,
                    settings.DeviceId,
                    settings.Path,
                    settings.Force,
                    settings.SdCardPath,
                    settings.SdCardSizeMb.HasValue ? (settings.SdCardSizeMb.ToString() + "MB") : (string)null);
            }
            catch (SdkToolFailedExitException sdkEx)
            {
                Program.WriteException(sdkEx);
                return(1);
            }

            return(0);
        }
示例#8
0
        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);
            }
        }