Best WinAppDriver code snippet using WinAppDriverUIRecorder.RecordedUiTask.object
MainWindow.xaml.cs
Source:MainWindow.xaml.cs
...47 public double TabCodeHeight48 {49 get { return tabCodeHeight; }50 }51 private void Window_Loaded(object sender, RoutedEventArgs e)52 {53 gridXPath.RowDefinitions[2].Height = new GridLength(TabCodeHeight);54 windowHandle = new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow).Handle;55 System.Windows.Interop.HwndSource.FromHwnd(windowHandle).AddHook(XmlNodePathRecorder.ProcessMessage);56 s_mainWin = this;57 this.comboBoxRecordedUi.ItemsSource = s_listRecordedUi;58 ConstVariables.InitVk2StringMap();59 timer = new System.Timers.Timer(100) { Enabled = true, AutoReset = false };60 timer.Elapsed += InitMouseKeyboard;61 timer.Start();62 }63 private void InitMouseKeyboard(Object source, System.Timers.ElapsedEventArgs e)64 {65 timer.Stop();66 timer.Elapsed -= InitMouseKeyboard;67 timer = null;68 this.Dispatcher.Invoke(new Action(() =>69 {70 MouseKeyboardEventHandler.Init();71 }), System.Windows.Threading.DispatcherPriority.Normal);72 }73 private void Window_Closed(object sender, EventArgs e)74 {75 MouseKeyboardEventHandler.UnInit();76 MouseKeyboardHook.StopHook();77 System.Windows.Interop.HwndSource.FromHwnd(windowHandle).RemoveHook(XmlNodePathRecorder.ProcessMessage);78 s_mainWin = null;79 }80 private void btnRecord_Click(object sender, RoutedEventArgs e)81 {82 // Stop recording83 if (btnRecord.Content.ToString() == strStopRecording)84 {85 btnRecord.Content = "Record";86 this.btnClear.IsEnabled = true;87 }88 else // Start recording89 {90 btnRecord.Content = strStopRecording;91 this.btnClear.IsEnabled = false;92 }93 btnRecord.Focusable = false;94 }95 public static bool IsRecording()96 {97 return s_mainWin.btnRecord.Content.ToString() == strStopRecording;98 }99 public static void UpdateLastUi(RecordedUiTask uiTask)100 {101 s_mainWin.Dispatcher.Invoke(new Action(() =>102 {103 TextRange tr = new TextRange(s_mainWin.rtbXPath.Document.ContentStart, s_mainWin.rtbXPath.Document.ContentEnd);104 tr.Text = uiTask.GetXPath();105 s_mainWin.textBoxXml.Text = uiTask.GetXml();106 s_mainWin.textBoxCode.Text = "";107 }), System.Windows.Threading.DispatcherPriority.ContextIdle);108 }109 public static void AddRecordedUi(RecordedUiTask uiTask)110 {111 if (uiTask.GetTask() != UiTaskName.Inspect)112 {113 MainWindow.s_listRecordedUi.Add(uiTask);114 s_mainWin.btnClear.IsEnabled = s_listRecordedUi.Count > 0;115 s_mainWin.btnWinAppDriverCode.IsEnabled = s_listRecordedUi.Count > 0;116 }117 s_mainWin.Dispatcher.Invoke(new Action(() =>118 {119 if (uiTask.GetTask() != UiTaskName.Inspect)120 {121 s_mainWin.comboBoxRecordedUi.ItemsSource = null;122 s_mainWin.comboBoxRecordedUi.ItemsSource = MainWindow.s_listRecordedUi;123 s_mainWin.comboBoxRecordedUi.SelectedIndex = MainWindow.s_listRecordedUi.Count - 1;124 }125 else126 {127 UpdateLastUi(uiTask);128 }129 }), System.Windows.Threading.DispatcherPriority.ContextIdle);130 }131 private void comboBoxRecordedUi_SelectionChanged(object sender, SelectionChangedEventArgs e)132 {133 var selUiTask = (comboBoxRecordedUi.SelectedItem as RecordedUiTask);134 if (selUiTask != null)135 {136 TextRange tr = new TextRange(rtbXPath.Document.ContentStart, rtbXPath.Document.ContentEnd);137 tr.Text = selUiTask.GetXPath();138 textBoxXml.Text = selUiTask.GetXml();139 textBoxCode.Text = selUiTask.GetCSCode(comboBoxRecordedUi.SelectedIndex, comboBoxRecordedUi.SelectedIndex);140 }141 }142 private void btnClear_Click(object sender, RoutedEventArgs e)143 {144 s_listRecordedUi.Clear();145 this.comboBoxRecordedUi.Items.Refresh();146 TextRange tr = new TextRange(rtbXPath.Document.ContentStart, rtbXPath.Document.ContentEnd);147 tr.Text = "";148 textBoxXml.Text = "";149 textBoxCode.Text = "";150 btnClear.IsEnabled = s_listRecordedUi.Count > 0;151 btnWinAppDriverCode.IsEnabled = s_listRecordedUi.Count > 0;152 }153 private void btnWinAppDriverCode_Click(object sender, RoutedEventArgs e)154 {155 // Stop recording156 if (btnRecord.Content.ToString() == strStopRecording)157 {158 btnRecord_Click(null, null);159 }160 StringBuilder sb = new StringBuilder();161 int nOrder = 0;162 int nOrderFocused = 0;163 foreach (var uiTask in s_listRecordedUi)164 {165 nOrder++;166 if (uiTask.GetTask() == UiTaskName.LeftClick)167 {168 nOrderFocused = nOrder;169 }170 sb.AppendLine(uiTask.GetCSCode(nOrder, nOrderFocused));171 }172 if (sb.Length > 0)173 {174 Clipboard.SetText(sb.ToString());175 this.Dispatcher.Invoke(new Action(() =>176 {177 this.toolTipText.Text = "WinAppDriver client code copied to clipboard";178 ((ToolTip)btnWinAppDriverCode.ToolTip).IsOpen = true;179 timer = new System.Timers.Timer(2000) { Enabled = true, AutoReset = false };180 timer.Elapsed += ResetToolTip;181 timer.Start();182 }), System.Windows.Threading.DispatcherPriority.ContextIdle);183 }184 }185 private void ResetToolTip(Object source, System.Timers.ElapsedEventArgs e)186 {187 timer.Stop();188 timer.Elapsed -= ResetToolTip;189 timer = null;190 this.Dispatcher.Invoke(new Action(() =>191 {192 ((ToolTip)btnWinAppDriverCode.ToolTip).IsOpen = false;193 toolTipText.Text = "Generate and copy C# code to Clipboard";194 }), System.Windows.Threading.DispatcherPriority.ContextIdle);195 }196 private void HideTabXmlCode(bool bHide)197 {198 if (bHide == true)199 {200 if (gridXPath.RowDefinitions[2].ActualHeight < TabCodeHeight + 1)201 {202 bHide = false;203 }204 }205 if (bHide == false)206 {207 gridXPath.RowDefinitions[2].Height = new GridLength(gridXPath.ActualHeight * 0.667);208 }209 else210 {211 gridXPath.RowDefinitions[2].Height = new GridLength(TabCodeHeight);212 }213 }214 private void TabItemNodes_Click(object sender, RoutedEventArgs e)215 {216 HideTabXmlCode(this.tabControl.SelectedIndex == 0);217 }218 private void TabItemCode_Click(object sender, RoutedEventArgs e)219 {220 HideTabXmlCode(this.tabControl.SelectedIndex == 1);221 }222 private void MenuCopy(object sender, RoutedEventArgs e)223 {224 var tr = new TextRange(rtbXPath.Document.ContentStart, rtbXPath.Document.ContentEnd);225 if (tr != null)226 {227 Clipboard.SetText(tr.Text);228 }229 }230 string StringFromRichTextBox(RichTextBox rtb)231 {232 TextRange tr = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);233 return tr.Text;234 }235 private void btnClear_Loaded(object sender, RoutedEventArgs e)236 {237 btnClear.IsEnabled = s_listRecordedUi.Count > 0;238 }239 private void btnWinAppDriverCode_Loaded(object sender, RoutedEventArgs e)240 {241 btnWinAppDriverCode.IsEnabled = s_listRecordedUi.Count > 0;242 }243 }244}...
UiTreeNode.cs
Source:UiTreeNode.cs
1//******************************************************************************2//3// Copyright (c) 2018 Microsoft Corporation. All rights reserved.4//5// This code is licensed under the MIT License (MIT).6//7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN13// THE SOFTWARE.14//15//******************************************************************************16using System.Collections.Generic;17using System.Collections.ObjectModel;18using System.Linq;19namespace WinAppDriverUIRecorder20{21 public class PropNameValue22 {23 public PropNameValue(string name, string value)24 {25 PropName = name;26 PropValue = value;27 }28 public string PropName { set; get; }29 public string PropValue { set; get; }30 }31 public class UiTreeNode32 {33 public static List<UiTreeNode> s_uiTreeNodes = new List<UiTreeNode>();34 public enum CompareMethod { Equal, StartsWith, Contains };35 public UiTreeNode(UiTreeNode pNode)36 {37 Parent = pNode;38 this.Items = new List<UiTreeNode>();39 }40 public UiTreeNode Parent { get; set; }41 public string Tag { get; set; }42 public string Title { get; set; }43 public string ClassName { get; set; }44 public string Name { get; set; }45 public string AutomationId { get; set; }46 public string Position { get; set; }47 public string Left { get; set; }48 public string Top { get; set; }49 public string Width { get; set; }50 public string Height { get; set; }51 public string RuntimeId { get; set; }52 public RecordedUiTask UiTask { get; set; }53 public List<UiTreeNode> Items { get; set; }54 public CompareMethod NameCompareMethod { get; set; }55 public CompareMethod ClassNameCompareMethod { get; set; }56 public CompareMethod AutomationIdCompareMethod { get; set; }57 public string NodePath { get; set; }58 public ObservableCollection<PropNameValue> LoadPropertyData()59 {60 var listPropValue = new ObservableCollection<PropNameValue>();61 listPropValue.Add(new PropNameValue("Tag", Tag));62 listPropValue.Add(new PropNameValue("ClassName", ClassName));63 listPropValue.Add(new PropNameValue("Name", Name));64 listPropValue.Add(new PropNameValue("AutomationId", AutomationId));65 listPropValue.Add(new PropNameValue("Position", (Position ?? "").ToString()));66 listPropValue.Add(new PropNameValue("Left", (Left ?? "").ToString()));67 listPropValue.Add(new PropNameValue("Top", (Top ?? "").ToString()));68 listPropValue.Add(new PropNameValue("Width", (Width ?? "").ToString()));69 listPropValue.Add(new PropNameValue("Height", (Height ?? "").ToString()));70 listPropValue.Add(new PropNameValue("RuntimeId", RuntimeId));71 listPropValue.Add(new PropNameValue("UiTask", UiTask != null ? UiTask.ToString() : ""));72 return listPropValue;73 }74 public static bool CompareRuntimeId(UiTreeNode nodeLeft, UiTreeNode nodeRight)75 {76 if (nodeLeft == null || nodeRight == null)77 {78 return false;79 }80 else81 {82 return nodeLeft.RuntimeId == nodeRight.RuntimeId;83 }84 }85 static UiTreeNode FindInCachedUiTree(UiTreeNode node, List<UiTreeNode> cachedNode)86 {87 foreach (var snode in cachedNode)88 {89 if (CompareRuntimeId(node, snode))90 {91 return snode;92 }93 }94 return null;95 }96 public static void AddToUiTree(RecordedUiTask recordedUi)97 {98 if (recordedUi.GetUiTreeNode() == null)99 {100 if (string.IsNullOrEmpty(recordedUi.GetXPath(false)))101 {102 return;103 }104 }105 var uiNode = recordedUi.GetUiTreeNode();106 if (uiNode == null || uiNode.Items.Count == 0)107 {108 //Log error109 return;110 }111 // Empty root112 if (s_uiTreeNodes.Count == 0)113 {114 s_uiTreeNodes.Add(uiNode);115 return;116 }117 // Add uiNode to s_uiNode118 UiTreeNode node = uiNode;119 UiTreeNode nodeCached = null;120 List<UiTreeNode> listCached = s_uiTreeNodes;121 while (node != null)122 {123 var nodeAddTo = FindInCachedUiTree(node, listCached);124 if (node.Items.Count > 0 && nodeAddTo != null)125 {126 node = node.Items.First();127 listCached = nodeAddTo.Items;128 nodeCached = nodeAddTo;129 }130 else131 {132 if (nodeCached != null)133 {134 node.Parent = nodeCached;135 nodeCached.Items.Add(node);136 }137 break;138 }139 }140 }141 static UiTreeNode FindUiTaskNode(UiTreeNode node, RecordedUiTask uiTaskNode)142 {143 if (node.UiTask == uiTaskNode)144 {145 return node;146 }147 for (int i = 0; i < node.Items.Count; i++)148 {149 var ret = FindUiTaskNode(node.Items[i], uiTaskNode);150 if (ret != null)151 {152 return ret;153 }154 }155 return null;156 }157 public static void RemoveUiTreeNode(RecordedUiTask uiTaskNode)158 {159 if (s_uiTreeNodes.Count == 0)160 {161 return;162 }163 UiTreeNode uiNode = FindUiTaskNode(s_uiTreeNodes.First(), uiTaskNode);164 if (uiNode == null)165 {166 s_uiTreeNodes.Clear();167 return;168 }169 UiTreeNode uiParent = uiNode.Parent;170 if (uiParent == null)171 {172 s_uiTreeNodes.Clear();173 return;174 }175 while (uiParent != null)176 {177 uiNode.UiTask = null;178 if (uiNode.Items.Count == 0)179 {180 uiParent.Items.Remove(uiNode);181 }182 if (uiParent.Items.Count > 0)183 {184 break;185 }186 else187 {188 uiNode = uiParent;189 uiParent = uiNode.Parent;190 }191 }192 if (uiParent == null && (s_uiTreeNodes.Count == 1 && s_uiTreeNodes.First().Items.Count == 0))193 {194 s_uiTreeNodes.Clear();195 }196 }197 }198}...
object
Using AI Code Generation
1var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();2recordedUiTask.ObjectMethod();3var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();4recordedUiTask.ObjectMethod();5var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();6recordedUiTask.ObjectMethod();7var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();8recordedUiTask.ObjectMethod();9var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();10recordedUiTask.ObjectMethod();11var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();12recordedUiTask.ObjectMethod();13var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();14recordedUiTask.ObjectMethod();15var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();16recordedUiTask.ObjectMethod();17var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();18recordedUiTask.ObjectMethod();19var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();20recordedUiTask.ObjectMethod();21var recordedUiTask = new WinAppDriverUIRecorder.RecordedUiTask();
object
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7using WinAppDriverUIRecorder.Recorder;8using WinAppDriverUIRecorder.Recorder.RecordedTasks;9using WinAppDriverUIRecorder.Recorder.RecordedTasks.RecordedUiTasks;10using WinAppDriverUIRecorder.Recorder.RecordedTasks.RecordedUiTasks.UiTaskParameters;11using WinAppDriverUIRecorder.Recorder.RecordedTasks.RecordedUiTasks.UiTaskParameters.UiTaskParameterTypes;12{13 {14 public RecordedUiTask()15 {16 this.UiTaskParameters = new List<IUiTaskParameter>();17 }18 public string TaskName { get; set; }19 public List<IUiTaskParameter> UiTaskParameters { get; set; }20 public override string ToString()21 {22 var sb = new StringBuilder();23 sb.Append(this.TaskName);24 sb.Append("(");25 foreach (var parameter in this.UiTaskParameters)26 {27 sb.Append(parameter.ToString());28 sb.Append(", ");29 }30 sb.Remove(sb.Length - 2, 2);31 sb.Append(")");32 return sb.ToString();33 }34 }35}36using System;37using System.Collections.Generic;38using System.Linq;39using System.Text;40using System.Threading.Tasks;41using WinAppDriverUIRecorder;42using WinAppDriverUIRecorder.Recorder;43using WinAppDriverUIRecorder.Recorder.RecordedTasks;44using WinAppDriverUIRecorder.Recorder.RecordedTasks.RecordedUiTasks;45using WinAppDriverUIRecorder.Recorder.RecordedTasks.RecordedUiTasks.UiTaskParameters;46using WinAppDriverUIRecorder.Recorder.RecordedTasks.RecordedUiTasks.UiTaskParameters.UiTaskParameterTypes;47{48 {49 public RecordedUiTask()50 {51 this.UiTaskParameters = new List<IUiTaskParameter>();52 }53 public string TaskName { get; set; }54 public List<IUiTaskParameter> UiTaskParameters { get; set; }55 public override string ToString()56 {57 var sb = new StringBuilder();58 sb.Append(this.TaskName);59 sb.Append("(");60 foreach (var parameter in this.UiTask
object
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7using System.Windows.Automation;8using System.Threading;9{10 {11 static void Main(string[] args)12 {13 RecordedUiTask recordedTask = new RecordedUiTask();14 recordedTask.StartRecording();15 Thread.Sleep(5000);16 recordedTask.StopRecording();17 Console.WriteLine(recordedTask.GetRecordedTask());18 Console.Read();19 }20 }21}22using System;23using System.Collections.Generic;24using System.Linq;25using System.Text;26using System.Threading.Tasks;27using WinAppDriverUIRecorder;28using System.Windows.Automation;29using System.Threading;30{31 {32 static void Main(string[] args)33 {34 RecordedUiTask.StartRecording();35 Thread.Sleep(5000);36 RecordedUiTask.StopRecording();37 Console.WriteLine(RecordedUiTask.GetRecordedTask());38 Console.Read();39 }40 }41}42using System;43using System.Collections.Generic;44using System.Linq;45using System.Text;46using System.Threading.Tasks;47using WinAppDriverUIRecorder;48using System.Windows.Automation;49using System.Threading;50{51 {52 static void Main(string[] args)53 {54 RecordedUiTask.StartRecording();55 Thread.Sleep(5000);56 RecordedUiTask.StopRecording();57 Console.WriteLine(RecordedUiTask.GetRecordedTask());58 Console.Read();59 }60 }61}
object
Using AI Code Generation
1var task = new WinAppDriverUIRecorder.RecordedUiTask();2task.AddAction("LaunchApp", "C:\\Windows\\System32\\calc.exe", "Calculator", null);3task.AddAction("Click", "Calculator", "Calculator", "CalculatorResults");4task.AddAction("Click", "CalculatorResults", "CalculatorResults", "Button1");5task.AddAction("Click", "Button1", "Button1", "Button2");6task.AddAction("Click", "Button2", "Button2", "Button3");7task.AddAction("Click", "Button3", "Button3", "Button4");8task.AddAction("Click", "Button4", "Button4", "Button5");9task.AddAction("Click", "Button5", "Button5", "Button6");10task.AddAction("Click", "Button6", "Button6", "Button7");11task.AddAction("Click", "Button7", "Button7", "Button8");12task.AddAction("Click", "Button8", "Button8", "Button9");13task.AddAction("Click", "Button9", "Button9", "Button0");14task.AddAction("Click", "Button0", "Button0", "ButtonDecimal");15task.AddAction("Click", "ButtonDecimal", "ButtonDecimal", "ButtonPlus");16task.AddAction("Click", "ButtonPlus", "ButtonPlus", "ButtonMinus");17task.AddAction("Click", "ButtonMinus", "ButtonMinus", "ButtonMultiply");18task.AddAction("Click", "ButtonMultiply", "ButtonMultiply", "ButtonDivide");19task.AddAction("Click", "ButtonDivide", "ButtonDivide", "ButtonEquals");20task.AddAction("Click", "ButtonEquals", "ButtonEquals", "ButtonClear");21task.AddAction("Click", "ButtonClear", "ButtonClear", "ButtonClearEntry");22task.AddAction("Click", "ButtonClearEntry", "ButtonClearEntry", "ButtonBackspace");23task.AddAction("Click", "ButtonBackspace", "ButtonBackspace", "ButtonMemoryClear");24task.AddAction("Click", "ButtonMemoryClear", "ButtonMemoryClear", "ButtonMemoryRecall");25task.AddAction("Click", "ButtonMemoryRecall", "ButtonMemoryRecall", "ButtonMemoryStore");26task.AddAction("Click", "ButtonMemoryStore", "ButtonMemoryStore", "ButtonMemoryAdd");27task.AddAction("Click", "ButtonMemory
object
Using AI Code Generation
1RecordedUiTask recordedUiTask = new RecordedUiTask();2recordedUiTask.RunTask();3RecordedUiTask recordedUiTask = new RecordedUiTask();4recordedUiTask.RunTask();5RecordedUiTask recordedUiTask = new RecordedUiTask();6recordedUiTask.RunTask();7RecordedUiTask recordedUiTask = new RecordedUiTask();8recordedUiTask.RunTask();9RecordedUiTask recordedUiTask = new RecordedUiTask();10recordedUiTask.RunTask();11RecordedUiTask recordedUiTask = new RecordedUiTask();12recordedUiTask.RunTask();13RecordedUiTask recordedUiTask = new RecordedUiTask();14recordedUiTask.RunTask();
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!!