Best FlaUI code snippet using FlaUI.Core.UITests.Win10Calc.Win10Calc
CalculatorTests.cs
Source:CalculatorTests.cs
...20 [Test]21 public void CalculatorTest()22 {23 var window = App.GetMainWindow(Automation);24 var calc = OperatingSystem.IsWindows10() ? (ICalculator)new Win10Calc(window) : new LegacyCalc(window);2526 // Switch to default mode27 System.Threading.Thread.Sleep(1000);28 Keyboard.TypeSimultaneously(VirtualKeyShort.ALT, VirtualKeyShort.KEY_1);29 Wait.UntilInputIsProcessed();30 App.WaitWhileBusy();31 System.Threading.Thread.Sleep(1000);3233 // Simple addition34 calc.Button1.Click();35 calc.Button2.Click();36 calc.Button3.Click();37 calc.Button4.Click();38 calc.ButtonAdd.Click();39 calc.Button5.Click();40 calc.Button6.Click();41 calc.Button7.Click();42 calc.Button8.Click();43 calc.ButtonEquals.Click();44 App.WaitWhileBusy();45 var result = calc.Result;46 Assert.That(result, Is.EqualTo("6912"));4748 // Date comparison49 using (Keyboard.Pressing(VirtualKeyShort.CONTROL))50 {51 Keyboard.Type(VirtualKeyShort.KEY_E);52 }53 }5455 protected override Application StartApplication()56 {57 if (OperatingSystem.IsWindows10())58 {59 // Use the store application on those systems60 return Application.LaunchStoreApp("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");61 }62 if (OperatingSystem.IsWindowsServer2016())63 {64 // The calc.exe on this system is just a stub which launches win32calc.exe65 return Application.Launch("win32calc.exe");66 }67 return Application.Launch("calc.exe");68 }69 }7071 public interface ICalculator72 {73 Button Button1 { get; }74 Button Button2 { get; }75 Button Button3 { get; }76 Button Button4 { get; }77 Button Button5 { get; }78 Button Button6 { get; }79 Button Button7 { get; }80 Button Button8 { get; }81 Button ButtonAdd { get; }82 Button ButtonEquals { get; }83 string Result { get; }84 }8586 public class LegacyCalc : ICalculator87 {88 private readonly AutomationElement _mainWindow;8990 public Button Button1 => FindElement("1").AsButton();91 public Button Button2 => FindElement("2").AsButton();92 public Button Button3 => FindElement("3").AsButton();93 public Button Button4 => FindElement("4").AsButton();94 public Button Button5 => FindElement("5").AsButton();95 public Button Button6 => FindElement("6").AsButton();96 public Button Button7 => FindElement("7").AsButton();97 public Button Button8 => FindElement("8").AsButton();98 public Button ButtonAdd => FindElement("Add").AsButton();99 public Button ButtonEquals => FindElement("Equals").AsButton();100101 public string Result102 {103 get104 {105 var resultElement = _mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("158"));106 var value = resultElement.Properties.Name;107 return Regex.Replace(value, "[^0-9]", String.Empty);108 }109 }110111 public LegacyCalc(AutomationElement mainWindow)112 {113 _mainWindow = mainWindow;114 }115116 private AutomationElement FindElement(string text)117 {118 var element = _mainWindow.FindFirstDescendant(cf => cf.ByText(text));119 return element;120 }121 }122123 public class Win10Calc : ICalculator124 {125 private readonly AutomationElement _mainWindow;126127 public Button Button1 => FindElement("num1Button").AsButton();128 public Button Button2 => FindElement("num2Button").AsButton();129 public Button Button3 => FindElement("num3Button").AsButton();130 public Button Button4 => FindElement("num4Button").AsButton();131 public Button Button5 => FindElement("num5Button").AsButton();132 public Button Button6 => FindElement("num6Button").AsButton();133 public Button Button7 => FindElement("num7Button").AsButton();134 public Button Button8 => FindElement("num8Button").AsButton();135 public Button ButtonAdd => FindElement("plusButton").AsButton();136 public Button ButtonEquals => FindElement("equalButton").AsButton();137138 public string Result139 {140 get141 {142 var resultElement = FindElement("CalculatorResults");143 var value = resultElement.Properties.Name;144 return Regex.Replace(value, "[^0-9]", String.Empty);145 }146 }147148 public Win10Calc(AutomationElement mainWindow)149 {150 _mainWindow = mainWindow;151 }152153 private AutomationElement FindElement(string text)154 {155 var element = _mainWindow.FindFirstDescendant(cf => cf.ByAutomationId(text));156 return element;157 }158 }159}
...
Win10Calc
Using AI Code Generation
1using System;2using System.Threading;3using FlaUI.Core;4using FlaUI.Core.AutomationElements;5using FlaUI.Core.Definitions;6using FlaUI.Core.Input;7using FlaUI.Core.Tools;8using FlaUI.UIA3;9using FlaUI.Core.UITests.Win10Calc;10using FlaUI.Core.UITests.Common;11using System.Windows.Forms;12{13 {14 static void Main(string[] args)15 {16 var app = Application.Launch("C:\\Windows\\System32\\calc.exe");17 var window = app.GetMainWindow(new UIA3PropertyLibrary());18 var buttons = window.FindAllChildren();19 var button1 = buttons[0];20 var button1Val = button1.Properties.Name.Value;21 var button2 = buttons[1];22 var button2Val = button2.Properties.Name.Value;23 var button3 = buttons[2];24 var button3Val = button3.Properties.Name.Value;25 var button4 = buttons[3];26 var button4Val = button4.Properties.Name.Value;27 var button5 = buttons[4];28 var button5Val = button5.Properties.Name.Value;29 var button6 = buttons[5];30 var button6Val = button6.Properties.Name.Value;31 var button7 = buttons[6];32 var button7Val = button7.Properties.Name.Value;33 var button8 = buttons[7];34 var button8Val = button8.Properties.Name.Value;35 var button9 = buttons[8];36 var button9Val = button9.Properties.Name.Value;37 var button0 = buttons[10];38 var button0Val = button0.Properties.Name.Value;39 var buttonPlus = buttons[11];
Win10Calc
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.Core.UITests;3using FlaUI.Core.UITests.TestFramework;4using FlaUI.Core.WindowsAPI;5using NUnit.Framework;6{7 {8 private Win10Calc _win10Calc;9 public void Setup()10 {11 _win10Calc = new Win10Calc();12 }13 public void TearDown()14 {15 _win10Calc.Dispose();16 }17 public void AddTwoNumbers()18 {19 var result = _win10Calc.Add(2, 3);20 Assert.That(result, Is.EqualTo(5));21 }22 }23}24using FlaUI.Core;25using FlaUI.Core.UITests;26using FlaUI.Core.UITests.TestFramework;27using FlaUI.Core.WindowsAPI;28using NUnit.Framework;29{30 {31 private Win10Calc _win10Calc;32 public void Setup()33 {34 _win10Calc = new Win10Calc();35 }36 public void TearDown()37 {38 _win10Calc.Dispose();39 }40 public void AddTwoNumbers()41 {42 var result = _win10Calc.Add(2, 3);43 Assert.That(result, Is.EqualTo(5));44 }45 }46}47using FlaUI.Core;48using FlaUI.Core.UITests;49using FlaUI.Core.UITests.TestFramework;50using FlaUI.Core.WindowsAPI;51using NUnit.Framework;52{53 {54 private Win10Calc _win10Calc;55 public void Setup()56 {57 _win10Calc = new Win10Calc();58 }59 public void TearDown()60 {61 _win10Calc.Dispose();62 }
Win10Calc
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.Core.UITests;3using FlaUI.UIA3;4using System;5using System.Diagnostics;6using System.Threading;7using System.Windows.Automation;8{9 {10 private readonly Application _application;11 private readonly Window _window;12 public Win10Calc()13 {14 var app = Process.Start(new ProcessStartInfo("calc.exe"));15 _application = Application.Attach(app);16 _window = _application.GetMainWindow(AutomationObjectIds.Window);17 }18 public void DoStuff()19 {20 var button = _window.FindFirstDescendant(cf => cf.ByAutomationId("num1Button")).AsButton();21 button.Click();22 button.Click();23 button.Click();24 button.Click();25 button.Click();26 button.Click();27 button.Click();28 button.Click();29 button.Click();30 button.Click();31 var addButton = _window.FindFirstDescendant(cf => cf.ByAutomationId("plusButton")).AsButton();32 addButton.Click();33 button.Click();34 button.Click();35 button.Click();36 button.Click();
Win10Calc
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;14using NUnit.Framework;15{16 {17 private Application _calcApp;18 private Window _mainWindow;19 public void Setup()20 {21 _calcApp = Application.Launch("C:\\Windows\\System32\\calc.exe");22 _mainWindow = _calcApp.GetMainWindow(Automation);23 }24 public void TearDown()25 {26 _calcApp.Close();27 }28 public void Win10Calc()29 {30 var buttons = _mainWindow.FindAllDescendants(x => x.ByControlType(ControlType.Button)).ToList();31 buttons[0].AsButton().Invoke();32 buttons[11].AsButton().Invoke();33 buttons[1].AsButton().Invoke();34 buttons[18].AsButton().Invoke();35 var result = _mainWindow.FindFirstDescendant(x => x.ByAutomationId("150")).AsTextBox().Text;36 Assert.AreEqual("3", result);37 }38 public static UIA3Automation Automation { get; } = new UIA3Automation();39 }40}41using System;42using System.Collections.Generic;43using System.Linq;44using System.Text;45using System.Threading.Tasks;46using FlaUI.Core;47using FlaUI.Core.AutomationElements;48using FlaUI.Core.AutomationElements.Infrastructure;49using FlaUI.Core.Definitions;50using FlaUI.Core.Input;51using FlaUI.Core.Tools;
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!!