Best FlaUI code snippet using FlaUI.Core.AutomationElements.Window
AppManager.cs
Source:AppManager.cs
...17 private readonly string _exePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "OfficeRibbonXEditor.exe");18 private bool _disposed;19 public Application? App { get; private set; }20 public AutomationBase? Automation { get; private set; }21 public Window? Window { get; private set; }22 public Menu? FileMenu => Window?.FindFirstDescendant(x => x.ByText("File"), TimeSpan.FromSeconds(1)).AsMenu();23 public Menu? HelpMenu => Window?.FindFirstDescendant(x => x.ByText("Help"), TimeSpan.FromSeconds(1)).AsMenu();24 public void Launch(params string[] arguments)25 {26 var psi = new ProcessStartInfo27 {28 FileName = _exePath,29 Arguments = string.Join(" ", arguments.Select(x => $"\"{x}\"")),30 WorkingDirectory = TestContext.CurrentContext.TestDirectory,31 };32 App = Application.Launch(psi);33 Automation = new UIA3Automation();34 App.WaitWhileMainHandleIsMissing(TimeSpan.FromSeconds(10));35 Window = App.GetMainWindow(Automation, TimeSpan.FromSeconds(10));36 Assume.That(Window, Is.Not.Null, "Cannot find main window");37 }38 public Window[] GetTopLevelWindows()39 {40 if (App == null || Automation == null)41 {42 return Array.Empty<Window>();43 }44 return App.GetAllTopLevelWindows(Automation);45 }46 /// <summary>47 /// Attempts to close the application gracefully by closing any unsaved changes prompt that appears when closing the48 /// main window. Otherwise, killing the application process will result in no code coverage.49 /// </summary>50 private void Close()51 {52 if (App == null)53 {54 return;55 }56 var status = TestContext.CurrentContext.Result.Outcome.Status;57 if (status == TestStatus.Failed)58 {59 Window?.TestCapture("MainWindow.png", "Main Window status when the test failed");60 // TODO: Capture all modal windows and top-level windows61 }62 while (Window?.ModalWindows.Any() ?? false)63 {64 Window.ModalWindows.First().Close();65 App.WaitWhileBusy();66 }67 Window?.Close();68 try69 {70 // TODO: Loop might not be needed if WaitWhileBusy() is really working71 for (var attempts = 0; attempts < 10 && !App.HasExited; ++attempts)72 {73 App.WaitWhileBusy();74 var dialog = Window?.ModalWindows.FirstOrDefault();75 dialog?.FindFirstChild(x => x.ByName("No")).Click();76 }77 }78 catch (NoClickablePointException)79 {80 }81 catch (InvalidOperationException)82 {83 }84 catch (COMException)85 {86 // This is new from .NET 6. Accessing ModalWindows can cause:87 // Error Message:88 // TearDown: System.Runtime.InteropServices.COMException : Catastrophic failure(0x8000FFFF(E_UNEXPECTED))89 // Stack Trace:90 // --TearDown91 // at Interop.UIAutomationClient.IUIAutomationElement.FindAll(TreeScope scope, IUIAutomationCondition condition)92 // at FlaUI.UIA3.UIA3FrameworkAutomationElement.FindAll(TreeScope treeScope, ConditionBase condition)93 // at FlaUI.Core.AutomationElements.AutomationElement.FindAll(TreeScope treeScope, ConditionBase condition)94 // at FlaUI.Core.AutomationElements.AutomationElement.FindAllChildren(ConditionBase condition)95 // at FlaUI.Core.AutomationElements.AutomationElement.FindAllChildren(Func`2 conditionFunc)96 // at FlaUI.Core.AutomationElements.Window.get_ModalWindows()97 // at OfficeRibbonXEditor.UITests.Helpers.AppManager.Close() in D:\a\1\s\tests\UITests\Helpers\AppManager.cs:line 9098 // at OfficeRibbonXEditor.UITests.Helpers.AppManager.Dispose(Boolean disposing) in D:\a\1\s\tests\UITests\Helpers\AppManager.cs:line 12299 // at OfficeRibbonXEditor.UITests.Helpers.AppManager.Dispose() in D:\a\1\s\tests\UITests\Helpers\AppManager.cs:line 108100 // at OfficeRibbonXEditor.UITests.Main.MainWindowTests.TearDown() in D:\a\1\s\tests\UITests\Main\MainWindowTests.cs:line 37101 // Unfortunately, this might mean that the code coverage won't be collected correctly for the tests which cause this102 }103 Automation?.Dispose();104 App.Close();105 }106 /// <summary>Disposes the application.</summary>107 public void Dispose()108 {109 Dispose(true);110 GC.SuppressFinalize(this);111 }112 /// <summary>Disposes the application.</summary>113 protected virtual void Dispose(bool disposing)114 {...
Form1.cs
Source:Form1.cs
...4using System.Data;5using System.Drawing;6using System.Linq;7using System.Text;8using System.Windows.Forms;9using FlaUI.UIA2;10using FlaUI.Automation;11using FlaUI.Core.WindowsAPI;12using FlaUI.Core.AutomationElements;13using FlaUI.Core.Tools;14using FlaUI.Core.Definitions;15using FlaUI.Core.Conditions;16using FlaUI.UIA3;17using FlaUI.UIA2.Patterns;18using FlaUI.Core.AutomationElements.Infrastructure;19using FlaUI.Core.AutomationElements.PatternElements;20using FlaUI.Core.Patterns.Infrastructure;21namespace FlaUI.Automation22{23 public partial class Form1 : Form24 {25 public Form1()26 {27 InitializeComponent();28 }29 private void Form1_Load(object sender, EventArgs e)30 {31 }32 private void button1_Click(object sender, EventArgs e)33 {34 var app = FlaUI.Core.Application.Launch("TaskListApp.exe");35 using (var automation = new UIA3Automation())36 {37 var window = app.GetMainWindow(automation);38 //var txtDesc = window.FindFirst(TreeScope.Descendants, window.ConditionFactory.ByAutomationId("txtTaskDescription"));39 // PropertyCondition xEllist3 = new PropertyCondition(("txtTaskDescription"), "CustomHeaderClass", PropertyConditionFlags.IgnoreCase);40 //var txtDesc = window.FindFirst(TreeScope.Children, new PropertyCondition(ControlType.Text));41 var txtDesc = window.FindFirst(TreeScope.Descendants, window.ConditionFactory.ByAutomationId("txtTaskDescription")).AsTextBox();42 txtDesc.Text = "Hello world";43 //var menu = window.FindFirst(TreeScope.Descendants, window.ConditionFactory.ByAutomationId("menuone")).AsMenu();44 var btn = window.FindFirst(TreeScope.Descendants, window.ConditionFactory.ByAutomationId("btnSave"));45 btn.Click();46 //FlaUI.Core.AutomationElements.Infrastructure.AutomationElement menuElement = window.FindFirst(TreeScope.Descendants, new PropertyCondition(FlaUI.Core.AutomationElements.Infrastructure.AutomationElement., menuName)); 47 48 49 }50 }51 }...
Notepad.cs
Source:Notepad.cs
...18 protected override void Execute(CodeActivityContext context)19 {20 // Start Application21 var app = Application.Launch("notepad.exe");22 var window = app.GetMainWindow(new UIA3Automation());23 Console.WriteLine(window.Title);24 25 ConditionFactory cf = new ConditionFactory(new UIA3PropertyLibrary());26 window.FindFirstDescendant(cf.ByName("ТекÑÑовÑй ÑедакÑоÑ")).AsTextBox().Enter("Hello World");27 Console.ReadLine();28 }29 }30}...
Window
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.AutomationElements.Infrastructure;3using FlaUI.Core.Definitions;4using FlaUI.Core.Input;5using FlaUI.Core.WindowsAPI;6using FlaUI.UIA3;7using System;8using System.Diagnostics;9{10 {11 static void Main(string[] args)12 {13 var application = Application.Launch(@"C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE");14 var automation = new UIA3Automation();15 using (var automation1 = new UIA3Automation())16 {17 var window = application.GetMainWindow(automation1);18 var button = window.FindFirstDescendant(cf => cf.ByAutomationId("FileSaveAsMenu")).AsButton();19 button.Invoke();20 var dialog = window.ModalWindows()[0];21 var textbox = dialog.FindFirstDescendant(cf => cf.ByAutomationId("1001")).AsTextBox();22 textbox.Text = "C:\\Users\\johndoe\\Desktop\\Test.docx";23 var buttonSave = dialog.FindFirstDescendant(cf => cf.ByAutomationId("1")).AsButton();24 buttonSave.Invoke();25 Console.WriteLine("Press any key to exit");26 Console.ReadKey();27 }28 }29 }30}
Window
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core;3using FlaUI.Core.Input;4using FlaUI.Core.WindowsAPI;5using FlaUI.Core.WindowsAPI;6using System.Diagnostics;7using System.Threading;8using System;9using System.Drawing;10using System.Drawing.Imaging;11using System.Collections.Generic;12using System.IO;13using System.Linq;14using System.Windows.Forms;15{16 {17 static void Main(string[] args)18 {19 ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\calc.exe");20 Process.Start(startInfo);21 Thread.Sleep(1000);22 var application = Automation.GetApplication("calc.exe");23 var window = application.GetWindow("Calculator");24 var buttons = window.FindAllChildren();25 var text = buttons.Select(b => b.AsButton().Name);26 foreach (var txt in text)27 {28 Console.WriteLine(txt);29 }30 window.FindFirstDescendant(cf => cf.ByAutomationId("num5Button")).AsButton().Invoke();31 window.FindFirstDescendant(cf => cf.ByAutomationId("plusButton")).AsButton().Invoke();32 window.FindFirstDescendant(cf => cf.ByAutomationId("num5Button")).AsButton().Invoke();33 window.FindFirstDescendant(cf => cf.ByAutomationId("equalButton")).AsButton().Invoke();34 var result = window.FindFirstDescendant(cf
Window
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.AutomationElements.Infrastructure;3using FlaUI.Core.Definitions;4using FlaUI.Core.Input;5using FlaUI.Core.Tools;6using FlaUI.UIA3;7using System;8using System.Collections.Generic;9using System.Linq;10using System.Text;11using System.Threading.Tasks;12{13 {14 static void Main(string[] args)15 {16 var automation = new UIA3Automation();17 var process = FlaUI.Core.Application.GetApplication("notepad").GetMainWindow(automation);18 var child = process.FindFirstChild(cf => cf.ByControlType(ControlType.Edit));19 child.AsTextBox().Text = "Hello World";20 var child2 = process.FindFirstChild(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Save")));21 child2.AsButton().Click();22 var child3 = process.FindFirstChild(cf => cf.ByControlType(ControlType.Window).And(cf.ByName("Save As")));23 var child4 = child3.FindFirstChild(cf => cf.ByControlType(ControlType.Edit));24 child4.AsTextBox().Text = "C:\\Users\\Public\\Documents\\test.txt";25 var child5 = child3.FindFirstChild(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Save")));26 child5.AsButton().Click();27 }28 }29}
Window
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.Conditions;3using FlaUI.Core.Input;4using FlaUI.Core.WindowsAPI;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10{11 {12 static void Main(string[] args)13 {14 var app = FlaUI.Core.Application.Launch(@"C:\Program Files\Notepad++\notepad++.exe");15 var automation = FlaUI.Core.Automation.AutomationFactory.GetAutomation();16 var window = app.GetMainWindow(automation);17 var menu = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Menu));18 var menuItem = menu.FindFirstDescendant(cf => cf.ByText("Help"));19 menuItem.Click();20 var subMenuItem = menuItem.FindFirstDescendant(cf => cf.ByText("About Notepad++"));21 subMenuItem.Click();22 var button = window.FindFirstDescendant(cf => cf.ByControlType(FlaUI.Core.Definitions.ControlType.Button).And(cf.ByText("OK")));23 button.Click();24 app.Close();25 }26 }27}
Window
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.Definitions;3using FlaUI.Core.Input;4using FlaUI.Core.WindowsAPI;5using FlaUI.UIA3;6using System;7using System.Collections.Generic;8using System.Diagnostics;9using System.Linq;10using System.Text;11using System.Threading.Tasks;12{13 {14 static void Main(string[] args)15 {16 Process process = Process.Start("C:\\Windows\\System32\\calc.exe");17 var automation = new UIA3Automation();18 var window = automation.GetDesktop().FindFirstChild(cf => cf.ByName("Calculator").And(cf.ByControlType(ControlType.Window))).AsWindow();19 window.FindFirstDescendant(cf => cf.ByName("1").And(cf.ByControlType(ControlType.Button))).AsButton().Click();20 window.FindFirstDescendant(cf => cf.ByName("2").And(cf.ByControlType(ControlType.Button))).AsButton().Click();21 window.FindFirstDescendant(cf => cf.ByName("3").And(cf.ByControlType(ControlType.Button))).AsButton().Click();22 window.FindFirstDescendant(cf => cf.ByName("4").And(cf.ByControlType(ControlType.Button))).AsButton().Click();23 window.FindFirstDescendant(cf => cf.ByName("5").And(cf.ByControlType(ControlType.Button))).AsButton().Click();24 window.FindFirstDescendant(cf => cf.ByName("6").And(cf.ByControlType(ControlType.Button))).AsButton().Click();25 window.FindFirstDescendant(cf => cf.ByName("7").And(cf.ByControlType(ControlType.Button))).AsButton().Click();26 window.FindFirstDescendant(cf => cf.ByName("8").And(cf.ByControlType(ControlType.Button))).AsButton().Click();27 window.FindFirstDescendant(cf => cf.ByName("9").And(cf.ByControlType(ControlType.Button))).AsButton().Click();28 window.FindFirstDescendant(cf => cf.ByName("0").And(cf.ByControlType(ControlType.Button))).AsButton().Click();29 window.FindFirstDescendant(cf => cf.ByName("Add").And(cf.ByControlType(ControlType.Button))).AsButton().Click();30 window.FindFirstDescendant(cf => cf.ByName("1
Window
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.AutomationElements.Infrastructure;3using FlaUI.Core.Definitions;4using FlaUI.Core.Input;5using FlaUI.Core.Tools;6using FlaUI.Core.WindowsAPI;7using FlaUI.UIA3;8using FlaUI.UIA3.Converters;9using FlaUI.UIA3.Extensions;10using FlaUI.UIA3.Patterns;11using FlaUI.UIA3.Tools;12using FlaUI.UIA3.Windows;13using FlaUI.UIA3.WindowsAPI;14using FlaUI.UIA3.WindowsAPI.Enums;15using FlaUI.UIA3.WindowsAPI.Structures;16using FlaUI.UIA3.WindowsAPI.Types;17using FlaUI.UIA3.WindowsAPI.Types.Enums;18using System.Collections.Generic;19using System.Diagnostics;20using System.Drawing;21using System.Linq;22using System.Runtime.InteropServices;23using System.Text;24using System.Threading.Tasks;25using System.Windows.Automation;26using System.Windows.Automation.Text;27using System.Windows.Forms;28using System.Windows.Interop;29using System.Windows.Media;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!