public void AutoClickStart() { try { Application app = FlaUI.Core.Application.Attach(Process.GetProcessById(RemotePlayProcess.Id)); using (var automation = new UIA2Automation()) { while (true) { Window window = app.GetMainWindow(automation); Button button1 = window.FindFirstDescendant(cf => cf.ByText("Start"))?.AsButton(); if (button1 == null) { Thread.Sleep(1000); } else { button1?.Invoke(); break; } } } } catch (Exception ex) { Log.Logger.Error("Problem with auto-clicker: " + ex.Message); } }
static void AutomateConnectionCreation(object obj) { var dataConnectionDialog = (IVsDataConnectionDialog)obj; var application = Application.Attach(Process.GetCurrentProcess().Id); var automation = new UIA2Automation(); var conditionally = automation.ConditionFactory; var mainWindow = application.GetMainWindow(automation); Window dialog; do { Thread.Yield(); dialog = mainWindow.ModalWindows.FirstOrDefault(w => w.Name == dataConnectionDialog.Title); }while (dialog == null); dialog .FindFirstDescendant( conditionally.ByName("Database file name").And(conditionally.ByControlType(ControlType.Edit))) .AsTextBox() .Text = ":memory:"; dialog .FindFirstDescendant( conditionally.ByName(dataConnectionDialog.AcceptButtonText)) .AsButton() .Invoke(); }
public static ITextRange[] NativeArrayToManaged(UIA2Automation automation, UIA.Text.TextPatternRange[] nativeElements) { if (nativeElements == null) { return(new ITextRange[0]); } return(nativeElements.Select(r => (ITextRange)NativeToManaged(automation, r)).ToArray()); }
public static AutomationElement[] NativeArrayToManaged(UIA2Automation automation, UIA.AutomationElement[] nativeElements) { if (nativeElements == null) { return(new AutomationElement[0]); } return(nativeElements.Select(automation.WrapNativeElement).ToArray()); }
private void ProcessNumbers() { using (var automation = new UIA2Automation()) { var window = app.GetMainWindow(automation); var processButton = window.FindFirstDescendant(cf => cf.ByName("Обработать")).AsButton(); processButton.Invoke(); } }
public void Launch() { app = Application.Launch(executable); automation = new UIA2Automation(); mainWindow = app.GetMainWindow(automation, TimeSpan.FromSeconds(10)); Thread.Sleep(1000); var popup = mainWindow.FindFirstDescendant(cf => cf.ByName("UMD(TM) Stream Composer / Viewer"))?.AsWindow(); if (popup != null) { popup.FindFirstDescendant(cf => cf.ByClassName("Button"))?.AsButton().Click(); } }
private void InputNumbers() { using (var automation = new UIA2Automation()) { var window = app.GetMainWindow(automation); var submitButton = window.FindFirstDescendant(cf => cf.ByName("Ввод")).AsButton(); var textBox = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Edit)).AsTextBox(); foreach (int number in testData) { textBox.Text = number.ToString(); Thread.Sleep(50); submitButton.Invoke(); } } }
public static AutomationElement[] NativeArrayToManaged(UIA2Automation automation, UIA.AutomationElementCollection nativeElements) { if (nativeElements == null) { return(new AutomationElement[0]); } var retArray = new AutomationElement[nativeElements.Count]; for (var i = 0; i < nativeElements.Count; i++) { var nativeElement = nativeElements[i]; var automationElement = automation.WrapNativeElement(nativeElement); retArray[i] = automationElement; } return(retArray); }
public MainViewModel() { var auto2 = new UIA2Automation(); var auto3 = new UIA3Automation(); //var desk2 = auto2.GetDesktop(); var desk3 = auto3.GetDesktop(); //Console.WriteLine(desk2.Current.BoundingRectangle.ToString()); Console.WriteLine(desk3.Current.BoundingRectangle.ToString()); Elements = new ObservableCollection <ElementViewModel>(); var desktop = auto3.GetDesktop(); var treeWalker = new TreeWalker(auto3); Elements.Add(new ElementViewModel(desktop)); }
static void Main(string[] args) { #if true var notepad = Application.Launch("notepad.exe"); #else var notepadProcess = System.Diagnostics.Process.Start("notepad.exe"); var notepad = Application.Attach(notepadProcess.Id); #endif using (var automation = new UIA2Automation()) { var notepadMainWindow = notepad.GetMainWindow(automation); notepadMainWindow.SetForeground(); var menuBar = notepadMainWindow.FindFirstChild(ControlType.MenuBar); menuBar.DrawHighlight(); var menuBarItems = menuBar.FindAllChildren(ControlType.MenuItem); #if false var helpMenu = menuBarItems[4] as MenuItem; // SO WRONG!!! #else var helpMenu = menuBarItems[4].AsMenuItem(); #endif helpMenu.DrawHighlight(); helpMenu.Expand(); var expandedHelpMenu = FlaUIHelper.WaitForElement( #if false () => notepadMainWindow.FindFirstChild(ControlType.Menu) #else () => notepadMainWindow.FindFirstDescendant(ControlType.Menu) #endif ); expandedHelpMenu.DrawHighlight(); var helpMenuItems = expandedHelpMenu.FindAllChildren(ControlType.MenuItem); var aboutMenuItem = helpMenuItems[2].AsMenuItem(); aboutMenuItem.Invoke(); } notepad.Close(); Console.ReadKey(); }
private void CompareResults() { using (var automation = new UIA2Automation()) { var window = app.GetMainWindow(automation); var outputMessage = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Window)).AsWindow(); var okButton = outputMessage.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Button)).AsButton(); var outputText = outputMessage.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Text)).AsTextBox(); string result = outputText.Text; if (result != "В введённом массиве нет подходящих под условия числел") { result = result.Remove(0, "Количество подходящих под условия чисел в массиве: ".Length); Assert.AreEqual(testDataAnalysisResult, Int32.Parse(result)); } okButton.Invoke(); } }
public WeChatAssistant() { var wechatProcess = Process.GetProcessesByName("WeChat").FirstOrDefault(); if (wechatProcess == null) { throw new Exception("WeChat is not running."); } Application = Application.Attach(wechatProcess.Id); #if USING_UIA3 automation_ = new UIA3Automation(); #else automation_ = new UIA2Automation(); #endif MainWindow = Application.GetMainWindow(automation_, TimeSpan.FromSeconds(3)); if (MainWindow == null) { throw new Exception("Could not find WeChat main window."); } }
public static UIA2TextRange NativeToManaged(UIA2Automation automation, UIA.Text.TextPatternRange nativeElement) { return(nativeElement == null ? null : new UIA2TextRange(automation, nativeElement)); }
public static AutomationElement NativeToManaged(UIA2Automation automation, UIA.AutomationElement nativeElement) { return(automation.WrapNativeElement(nativeElement)); }
static void Run(string[] args) { var stopwatch = Stopwatch.StartNew(); var labels = new List <string>(); var mss = new List <long>(); if (args.Length != 1 && args.Length != 2) { return; } int processId; if (!int.TryParse(args[0], out processId)) { return; } labels.Add("Parse CLI"); mss.Add(stopwatch.ElapsedMilliseconds); bool highlight = args.Length == 2; var application = FlaUI.Core.Application.Attach(processId); labels.Add("Attach"); mss.Add(stopwatch.ElapsedMilliseconds); using (var automation = new UIA2Automation()) { labels.Add("Create UIA2Automation"); mss.Add(stopwatch.ElapsedMilliseconds); var mainWindow = application.GetMainWindow(automation); labels.Add("GetMainWindow"); mss.Add(stopwatch.ElapsedMilliseconds); var grid = mainWindow.FindFirstChild(ControlType.Table).AsGrid(); if (grid == null) { return; } labels.Add("Find DataGridView"); mss.Add(stopwatch.ElapsedMilliseconds); if (highlight) { grid.DrawHighlight(); labels.Add("Highlight DataGridView"); mss.Add(stopwatch.ElapsedMilliseconds); } // Don't work. //var selectedRow = grid.SelectedItem; // Still don't work. WinForms DataGridView supports SelectionItemPattern. //var selectedRow = grid.Patterns.SelectionItem.Pattern.SelectionContainer.Value; //if (selectedRow == null) // return; #if USE_TREE_WALKER var selectedRow = grid.FindFirstChild( element => { if (element.ControlType != ControlType.DataItem) { return(false); } else { return(element.Patterns.SelectionItem.Pattern.IsSelected); } }); if (selectedRow == null) { return; } labels.Add("Find selected row"); mss.Add(stopwatch.ElapsedMilliseconds); var rowContent = string.Join(" ", selectedRow.FindAllChildren(ControlType.Text) .Select(element => element.Name)); labels.Add("Get row content"); mss.Add(stopwatch.ElapsedMilliseconds); var performance = string.Join("\n", labels.Zip(mss, (label, t) => string.Format("{0}: {1}", label, t))); MessageBox.Show( rowContent + '\n' + performance, "Selected row:", MessageBoxButtons.OK, MessageBoxIcon.Information); #else #if true // Not very slow. var rows = grid.Rows; #else var rows = grid.FindAllChildren(ControlType.DataItem); #endif labels.Add("Get all rows"); mss.Add(stopwatch.ElapsedMilliseconds); labels.Add("Get all rows"); mss.Add(stopwatch.ElapsedMilliseconds); foreach (var row in rows) { labels.Add("Iterate row"); mss.Add(stopwatch.ElapsedMilliseconds); // OK. Now DataGridView supports this pattern. if (row.Patterns.SelectionItem.Pattern.IsSelected) { if (highlight) { // Doesn't support this pattern. //row.DrawHighlight(); labels.Add("Highlight row"); mss.Add(stopwatch.ElapsedMilliseconds); } var rowContent = string.Join(" ", row.FindAllChildren(ControlType.Text) .Select(element => element.Name)); labels.Add("Get row content"); mss.Add(stopwatch.ElapsedMilliseconds); var performance = string.Join("\n", labels.Zip(mss, (label, t) => string.Format("{0}: {1}", label, t))); MessageBox.Show( rowContent + '\n' + performance, "Selected row:", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } #endif } }
public static Window GetMainWindow(this Application app, UIA2Automation automation) { var window = automation.FromHandle(app.MainWindowHandle).AsWindow(); return(window); }
static void Run(string[] args) { var stopwatch = Stopwatch.StartNew(); var labels = new List <string>(); var mss = new List <long>(); if (args.Length != 3 && args.Length != 4) { return; } int processId; if (!int.TryParse(args[0], out processId)) { return; } int x; int y; if (!int.TryParse(args[1], out x) || !int.TryParse(args[2], out y)) { return; } labels.Add("Parse CLI"); mss.Add(stopwatch.ElapsedMilliseconds); bool highlight = args.Length == 4; Point point = new Point(x, y); var application = FlaUI.Core.Application.Attach(processId); labels.Add("Attach"); mss.Add(stopwatch.ElapsedMilliseconds); #if USE_UIA2 using (var automation = new UIA2Automation()) #else using (var automation = new UIA3Automation()) #endif { labels.Add("Create UIA2Automation"); mss.Add(stopwatch.ElapsedMilliseconds); var mainWindow = application.GetMainWindow(automation); labels.Add("GetMainWindow"); mss.Add(stopwatch.ElapsedMilliseconds); #if AS_GRID var grid = mainWindow.FindFirstChild(ControlType.Table).AsGrid(); if (grid == null) { return; } labels.Add("Find DataGridView"); mss.Add(stopwatch.ElapsedMilliseconds); if (highlight) { grid.DrawHighlight(); labels.Add("Highlight DataGridView"); mss.Add(stopwatch.ElapsedMilliseconds); } // WinForms DataGridView doesn't support this pattern. var selectedRow = grid.SelectedItem; if (selectedRow == null) { return; } if (highlight) { selectedRow.DrawHighlight(); labels.Add("Highlight row"); mss.Add(stopwatch.ElapsedMilliseconds); } var rowContent = string.Join("\n", selectedRow.FindAllChildren(ControlType.Custom) .Select(element => element.Patterns.Value.Pattern.Value.ToString())); labels.Add("Get row content"); mss.Add(stopwatch.ElapsedMilliseconds); var performance = string.Join("\n", labels.Zip(mss, (label, t) => string.Format("{0}: {1}", label, t))); MessageBox.Show( rowContent + '\n' + performance, "Selected row:", MessageBoxButtons.OK, MessageBoxIcon.Information); #else var dataGridView = mainWindow.FindFirstChild(ControlType.Table).AsDataGridView(); if (dataGridView == null) { return; } labels.Add("Find DataGridView"); mss.Add(stopwatch.ElapsedMilliseconds); if (highlight) { dataGridView.DrawHighlight(); labels.Add("Highlight DataGridView"); mss.Add(stopwatch.ElapsedMilliseconds); } // Too slow if the DataGridView contains many rows. var rows = dataGridView.Rows; labels.Add("Get all rows"); mss.Add(stopwatch.ElapsedMilliseconds); foreach (var row in rows) { labels.Add("Iterate row"); mss.Add(stopwatch.ElapsedMilliseconds); #if false // WinForms DataGridView doesn't support this pattern. if (row.Patterns.SelectionItem.Pattern.IsSelected) #elif USE_UIA2 if (row.Properties.BoundingRectangle.Value.Contains(point)) #else // FlaUI.UIA2 doesn't support this pattern. // FlaUI.UIA3 supports this pattern. // References: // [LegacyIAccessible in Windows.Automation](https://stackoverflow.com/questions/6837935/legacyiaccessible-in-windows-automation) // https://github.com/TestStack/UIAComWrapper/blob/master/UiaComWrapper/LegacyIAccessiblePattern.cs // https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/Patterns/LegacyIAccessiblePattern.cs // https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.UIA2/Patterns/UnsupportedPatterns.cs // https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.UIA3/Patterns/LegacyIAccessiblePattern.cs if (row.Patterns.LegacyIAccessible.Pattern.State.Value.HasFlag( AccessibilityState.STATE_SYSTEM_SELECTED)) #endif { if (highlight) { row.DrawHighlight(); labels.Add("Highlight row"); mss.Add(stopwatch.ElapsedMilliseconds); } // In .NET 4.6.1, each cell is DateItem(UIA3) or Custom(UIA2). // In .NET 4.7.2, each cell is a Edit(UIA3) or Custom(UIA2). var cells = #if USE_UIA2 row.FindAllChildren(ControlType.Custom); #else row.FindAllChildren(ControlType.DataItem); if (cells.Length == 0) { cells = row.FindAllChildren(ControlType.Edit); if (cells.Length > 0) { MsgBox.Information("Each cell is a Edit."); } } #endif var rowContent = string.Join("\n", cells.Select(element => element.Name + ' ' + element.Patterns.Value.Pattern.Value.ToString())); labels.Add("Get row content"); mss.Add(stopwatch.ElapsedMilliseconds); var performance = string.Join("\n", labels.Zip(mss, (label, t) => string.Format("{0}: {1}", label, t))); MessageBox.Show( rowContent + '\n' + performance, "Selected row:", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } #endif } }
public Window(UIA2Automation automation, AutomationElement nativeElement) : base(automation, nativeElement) { }