private async void RefreshDeviceButton_Click(object sender, EventArgs e) { // If the server isn't running and the server start fails, return. if (!_serverRunning && !TryStartServer()) { return; } try { // Get a list of all devices currently connected. var devices = await _client.GetDevicesAsync(); // Pause drawing to the ComboBox while we add items. DeviceComboBox.BeginUpdate(); DeviceComboBox.Items.Clear(); foreach (Device d in devices) { DeviceComboBox.Items.Add(d); } // Resume drawing. DeviceComboBox.EndUpdate(); } catch (AdbException ex) { MessageBox.Show(ex.Message, "ADB Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
static async Task RunTest(Dictionary <string, string> arguments) { processExitCancellationTokenSource = new CancellationTokenSource(); if (arguments.ContainsKey("settings") && File.Exists(arguments["settings"])) { settingsXml = File.ReadAllText(arguments["settings"]); } System.Net.IPEndPoint testAdapterEndpoint = null; if (arguments.ContainsKey("remoteIp")) { var val = arguments["remoteIp"]; if (val.Contains(":") && System.Net.IPAddress.TryParse(val.Split(':')[0], out System.Net.IPAddress ip) && int.TryParse(val.Split(':')[1], out int port)) { testAdapterEndpoint = new System.Net.IPEndPoint(ip, port); } else { System.Console.WriteLine("Invalid remote ip and/or port"); testRunCompleted.TrySetResult(1); return; } } if (testAdapterEndpoint != null) { await OnApplicationLaunched(testAdapterEndpoint); } else { // Launch Android android app client = new AdbClient(); var devices = await client.GetDevicesAsync(); if (!devices.Any()) { System.Console.WriteLine("No devices connected to ADB"); testRunCompleted.TrySetResult(1); return; } if (arguments.ContainsKey("deviceid")) { device = devices.Where(d => d.Serial == arguments["deviceid"]).FirstOrDefault(); if (device == null) { System.Console.WriteLine($"ERROR Device '{arguments["deviceid"]}' not found"); testRunCompleted.TrySetResult(1); return; } } if (device == null) { if (devices.Count() > 1) { System.Console.WriteLine($"ERROR. Multiple devices connected. Please specify -deviceId <deviceid>"); foreach (var d in devices) { System.Console.WriteLine($"\t{d.Serial}\t{d.Name}\t{d.Model}\t{d.Product}\t{d.State}"); } testRunCompleted.TrySetResult(1); return; } device = devices.First(); } if (arguments.ContainsKey("apkid")) { apk_id = arguments["apkid"]; } if (arguments.ContainsKey("activity")) { activityName = arguments["activity"]; } await ShutdownApp(); if (arguments.ContainsKey("apkpath")) { var path = arguments["apkpath"]; if (!File.Exists(path)) { System.Console.WriteLine("ERROR. APK Not Found: " + path); testRunCompleted.TrySetResult(1); } if (apk_id == null || activityName == null) { string id; string name; ApkHelper.GetAPKInfo(path, out id, out name); if (apk_id == null) { apk_id = id; } if (activityName == null) { activityName = name; } } System.Console.WriteLine("Installing app..."); await device.InstallApk(path); } // Get unlock state var state = await device.GetPowerState(); if (!state.IsDisplayOn) { await device.TurnOnDisplayAsync(); while (!state.IsDisplayOn) { await Task.Delay(500); state = await device.GetPowerState(); } } if (state.DisplayState == Device.DisplayState.OnAndLocked) { if (arguments.ContainsKey("pin")) { string pin = null; if (int.TryParse(arguments["pin"], out int numericPin)) //Ensures it's numeric { pin = arguments["pin"]; } System.Console.WriteLine("Unlocking phone..."); await device.UnlockAsync(pin); } else { System.Console.WriteLine("Device appears to be locked. Please unlock your device"); // System.Console.ReadKey(); } } if (File.Exists("RunLog.txt")) { File.Delete("RunLog.txt"); } if (arguments.ContainsKey("logFileName")) { outputFilename = arguments["logFileName"]; } else { string defaultFilename = device.Serial + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH_mm_ss"); if (apk_id != null) { defaultFilename += "_" + apk_id; } outputFilename = Path.Combine(System.Environment.CurrentDirectory, defaultFilename + ".trx"); } monitor = new LogCatMonitor(device.Serial); // Connecting to logcat... await client.SendDeviceCommandAsync("shell:logcat -b all -c", device.Serial); //Configure logcat bool result; if (device.ApiLevel > 22) { result = await monitor.OpenAsync($"{apk_id} -T 1"); } else { result = await monitor.OpenAsync($""); } if (result) { System.Console.WriteLine("Connected logcat to " + device.Serial); } else { System.Console.WriteLine("Failed to connect to LogCat"); return; } monitor.LogReceived += Monitor_LogReceived; await device.SetPortForward(38300, 38300); //Set up port forwarding for socket communication System.Console.WriteLine($"Launching app {apk_id}/{activityName} on device " + device.Serial + "..."); await device.LaunchApp(apk_id, activityName, new Dictionary <string, object>() { { "TestAdapterPort", 38300 } }); // Keep looking for process starting up bool launched = false; while (!launched) { await Task.Delay(1000); var pid = await device.GetProcessId(apk_id); if (appLaunchDetected) { break; } if (pid > 0) { launched = true; System.Console.WriteLine($"Test Host Launched. Process ID '{pid}'"); OnApplicationLaunched(); break; } else { //Keep retrying. Doesn't always launch the first time await device.LaunchApp(apk_id, activityName, new Dictionary <string, object>() { { "TestAdapterPort", 38300 } }); } } } }