Best FlaUI code snippet using FlaUI.Core.AutomationElements.MenuItems.MenuItems
Notepad.cs
Source:Notepad.cs
...19 _window = application.GetMainWindow(automation);20 }21 public void ZoomIn()22 {23 var scaleUpBtn = GetZoomMenuItems().Single(menuItem => menuItem.Name == "УвелиÑиÑÑ");24 scaleUpBtn.WaitUntilClickable();25 scaleUpBtn.Click();26 }27 public void ZoomOut()28 {29 var scaleUpBtn = GetZoomMenuItems().Single(menuItem => menuItem.Name == "УменÑÑиÑÑ");30 scaleUpBtn.WaitUntilClickable();31 scaleUpBtn.Click();32 }33 public int GetZoomPercent()34 {35 var zoomText = _window.FindAllByXPath("/StatusBar/Text[3]")[0].Name;36 var zoomNumberString = Regex.Match(zoomText, @"[0-9]+").ToString();37 return Convert.ToInt32(zoomNumberString);38 }39 public void WriteToDocument(string text)40 {41 var document = _window.FindAllChildren().Single(element =>42 element.ControlType == ControlType.Document && element.ClassName == "Edit");43 document.Click();44 Keyboard.Type(text);45 }46 public string OpenDocument(string path)47 {48 var openBtn = GetFileMenuItems().Single(menuItem =>49 menuItem.ControlType == ControlType.MenuItem && menuItem.Name == "ÐÑкÑÑÑÑ...");50 openBtn.WaitUntilClickable();51 openBtn.Click();52 53 Retry.WhileTrue(() => _window.ModalWindows.Length == 0);54 var openingModal = _window.ModalWindows[0].AsWindow();55 var nameEdit = openingModal.FindAllDescendants().Single(element =>56 element.ControlType == ControlType.Edit && element.Name == "ÐÐ¼Ñ Ñайла:");57 58 Retry.WhileTrue(() => !nameEdit.FrameworkAutomationElement.HasKeyboardFocus);59 Keyboard.Type(path);60 61 var openingBtn = _window.FindAllByXPath("/Window/Button[1]")[0];62 openingBtn.WaitUntilClickable();63 openingBtn.Click();64 var document = _window.FindAllChildren().Single(element =>65 element.ControlType == ControlType.Document && element.ClassName == "Edit");66 67 return document.AsTextBox().Text;68 }69 public void SaveDocument(string path)70 {71 var saveBtn = GetFileMenuItems().Single(menuItem =>72 menuItem.ControlType == ControlType.MenuItem && menuItem.Name == "СоÑ
ÑаниÑÑ");73 saveBtn.WaitUntilClickable();74 saveBtn.Click();75 76 Retry.WhileTrue(() => _window.ModalWindows.Length == 0);77 var savingModal = _window.ModalWindows[0].AsWindow();78 var nameEdit = savingModal.FindAllDescendants().Single(element =>79 element.ControlType == ControlType.Edit && element.Name == "ÐÐ¼Ñ Ñайла:");80 Retry.WhileTrue(() => !nameEdit.FrameworkAutomationElement.HasKeyboardFocus);81 Keyboard.Type(path);82 var savingBtn = savingModal.FindAllDescendants().Single(element =>83 element.ControlType == ControlType.Button && element.Name == "СоÑ
ÑаниÑÑ");84 savingBtn.WaitUntilClickable();85 savingBtn.Click();86 }87 public string GetWindowTitle()88 {89 return _window.Title;90 }91 private MenuItems GetZoomMenuItems()92 {93 var menuItems = GetMenuItems();94 95 var viewTab = menuItems.Single(menuItem =>96 menuItem.ControlType == ControlType.MenuItem && menuItem.Name == "Ðид");97 viewTab.WaitUntilClickable();98 viewTab.Click();99 100 var viewMenu = _window.FindAllDescendants().Single(element =>101 element.ControlType == ControlType.Menu && element.Name == "Ðид");102 103 var scaleTab = viewMenu.FindAllChildren().Single(104 menuItem => menuItem.ControlType == ControlType.MenuItem && menuItem.Name == "ÐаÑÑÑаб");105 scaleTab.WaitUntilClickable();106 scaleTab.Click();107 108 var scaleMenu = _window.FindAllDescendants().Single(element =>109 element.ControlType == ControlType.Menu && element.Name == "ÐаÑÑÑаб");110 return scaleMenu.AsMenu().Items;111 }112 private MenuItems GetFileMenuItems()113 {114 var fileTab = GetMenuItems().Single(menuItem =>115 menuItem.ControlType == ControlType.MenuItem && menuItem.Name == "Файл");116 fileTab.WaitUntilClickable();117 fileTab.Click();118 return _window.FindAllChildren().Single(element =>119 element.ControlType == ControlType.Menu && element.Name == "Файл").AsMenu().Items;120 }121 122 private MenuItems GetMenuItems()123 {124 return _window.FindAllByXPath("/MenuBar")[0].AsMenu().Items;125 }126 public void Dispose()127 {128 _window.Close();129 }130 }131}...
MenuItem.cs
Source:MenuItem.cs
...29 public string Text => Properties.Name.Value;30 /// <summary>31 /// Gets all <see cref="MenuItem"/> which are inside this element.32 /// </summary>33 public MenuItems Items34 {35 get36 {37 // Special handling for Win32 context menus38 if (IsWin32Menu)39 {40 // Click the item to load the child items41 Click();42 // In Win32, the nested menu items are below a menu control which is below the application window43 // So search the app window first44 var appWindow = FrameworkAutomationElement.Automation.GetDesktop().FindFirstChild(cf => cf.ByControlType(ControlType.Window).And(cf.ByProcessId(Properties.ProcessId)));45 // Then search the menu below the window46 var menu = appWindow.FindFirstChild(cf => cf.ByControlType(ControlType.Menu).And(cf.ByName(Text))).AsMenu();47 menu.IsWin32Menu = true;48 // Now return the menu items49 return menu.Items;50 }51 // Expand if needed, WinForms does not have the expand pattern but all children are already visible so it works as well52 if (Patterns.ExpandCollapse.IsSupported)53 {54 ExpandCollapseState state;55 do56 {57 state = _expandCollapseAutomationElement.ExpandCollapseState;58 if (state == ExpandCollapseState.Collapsed)59 {60 Expand();61 }62 Thread.Sleep(50);63 } while (state != ExpandCollapseState.Expanded);64 }65 var childItems = FindAllChildren(cf => cf.ByControlType(ControlType.MenuItem)).Select(e => e.AsMenuItem());66 return new MenuItems(childItems);67 }68 }69 /// <summary>70 /// Invokes the element.71 /// </summary>72 public MenuItem Invoke()73 {74 _invokeAutomationElement.Invoke();75 return this;76 }77 /// <summary>78 /// Expands the element.79 /// </summary>80 public MenuItem Expand()...
Test.cs
Source:Test.cs
1using System;2using System.Linq;3using System.Threading.Tasks;4using FlaUI.Core.AutomationElements;5using FlaUI.Core.Input;6using FlaUI.Core.WindowsAPI;7using FlaUI.UIA3;8using FluentAssertions;9using Xunit;10namespace Cooking.Tests.UITests;11public class TagTest : UITest12{13 public TagTest(BuildAppFixture appFixture) : base(appFixture)14 {15 }16 [Fact]17 public async Task CreateTag_CreatesTag()18 {19 using var app = FlaUI.Core.Application.Launch(@$"{AppFixture.OutputDirectory}\Cooking.WPF.exe");20 using var automation = new UIA3Automation();21 Window? window = app.GetMainWindow(automation, TimeSpan.FromSeconds(15));22 await CreateTag(window);23 AutomationElement[]? tagsAfterAdd = window.FindAllDescendants(cf => cf.ByAutomationId("TagTextBlock"));24 app.Kill();25 tagsAfterAdd.Length.Should().Be(2);26 }27 [Fact]28 public async Task DeleteTag_DeletesTag()29 {30 using var app = FlaUI.Core.Application.Launch(@$"{AppFixture.OutputDirectory}\Cooking.WPF.exe");31 using var automation = new UIA3Automation();32 Window? window = app.GetMainWindow(automation, TimeSpan.FromSeconds(15));33 await CreateTag(window);34 // Click delete button on last tag35 window.FindAllDescendants(x => x.ByAutomationId("TagDeleteButton")).Last().AsButton().Invoke();36 await WaitForDialogOpen();37 // When menu shown, press left (by default it's focused on Decline button) and Enter to submit38 Keyboard.Press(VirtualKeyShort.LEFT);39 await Task.Delay(50);40 Keyboard.Press(VirtualKeyShort.ENTER);41 await WaitForDialogClose();42 // Wait for grid update43 await Task.Delay(300);44 AutomationElement[]? tagsAfter = window.FindAllDescendants(cf => cf.ByAutomationId("TagTextBlock"));45 app.Kill();46 tagsAfter.Length.Should().Be(1);47 }48 private static async Task CreateTag(Window window)49 {50 AutomationElement? menuItems = window.FindFirstChild("ButtonsListView");51 menuItems.FindAllChildren()[4].AsListBoxItem().Select();52 window.FindFirstDescendant(cf => cf.ByAutomationId("AddTagButton")).AsButton().Invoke();53 await WaitForDialogOpen();54 window.FindFirstDescendant(cf => cf.ByAutomationId("TagNameTextBox")).AsTextBox().Text = Guid.NewGuid().ToString();55 window.FindFirstDescendant(cf => cf.ByAutomationId("TagTypeComboBox")).AsComboBox().Select("ÐÑÑоÑник");56 window.FindFirstDescendant(cf => cf.ByAutomationId("OkButton")).AsButton().Invoke();57 await WaitForDialogClose();58 }59 private static async Task WaitForDialogOpen()60 {61 // Wait for dialog open animation62 await Task.Delay(500);63 }64 private static async Task WaitForDialogClose()65 {66 // Wait for dialog close animation67 await Task.Delay(300);68 }69}...
MenuItems
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core;7using FlaUI.Core.AutomationElements;8using FlaUI.Core.AutomationElements.Infrastructure;9using FlaUI.Core.Definitions;10using FlaUI.Core.Input;11using FlaUI.Core.Tools;12using FlaUI.Core.WindowsAPI;13using FlaUI.UIA3;14{15 {16 static void Main(string[] args)17 {18 var application = Application.Launch(@"C:\Windows\System32\notepad.exe");19 var automation = new UIA3Automation();20 var window = application.GetMainWindow(automation);21 var menuItem = window.FindFirstDescendant(x => x.ByText("Help")).AsMenuItem();22 menuItem.Click();23 foreach (var item in menuItem.MenuItems)24 {25 Console.WriteLine(item.Name);26 }27 Console.ReadLine();28 }29 }30}
MenuItems
Using AI Code Generation
1using System;2using System.Threading;3using FlaUI.Core;4using FlaUI.Core.AutomationElements;5using FlaUI.Core.AutomationElements.Infrastructure;6using FlaUI.Core.Conditions;7using FlaUI.Core.Definitions;8using FlaUI.Core.Input;9using FlaUI.Core.Tools;10using FlaUI.UIA3;11using System.Windows.Automation;12using System.Windows.Automation.Text;13using System.Windows.Automation.Provider;14using System.Windows.Automation.Peers;15using System.Windows.Automation;16using System.Windows.Automation.Text;17using System.Windows.Automation.Provider;18using System.Windows.Automation.Peers;19using System.Windows.Automation;20using System.Windows.Automation.Text;21using System.Windows.Automation.Provider;22using System.Windows.Automation.Peers;23using System.Windows.Automation;24using System.Windows.Automation.Text;25using System.Windows.Automation.Provider;26using System.Windows.Automation.Peers;27using System.Windows.Automation;28using System.Windows.Automation.Text;29using System.Windows.Automation.Provider;30using System.Windows.Automation.Peers;31using System.Windows.Automation;32using System.Windows.Automation.Text;33using System.Windows.Automation.Provider;34using System.Windows.Automation.Peers;35using System.Windows.Automation;36using System.Windows.Automation.Text;37using System.Windows.Automation.Provider;38using System.Windows.Automation.Peers;39using System.Windows.Automation;40using System.Windows.Automation.Text;41using System.Windows.Automation.Provider;42using System.Windows.Automation.Peers;43{44 {45 static void Main(string[] args)46 {47 using (var automation = new UIA3Automation())48 {49 var window = Application.Launch("C:\\Windows\\System32\\calc.exe").GetMainWindow(automation);50 var menuBar = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.MenuBar)).AsMenuBar();51 var menuItems = menuBar.MenuItems();52 foreach (var menuItem in menuItems)53 {54 Console.WriteLine(menuItem.Name);55 }56 }57 }58 }59}60using System;61using System.Threading;62using FlaUI.Core;63using FlaUI.Core.AutomationElements;64using FlaUI.Core.AutomationElements.Infrastructure;65using FlaUI.Core.Conditions;66using FlaUI.Core.Definitions;67using FlaUI.Core.Input;68using FlaUI.Core.Tools;69using FlaUI.UIA3;70using System.Windows.Automation;71using System.Windows.Automation.Text;72using System.Windows.Automation.Provider;73using System.Windows.Automation.Peers;74using System.Windows.Automation;75using System.Windows.Automation.Text;76using System.Windows.Automation.Provider;
MenuItems
Using AI Code Generation
1using System;2using System.Windows.Automation;3using FlaUI.Core;4using FlaUI.Core.AutomationElements;5using FlaUI.Core.AutomationElements.Infrastructure;6using FlaUI.Core.Definitions;7using FlaUI.Core.Input;8using FlaUI.Core.WindowsAPI;9{10 {11 static void Main(string[] args)12 {13 var app = Application.Launch(@"C:\Windows\System32\calc.exe");14 var automation = AutomationUtil.GetAutomation();15 var mainWindow = app.GetMainWindow(automation);16 var menu = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Menu)).AsMenu();17 var menuItems = menu.MenuItems();18 Console.WriteLine("Menu Items are:");19 foreach (var item in menuItems)20 {21 Console.WriteLine(item);22 }23 Console.ReadKey();24 }25 }26}27using System;28using System.Windows.Automation;29using FlaUI.Core;30using FlaUI.Core.AutomationElements;31using FlaUI.Core.AutomationElements.Infrastructure;32using FlaUI.Core.Definitions;33using FlaUI.Core.Input;34using FlaUI.Core.WindowsAPI;35{36 {37 static void Main(string[] args)38 {39 var app = Application.Launch(@"C:\Windows\System32\calc.exe");40 var automation = AutomationUtil.GetAutomation();41 var mainWindow = app.GetMainWindow(automation);42 var menu = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Menu)).AsMenu();43 var menuItems = menu.MenuItems();44 Console.WriteLine("Menu Items are:");45 foreach (var item in menuItems)46 {47 Console.WriteLine(item);48 }49 Console.ReadKey();50 }51 }52}53using System;54using System.Windows.Automation;55using FlaUI.Core;56using FlaUI.Core.AutomationElements;57using FlaUI.Core.AutomationElements.Infrastructure;58using FlaUI.Core.Definitions;59using FlaUI.Core.Input;60using FlaUI.Core.WindowsAPI;61{62 {63 static void Main(string[] args)64 {
MenuItems
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.UIA2;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 using (var automation = new UIA2Automation())17 {18 var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");19 var window = app.GetMainWindow(automation);20 var menu = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Menu)).AsMenu();21 var menuItem = menu.MenuItems().FirstOrDefault(x => x.Name == "View");22 var subMenuItem = menuItem.MenuItems().FirstOrDefault(x => x.Name == "Scientific");23 subMenuItem.Click();24 Console.WriteLine("Done");25 Console.ReadLine();26 }27 }28 }29}30I am trying to automate a WinForm application, but I am getting an error when I try to set the value of a ComboBox (the error is 'The element is not enabled'). I have tried to set the value of the ComboBox in different ways, but I always get the same error. Could you please help me in this regard?31using FlaUI.Core.AutomationElements;32using FlaUI.Core.AutomationElements.Infrastructure;33using FlaUI.Core.Definitions;34using FlaUI.Core.Input;35using FlaUI.Core.Tools;36using FlaUI.UIA2;37using System;38using System.Collections.Generic;39using System.Linq;40using System.Text;41using System.Threading.Tasks;42{43 {44 static void Main(string[] args)45 {46 using (var automation = new UIA2Automation())47 {48 var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");
MenuItems
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.EventHandlers;3using FlaUI.Core.Input;4using FlaUI.Core.WindowsAPI;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10using FlaUI.Core;11using FlaUI.Core.Conditions;12using FlaUI.Core.Definitions;13using FlaUI.Core.Tools;14using FlaUI.Core.WindowsAPI;15using FlaUI.Core.WindowsAPI;16using FlaUI.UIA3;17using FlaUI.UIA3.EventHandlers;18using FlaUI.UIA3.Patterns;19using FlaUI.UIA3.Tools;20using FlaUI.Core.AutomationElements.Infrastructure;21using FlaUI.Core.AutomationElements.PatternElements;22using FlaUI.Core.Identifiers;23using System.Drawing;24using FlaUI.Core.Tools;25using FlaUI.Core.WindowsAPI;26using System.Windows;27using System.Windows.Input;28using System.Windows.Forms;29using FlaUI.Core.EventHandlers;30using FlaUI.Core.Input;31using FlaUI.Core.WindowsAPI;32using System;33using System.Collections.Generic;34using System.Linq;35using System.Text;36using System.Threading.Tasks;37using FlaUI.Core;38using FlaUI.Core.Conditions;39using FlaUI.Core.Definitions;40using FlaUI.Core.Tools;41using FlaUI.Core.WindowsAPI;42using FlaUI.Core.WindowsAPI;43using FlaUI.UIA3;44using FlaUI.UIA3.EventHandlers;45using FlaUI.UIA3.Patterns;46using FlaUI.UIA3.Tools;47using FlaUI.Core.AutomationElements.Infrastructure;48using FlaUI.Core.AutomationElements.PatternElements;49using FlaUI.Core.Identifiers;50using System.Drawing;51using FlaUI.Core.Tools;52using FlaUI.Core.WindowsAPI;53using System.Windows;54using System.Windows.Input;55using System.Windows.Forms;56using FlaUI.Core.EventHandlers;57using FlaUI.Core.Input;58using FlaUI.Core.WindowsAPI;59using System;60using System.Collections.Generic;61using System.Linq;62using System.Text;63using System.Threading.Tasks;64using FlaUI.Core;65using FlaUI.Core.Conditions;66using FlaUI.Core.Definitions;67using FlaUI.Core.Tools;68using FlaUI.Core.WindowsAPI;69using FlaUI.Core.WindowsAPI;70using FlaUI.UIA3;71using FlaUI.UIA3.EventHandlers;72using FlaUI.UIA3.Patterns;73using FlaUI.UIA3.Tools;74using FlaUI.Core.AutomationElements.Infrastructure;75using FlaUI.Core.AutomationElements.PatternElements;76using FlaUI.Core.Identifiers;77using System.Drawing;78using FlaUI.Core.Tools;
MenuItems
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.UIA2;3using FlaUI.Core.AutomationElements;4using FlaUI.Core.AutomationElements.Infrastructure;5using FlaUI.Core.Definitions;6using FlaUI.Core.Input;7using FlaUI.Core.WindowsAPI;8using System;9using System.Windows.Automation;10{11 {12 static void Main(string[] args)13 {14 var application = Application.Launch(@"C:\Windows\System32\calc.exe");15 var automation = new UIA2Automation();16 var window = application.GetMainWindow(automation);17 var menu = window.FindFirstDescendant(cf => cf.ByAutomationId("MenuBar")).AsMenu();18 var menuItems = menu.MenuItems;19 foreach (var item in menuItems)20 {21 Console.WriteLine(item.Name);22 }23 Console.ReadLine();24 }25 }26}27using FlaUI.Core;28using FlaUI.UIA2;29using FlaUI.Core.AutomationElements;30using FlaUI.Core.AutomationElements.Infrastructure;31using FlaUI.Core.Definitions;32using FlaUI.Core.Input;33using FlaUI.Core.WindowsAPI;34using System;35using System.Windows.Automation;36{37 {38 static void Main(string[] args)39 {40 var application = Application.Launch(@"C:\Windows\System32\calc.exe");41 var automation = new UIA2Automation();42 var window = application.GetMainWindow(automation);43 var menu = window.FindFirstDescendant(cf => cf.ByAutomationId("MenuBar")).AsMenu();44 var menuItems = menu.MenuItems;45 foreach (var item in menuItems)46 {47 if (item.Name == "Edit")48 {49 item.Click();50 break;51 }52 }53 Console.ReadLine();54 }55 }56}57using FlaUI.Core;58using FlaUI.UIA2;59using FlaUI.Core.AutomationElements;60using FlaUI.Core.AutomationElements.Infrastructure;61using FlaUI.Core.Definitions;62using FlaUI.Core.Input;63using FlaUI.Core.WindowsAPI;64using System;65using System.Windows.Automation;66{67 {68 static void Main(string[] args)
MenuItems
Using AI Code Generation
1using System;2using System.Windows.Automation;3using FlaUI.Core.AutomationElements;4using FlaUI.Core.Input;5using FlaUI.Core.WindowsAPI;6using FlaUI.UIA3;7using FlaUI.Core;8using FlaUI.Core.Definitions;9using System.Collections.Generic;10using System.Linq;11using System.Text;12using System.Threading.Tasks;13{14 {15 static void Main(string[] args)16 {17 using (var automation = new UIA3Automation())18 {19 var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\notepad.exe");20 var window = app.GetMainWindow(automation);21 var menu = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Menu)).AsMenu();22 var menuItems = menu.MenuItems;23 var helpMenuItem = menuItems[1];24 var subMenuItems = helpMenuItem.MenuItems;25 var aboutNotepadMenuItem = subMenuItems[0];26 aboutNotepadMenuItem.Click();
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!!