Best FlaUI code snippet using FlaUI.Core.AutomationElements.Grid.Grid
DataGridViewExtensions.cs
Source:DataGridViewExtensions.cs
...5using System.Collections.Generic;6using System.Linq;7namespace WinformsTools.IntegrationTestUtils.Extensions8{9 public static class DataGridViewExtensions10 {11 public static DataGridViewRow[] GetRows(this DataGridView table)12 {13 if (table == null)14 {15 throw new ArgumentNullException(nameof(table));16 }17 return Retry.WhileEmpty(() => GetDataGridViewRow(table)).Result;18 }19 public static IEnumerable<IDictionary<string, DataGridViewCell>> ToDictionary(this DataGridView table)20 {21 var columns = GetCellNames(table);22 return table.Rows.ToDictionary(columns);23 }24 public static IEnumerable<IDictionary<string, DataGridViewCell>> ToDictionary(this DataGridViewRow[] rows)25 {26 var table = rows[0].Parent.AsDataGridView();27 var columns = GetCellNames(table);28 return rows.ToDictionary(columns);29 }30 private static IEnumerable<IDictionary<string, DataGridViewCell>> ToDictionary(this DataGridViewRow[] rows, List<string> columns)31 {32 foreach (var row in rows)33 {34 yield return row.Cells.ToDictionary(columns);35 }36 }37 private static List<string> GetCellNames(DataGridView table)38 {39 var header = GetDataGridViewHeader(table);40 var headerColumns = GetDataGridViewHeaderColumns(header);41 return headerColumns.Select(x => x.Text).ToList();42 }43 public static IDictionary<string, DataGridViewCell> ToDictionary(this DataGridViewCell[] cells, IEnumerable<string> columns)44 {45 var dictionary = new Dictionary<string, DataGridViewCell>();46 for (var i = 0; i < cells.Length; i++)47 {48 dictionary.Add(columns.ElementAt(i), cells[i]);49 }50 return dictionary;51 }52 /// <summary>53 /// <see cref="DataGridView.Rows"/> does not work in culture different than English or German:54 /// <see href="https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/AutomationElements/DataGridView.cs"/>55 /// <see href="https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/Tools/LocalizedStrings.cs"/>56 /// </summary>57 /// <param name="dataGridView"></param>58 /// <returns></returns>59 private static DataGridViewRow[] GetDataGridViewRow(DataGridView dataGridView)60 {61 var rows = dataGridView.FindAllChildren(cf =>62 cf.ByControlType(ControlType.Custom)63 .Or(cf.ByControlType(ControlType.DataItem))64 .And(cf.ByName(CustomLocalizedStrings.DataGridViewHeader).Not())65 );66 // Remove the last row if we have the "add" row67 if (dataGridView.HasAddRow)68 {69 rows = rows.Take(rows.Length - 1).ToArray();70 }71 return rows.Select(x => new DataGridViewRow(x.FrameworkAutomationElement)).ToArray();72 }73 /// <summary>74 /// <see cref="DataGridView.Header"/> does not work in culture different than English or German75 /// <see href="https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/AutomationElements/DataGridView.cs"/>76 /// <see href="https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/Tools/LocalizedStrings.cs"/>77 /// </summary>78 /// <param name="dataGridView"></param>79 /// <returns></returns>80 private static DataGridViewHeader GetDataGridViewHeader(DataGridView dataGridView)81 {82 var header = dataGridView.FindFirstChild(cf =>83 cf.ByName(CustomLocalizedStrings.DataGridViewHeader)84 .Or(cf.ByControlType(ControlType.Header)));85 return header == null ? null : new DataGridViewHeader(header.FrameworkAutomationElement);86 }87 /// <summary>88 /// <see cref="DataGridViewHeader.Columns"/> does not work in culture different than English or German89 /// </summary>90 /// <returns></returns>91 private static DataGridViewHeaderItem[] GetDataGridViewHeaderColumns(DataGridViewHeader dataGridViewHeader)92 {93 // WinForms uses Header control type, WPF uses HeaderItem control type94 var headerItems = dataGridViewHeader.FindAllChildren(cf =>95 cf.ByControlType(ControlType.Header)96 .Or(cf.ByControlType(ControlType.HeaderItem)));97 var convertedHeaderItems = headerItems.Select(x => new DataGridViewHeaderItem(x.FrameworkAutomationElement))98 .ToList();99 // Remove the top-left header item if it exists (can be the first or last item)100 if (convertedHeaderItems.Last().Text == CustomLocalizedStrings.DataGridViewHeaderItemTopLeft)101 {102 convertedHeaderItems = convertedHeaderItems.Take(convertedHeaderItems.Count - 1).ToList();103 }104 else if (convertedHeaderItems.First().Text == CustomLocalizedStrings.DataGridViewHeaderItemTopLeft)105 {106 convertedHeaderItems = convertedHeaderItems.Skip(1).ToList();107 }108 return convertedHeaderItems.ToArray();109 }110 }111}...
UnitTest2.cs
Source:UnitTest2.cs
...51 , x => x != null, 3);52 var dataPresenter = retry(() => gridControlResultList53 .FindFirstDescendant("dataPresenter")54 , x => x != null, 3);55 var dataRows = dataPresenter.FindAllChildren().Select(x => x.AsGridRow());56 // scanIndex: 4303 Çà ´õºí Ŭ¸¯57 dataRows.Where(x => x.Cells[0]58 .Patterns59 .LegacyIAccessible60 .Pattern61 .Value62 .Value == "4303").First().DoubleClick();63 }64 }65 }66}...
BaseGrid.cs
Source:BaseGrid.cs
...9using FlaUI.UIA3;10using Microsoft.VisualStudio.TestTools.UnitTesting;11using FlaUI.Core.Definitions;12using UIAutomation.YouTubePlaylistSyncer.WPF.Extensions;13namespace UIAutomation.YouTubePlaylistSyncer.WPF.Grids {14 public class BaseGrid {15 private DataGridView grid;16 protected DataGridView Grid { 17 get => this.grid; 18 set {19 this.grid = value;20 this.ColumnIndices = this.GenerateColumnIndices();21 } 22 }23 protected DataGridViewRow[] Rows { get => Grid.Rows; }24 protected DataGridViewHeaderItem[] Columns { get => Grid.Header.Columns; }25 protected Dictionary<string, int> ColumnIndices;26 // Forcibly map column names to cell indices here since FlaUI doesn't support searching within a column27 private Dictionary<string, int> GenerateColumnIndices() {28 var dict = new Dictionary<string, int>();29 int i = 0;30 foreach (DataGridViewHeaderItem column in Columns) {31 dict[column.Name] = i;32 i++;33 }34 return dict;35 }36 }37}
Grid
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.AutomationElements;7using FlaUI.Core.AutomationElements.Infrastructure;8using FlaUI.Core.Definitions;9using FlaUI.Core.Tools;10{11 {12 static void Main(string[] args)13 {14 Application app = Application.Launch(@"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE");15 Window window = app.GetMainWindow(AutomationBase.ById("EXCEL"));16 Button button = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.FileTab")).AsButton();17 button.Click();18 Button button1 = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.File.New")).AsButton();19 button1.Click();20 Button button2 = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.File.New.Workbook")).AsButton();21 button2.Click();22 Button button3 = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.File.New.Workbook.ExcelWorkbook")).AsButton();23 button3.Click();24 Button button4 = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.File.New.Workbook.ExcelWorkbook.ExcelWorksheet")).AsButton();25 button4.Click();26 Button button5 = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.File.New.Workbook.ExcelWorkbook.ExcelWorksheet.InsertTable")).AsButton();27 button5.Click();28 Button button6 = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.File.New.Workbook.ExcelWorkbook.ExcelWorksheet.InsertTable.Table")).AsButton();29 button6.Click();30 Button button7 = window.FindFirstDescendant(cf => cf.ByAutomationId("Ribbon.File.New.Workbook.ExcelWorkbook.ExcelWorksheet.InsertTable.Table.OK")).AsButton();
Grid
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.AutomationElements;7using FlaUI.Core.AutomationElements.Infrastructure;8using FlaUI.Core.Definitions;9using FlaUI.Core.Identifiers;10{11 {12 static void Main(string[] args)13 {14 var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");15 var mainWindow = app.GetMainWindow(AutomationType.UIA3);16 var grid = mainWindow.FindFirstDescendant(cf => cf.ByClassName("CalcFrame")).AsGrid();17 var cell = grid.GetCell(3, 3);18 cell.Click();19 app.WaitWhileMainHandleIsMissing();20 }21 }22}23using System;24using System.Collections.Generic;25using System.Linq;26using System.Text;27using System.Threading.Tasks;28using FlaUI.Core.AutomationElements;29using FlaUI.Core.AutomationElements.Infrastructure;30using FlaUI.Core.Definitions;31using FlaUI.Core.Identifiers;32{33 {34 static void Main(string[] args)35 {36 var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");37 var mainWindow = app.GetMainWindow(AutomationType.UIA2);38 var grid = mainWindow.FindFirstDescendant(cf => cf.ByClassName("CalcFrame")).AsGrid();39 var cell = grid.GetCell(3, 3);40 cell.Click();41 app.WaitWhileMainHandleIsMissing();42 }43 }44}45using System;46using System.Collections.Generic;47using System.Linq;48using System.Text;49using System.Threading.Tasks;50using FlaUI.Core.AutomationElements;51using FlaUI.Core.AutomationElements.Infrastructure;52using FlaUI.Core.Definitions;
Grid
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.Patterns;11using FlaUI.Core.Tools;12using FlaUI.UIA3;13using System.Diagnostics;14using System.Threading;15using FlaUI.Core.Input;16using FlaUI.Core.WindowsAPI;17using FlaUI.Core.Identifiers;18using System.Windows.Automation;19using System.Windows.Forms;20using System.Drawing;21using System.IO;22using System.Windows.Automation.Text;23using System.Windows.Automation.Provider;24{25 {26 static void Main(string[] args)27 {28 var application = Application.Launch(@"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE");29 var mainWindow = application.GetMainWindow(Automation);30 Console.WriteLine("Title: " + mainWindow.Title);31 var process = application.Process;32 Console.WriteLine("Process ID: " + process.Id);33 var automation = application.Automation;34 var automationOfWindow = mainWindow.Automation;35 Console.WriteLine("Name: " + application.Name);36 Console.WriteLine("Name of window: " + mainWindow.Name);37 Console.WriteLine("Name of process: " + process.ProcessName);38 Console.WriteLine("Name of automation: " + automation.Name);39 Console.WriteLine("Name of automation of window: " + automationOfWindow.Name);40 var topLevelWindow = application.GetTopLevelWindows(Automation).FirstOrDefault();41 Console.WriteLine("Top level window title: " + topLevelWindow.Title);42 Console.WriteLine("Top level window name: " + topLevelWindow.Name);
Grid
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.Tools;11using FlaUI.Core.Input;12using FlaUI.Core.WindowsAPI;13using FlaUI.UIA3;14using FlaUI.Core.Conditions;15using System.Diagnostics;16using System.Threading;17{18 {19 static void Main(string[] args)20 {21 var app = Application.Launch(@"C:\Users\Public\Documents\SmartBear\SoapUI-5.3.0\bin\SoapUI-5.3.0.exe");22 Thread.Sleep(5000);23 var window = app.GetMainWindow(Automation);24 var grid = window.FindFirstDescendant(cf => cf.ByAutomationId("testStepTable"));25 var cell = grid.Grid().GetCell(1, 1);26 Console.WriteLine(cell.Value);27 Console.ReadKey();28 }29 }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36using FlaUI.Core;37using FlaUI.Core.AutomationElements;38using FlaUI.Core.AutomationElements.Infrastructure;39using FlaUI.Core.Definitions;40using FlaUI.Core.Tools;41using FlaUI.Core.Input;42using FlaUI.Core.WindowsAPI;43using FlaUI.UIA3;44using FlaUI.Core.Conditions;45using System.Diagnostics;46using System.Threading;47{48 {49 static void Main(string[] args)50 {51 var app = Application.Launch(@"C:\Users\Public\Documents\SmartBear\SoapUI-5.3.0\bin\SoapUI
Grid
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.AutomationElements;7using FlaUI.Core.AutomationElements.Infrastructure;8using FlaUI.Core.Definitions;9using FlaUI.Core.Input;10using FlaUI.Core.Tools;11using FlaUI.UIA3;12using System.Diagnostics;13using System.Threading;14{15 {16 static void Main(string[] args)17 {18 Process.Start(@"C:\Users\Public\Documents\WindowsFormsApplication1.exe");19 Thread.Sleep(1000);20 var automation = new UIA3Automation();21 var mainWindow = automation.GetDesktop().FindFirstChild(cf => cf.ByAutomationId("Form1")).AsWindow();22 var grid = mainWindow.FindFirstChild(cf => cf.ByAutomationId("grid")).AsGrid();23 var gridChildren = grid.GetChildren();24 var header = gridChildren[0];25 var headerChildren = header.GetChildren();26 var headerChildrenChildren = headerChildren[0].GetChildren();27 var headerChildrenChildrenChildren = headerChildrenChildren[0].GetChildren();28 Console.WriteLine(headerChildrenChildrenChildren[0].AsLabel().Text);29 Console.ReadLine();30 }31 }32}
Grid
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.Identifiers;11using FlaUI.Core.Patterns;12using FlaUI.Core.Tools;13using FlaUI.UIA3;14using FlaUI.UIA3.Patterns;15{16 {17 static void Main(string[] args)18 {19 using (var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe"))20 {21 var automation = new UIA3Automation();22 var mainWindow = app.GetMainWindow(automation);23 mainWindow.WaitUntilResponsive();24 var table = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Table)).AsTable();25 var cell = table.FindFirstDescendant(cf => cf.ByControlType(ControlType.Custom)26 .And(cf.ByAutomationId("CalcResults")).And(cf.ByClassName("CalcResults"))).AsCustom();27 var cellRow = cell.Grid.Row;28 var cellColumn = cell.Grid.Column;29 Console.WriteLine("Row: " + cellRow);30 Console.WriteLine("Column: " + cellColumn);31 app.Close();32 }33 }34 }35}
Grid
Using AI Code Generation
1{2 {3 static void Main(string[] args)4 {5 var app = Application.Launch(@"C:\Windows\System32\calc.exe");6 var mainWindow = app.GetMainWindow(Automation);7 var table = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("Table")).AsGrid();8 var cellValue = table.GetCellValue(1, 1);9 Console.WriteLine(cellValue);10 Console.ReadLine();11 }12 }13}14GetCell(int row, int column) – This method is used to get the cell
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!!