Best WinAppDriver code snippet using WinAppDriverUIRecorder.GenerateCSCode.GetDecodedKeyboardInput
RecordedUiTask.cs
Source:RecordedUiTask.cs  
...129                    if (this._strDescription == null)130                    {131                        if (this.UiTaskName == EnumUiTaskName.KeyboardInput)132                        {133                            var keyboardTaskDescription = GenerateCSCode.GetDecodedKeyboardInput(this.Base64Text, this.CapsLock, this.NumLock, this.ScrollLock);134                            StringBuilder sb = new StringBuilder();135                            foreach (var strLine in keyboardTaskDescription)136                            {137                                sb.Append(strLine);138                            }139                            this._strDescription = $"{this.UiTaskName} VirtualKeys=\"{sb.ToString()}\" CapsLock={this.CapsLock} NumLock={this.NumLock} ScrollLock={this.ScrollLock}";140                        }141                        else142                        {143                            // set LeftLocal and TopLocal from _pathNodes.Last()144                            if (this.UiTaskName == EnumUiTaskName.Drag || this.UiTaskName == EnumUiTaskName.MouseWheel)145                            {146                                this._strDescription = $"{this.UiTaskName} on {Tag} \"{this.Name}\" at ({this.LeftLocal},{this.TopLocal}) drag ({this.DeltaX},{this.DeltaY})";147                            }148                            else149                            {150                                this._strDescription = $"{this.UiTaskName} on {Tag} \"{this.Name}\" at ({this.LeftLocal},{this.TopLocal})";151                            }152                        }153                    }154                }155                catch (Exception ex)156                {157                    AppInsights.LogException("Description", ex.Message);158                }159                if (string.IsNullOrEmpty(this._strDescription))160                {161                    this._strDescription = string.Empty;162                }163                return this._strDescription;164            }165            //set is not defined166        }167        public override string ToString()168        {169            return Description;170        }171        public void AppendKeyboardInput(string textToAppend)172        {173            byte[] data1 = Convert.FromBase64String(this.Base64Text);174            byte[] data2 = Convert.FromBase64String(textToAppend);175            byte[] data = new byte[data1.Length + data2.Length];176            data1.CopyTo(data, 0);177            data2.CopyTo(data, data1.Length);178            this.Base64Text = Convert.ToBase64String(data);179            var keyboardTaskDescription = GenerateCSCode.GetDecodedKeyboardInput(this.Base64Text, this.CapsLock, this.NumLock, this.ScrollLock);180            StringBuilder sb = new StringBuilder();181            foreach (var strLine in keyboardTaskDescription)182            {183                sb.Append(strLine);184            }185            this._strDescription = $"{this.UiTaskName} VirtualKeys=\"{sb.ToString()}\" CapsLock={this.CapsLock} NumLock={this.NumLock} ScrollLock={this.ScrollLock}";186        }187        public string GetCSCode(string focusedElemName)188        {189            StringBuilder sb = new StringBuilder();190            sb.AppendLine("// " + this.Description);191            string consoleWriteLine = "Console.WriteLine(\"" + this.Description.Replace("\"", "\\\"") + "\");";192            sb.AppendLine(consoleWriteLine);193            if (this.UiTaskName == EnumUiTaskName.LeftClick)194            {195                sb.AppendLine(GenerateCSCode.LeftClick(this, VariableName));196            }197            else if (this.UiTaskName == EnumUiTaskName.RightClick)198            {199                sb.AppendLine(GenerateCSCode.RightClick(this, VariableName));200            }201            else if (this.UiTaskName == EnumUiTaskName.LeftDblClick)202            {203                sb.AppendLine(GenerateCSCode.DoubleClick(this, VariableName));204            }205            else if (this.UiTaskName == EnumUiTaskName.MouseWheel)206            {207                sb.AppendLine(GenerateCSCode.Wheel(this, VariableName));208            }209            else if (this.UiTaskName == EnumUiTaskName.KeyboardInput)210            {211                sb.AppendLine(GenerateCSCode.SendKeys(this, focusedElemName));212            }213            return sb.ToString();214        }215        public void ChangeClickToDoubleClick()216        {217            this.UiTaskName = EnumUiTaskName.LeftDblClick;218            this._strDescription = null;219        }220        public void DragComplete(int deltaX, int deltaY)221        {222            this.DeltaX = deltaX;223            this.DeltaY = deltaY;224            this.UiTaskName = EnumUiTaskName.Drag;225            this._strDescription = null;226        }227        public void UpdateWheelData(int delta)228        {229            this.DeltaX += 1;230            this.DeltaY += delta;231            this._strDescription = null;232        }233    }234    class GenerateCSCode235    {236        public static string GetCodeBlock(RecordedUiTask uiTask, string elemName, string uiActionLine)237        {238            var xpath = "xpath_" + elemName;239            elemName = "winElem_" + elemName;240            string codeBlock = $"string {xpath} = {uiTask.GetXPath(true)};\n" +241                $"var {elemName} = desktopSession.FindElementByAbsoluteXPath({xpath});\n" +242                $"if ({elemName} != null)\n" +243                "{\n" +244                "CODEBLOCK" +245                "}\n" +246                "else\n" +247                "{\n" +248                "    Console.WriteLine($\"Failed to find element using xpath: {" + $"{xpath}" + "}\");\n" +249                "    return;\n" +250                "}\n";251            return codeBlock.Replace("CODEBLOCK", uiActionLine);252        }253        public static string LeftClick(RecordedUiTask uiTask, string elemName)254        {255            string codeLine = $"    winElem_{elemName}.Click();\n";256            return GetCodeBlock(uiTask, elemName, codeLine);257        }258        public static string DoubleClick(RecordedUiTask uiTask, string elemName)259        {260            string codeLine = $"    desktopSession.DesktopSessionElement.Mouse.MouseMove(winElem_{elemName}.Coordinates);\n" +261                            $"    desktopSession.DesktopSessionElement.Mouse.DoubleClick(null);\n";262            return GetCodeBlock(uiTask, elemName, codeLine);263        }264        public static string RightClick(RecordedUiTask uiTask, string elemName)265        {266            string codeLine = $"    desktopSession.DesktopSessionElement.Mouse.MouseMove(winElem_{elemName}.Coordinates);\n" +267                            $"    desktopSession.DesktopSessionElement.Mouse.ContextClick(null);\n";268            return GetCodeBlock(uiTask, elemName, codeLine);269        }270        public static string Wheel(RecordedUiTask uiTask, string elemName)271        {272            string codeLine = $"   //TODO: Wheel at ({uiTask.Left},{uiTask.Top}) on winElem_{elemName}, Count:{uiTask.DeltaX}, Total Amount:{uiTask.DeltaY}\n";273            return GetCodeBlock(uiTask, elemName, codeLine);274        }275        public static List<string> GetDecodedKeyboardInput(string strBase64, bool bCapsLock, bool bNumLock, bool bScrollLock)276        {277            byte[] data = Convert.FromBase64String(strBase64);278            int i = 0;279            bool shift = false;280            StringBuilder sb = new StringBuilder();281            List<string> lines = new List<string>();282            int nCtrlKeyDown = 0;283            while (i < data.Length / 2)284            {285                int n1 = i * 2;286                int n2 = i * 2 + 1;287                i++;288                bool bIsKeyDown = data[n1] == 0;289                VirtualKeys vk = (VirtualKeys)data[n2];290                char ch = ConstVariables.Vk2char((int)vk, shift || bCapsLock);291                if (bIsKeyDown) //Keydown292                {293                    if (char.IsControl(ch))294                    {295                        nCtrlKeyDown++;296                        if (nCtrlKeyDown == 1 && sb.Length > 0)297                        {298                            lines.Add("\"" + sb.ToString() + "\"");299                            sb.Clear();300                        }301                        string vkStr = vk.ToString();302                        string vkSendKey = ConstVariables.Vk2string(vkStr);303                        if (nCtrlKeyDown == 1)304                            sb.Append("Keys." + vkSendKey);305                        else306                            sb.Append(" + Keys." + vkSendKey);307                    }308                    else if (ch != 0)309                    {310                        string strToAppend = ch.ToString();311                        if (ch == '\\')312                        {313                            strToAppend += "\\";314                        }315                        if (nCtrlKeyDown > 0)316                        {317                            sb.Append(" + \"" + strToAppend + "\"");318                        }319                        else320                        {321                            sb.Append(strToAppend);322                        }323                    }324                }325                else //Keyup326                {327                    if (char.IsControl(ch))328                    {329                        nCtrlKeyDown--;330                        string vkStr = vk.ToString();331                        string vkSendKey = ConstVariables.Vk2string(vkStr);332                        if (nCtrlKeyDown == 0)333                        {334                            lines.Add(sb.ToString() + " + Keys." + vkSendKey);335                            sb.Clear();336                        }337                        else338                        {339                            sb.Append(" + Keys." + vkSendKey);340                        }341                    }342                }343            }344            if (sb.Length > 0)345            {346                lines.Add("\"" + sb.ToString() + "\"");347            }348            return lines;349        }350        public static string SendKeys(RecordedUiTask uiTask, string focusedElemeName)351        {352            List<string> lines = GetDecodedKeyboardInput(uiTask.Base64Text, uiTask.CapsLock, uiTask.NumLock, uiTask.ScrollLock);353            StringBuilder sb = new StringBuilder();354            focusedElemeName = "winElem_" + focusedElemeName;355            sb.AppendLine($"System.Threading.Thread.Sleep(100);");356            foreach (string line in lines)357            {358                sb.AppendLine($"{focusedElemeName}.SendKeys({line});");359            }360            return sb.ToString();361        }362    }363}...GetDecodedKeyboardInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7{8    {9        static void Main(string[] args)10        {11            string str = "2";12            string str1 = WinAppDriverUIRecorder.GenerateCSCode.GetDecodedKeyboardInput(str);13            Console.WriteLine("Decoded Keyboard Input is:" + str1);14            Console.ReadLine();15        }16    }17}18using System;19using System.Collections.Generic;20using System.Linq;21using System.Text;22using System.Threading.Tasks;23using WinAppDriverUIRecorder;24{25    {26        static void Main(string[] args)27        {28            string str = "3";29            string str1 = WinAppDriverUIRecorder.GenerateCSCode.GetDecodedKeyboardInput(str);30            Console.WriteLine("Decoded Keyboard Input is:" + str1);31            Console.ReadLine();32        }33    }34}35using System;36using System.Collections.Generic;37using System.Linq;38using System.Text;39using System.Threading.Tasks;40using WinAppDriverUIRecorder;41{42    {43        static void Main(string[] args)44        {45            string str = "4";46            string str1 = WinAppDriverUIRecorder.GenerateCSCode.GetDecodedKeyboardInput(str);47            Console.WriteLine("Decoded Keyboard Input is:" + str1);48            Console.ReadLine();49        }50    }51}52using System;53using System.Collections.Generic;54using System.Linq;55using System.Text;56using System.Threading.Tasks;57using WinAppDriverUIRecorder;58{59    {60        static void Main(string[] args)61        {62            string str = "5";63            string str1 = WinAppDriverUIRecorder.GenerateCSCode.GetDecodedKeyboardInput(strGetDecodedKeyboardInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7{8    {9        static void Main(string[] args)10        {11            string decodedString = GenerateCSCode.GetDecodedKeyboardInput("PAUSE", "PAUSE");12            Console.WriteLine(decodedString);13        }14    }15}16System.Threading.Thread.Sleep(2000);17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22using WinAppDriverUIRecorder;23{24    {25        static void Main(string[] args)26        {27            string decodedString = GenerateCSCode.GetDecodedKeyboardInput("F1", "F1");28            Console.WriteLine(decodedString);29        }30    }31}32Driver.Keyboard.SendKeys(Keys.F1);33using System;34using System.Collections.Generic;35using System.Linq;36using System.Text;37using System.Threading.Tasks;38using WinAppDriverUIRecorder;39{40    {41        static void Main(string[] args)42        {43            string decodedString = GenerateCSCode.GetDecodedKeyboardInput("F2", "F2");44            Console.WriteLine(decodedString);45        }46    }47}48Driver.Keyboard.SendKeys(Keys.F2);49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Threading.Tasks;54using WinAppDriverUIRecorder;55{56    {57        static void Main(string[] args)58        {59            string decodedString = GenerateCSCode.GetDecodedKeyboardInput("F3", "F3");60            Console.WriteLine(decodedString);61        }62    }63}64Driver.Keyboard.SendKeys(Keys.F3);65using System;66using System.Collections.Generic;67using System.Linq;GetDecodedKeyboardInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7{8    {GetDecodedKeyboardInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Forms;7using WinAppDriverUIRecorder;8{9    {10        public Form1()11        {12            InitializeComponent();13        }14        private void button1_Click(object sender, EventArgs e)15        {16            string decodedKeyboardInput = GenerateCSCode.GetDecodedKeyboardInput(textBox1.Text);17            textBox2.Text = decodedKeyboardInput;18        }19    }20}21using System;22using System.Collections.Generic;23using System.Linq;24using System.Text;25using System.Threading.Tasks;26using System.Windows.Forms;27using WinAppDriverUIRecorder;28{29    {30        public Form1()31        {32            InitializeComponent();33        }34        private void button1_Click(object sender, EventArgs e)35        {36            string decodedKeyboardInput = GenerateCSCode.GetDecodedKeyboardInput(textBox1.Text);37            textBox2.Text = decodedKeyboardInput;38        }39    }40}41using System;42using System.Collections.Generic;43using System.Linq;44using System.Text;45using System.Threading.Tasks;46using System.Windows.Forms;47using WinAppDriverUIRecorder;48{49    {50        public Form1()51        {52            InitializeComponent();53        }54        private void button1_Click(object sender, EventArgs e)55        {56            string decodedKeyboardInput = GenerateCSCode.GetDecodedKeyboardInput(textBox1.Text);57            textBox2.Text = decodedKeyboardInput;58        }59    }60}61using System;62using System.Collections.Generic;63using System.Linq;64using System.Text;65using System.Threading.Tasks;66using System.Windows.Forms;67using WinAppDriverUIRecorder;68{69    {70        public Form1()71        {72            InitializeComponent();73        }74        private void button1_Click(object sender, EventArgs e)75        {GetDecodedKeyboardInput
Using AI Code Generation
1var recorder = new WinAppDriverUIRecorder.GenerateCSCode();2var code = recorder.GetDecodedKeyboardInput("Hello World");3Console.WriteLine(code);4var recorder = new WinAppDriverUIRecorder.GenerateCSCode();5var code = recorder.GetDecodedKeyboardInput("Hello World");6Console.WriteLine(code);GetDecodedKeyboardInput
Using AI Code Generation
1using WinAppDriverUIRecorder;2using System.IO;3using System;4using System.Collections.Generic;5{6    {7        static void Main(string[] args)8        {9            var path = "C:\\Program.cs";10            var path2 = "C:\\Program2.cs";11            var path3 = "C:\\Program3.cs";12            var path4 = "C:\\Program4.cs";13            var path5 = "C:\\Program5.cs";14            var path6 = "C:\\Program6.cs";15            var path7 = "C:\\Program7.cs";16            var path8 = "C:\\Program8.cs";17            var path9 = "C:\\Program9.cs";18            var path10 = "C:\\Program10.cs";19            var path11 = "C:\\Program11.cs";20            var path12 = "C:\\Program12.cs";21            var path13 = "C:\\Program13.cs";22            var path14 = "C:\\Program14.cs";23            var path15 = "C:\\Program15.cs";24            var path16 = "C:\\Program16.cs";25            var path17 = "C:\\Program17.cs";26            var path18 = "C:\\Program18.cs";GetDecodedKeyboardInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Forms;7using WinAppDriverUIRecorder;8{9    {10        static void Main(string[] args)11        {12            StringBuilder sb = new StringBuilder();13            WinAppDriverUIRecorder.GenerateCSCode codeGen = new WinAppDriverUIRecorder.GenerateCSCode();14            codeGen.GetDecodedKeyboardInput(sb, "This is a test");15            Console.WriteLine(sb.ToString());16        }17    }18}19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24using System.Windows.Forms;25using WinAppDriverUIRecorder;26{27    {28        static void Main(string[] args)29        {30            StringBuilder sb = new StringBuilder();31            WinAppDriverUIRecorder.GenerateCSCode codeGen = new WinAppDriverUIRecorder.GenerateCSCode();32            codeGen.GetDecodedMouseInput(sb, "Left", "Click", "100", "200");33            Console.WriteLine(sb.ToString());34        }35    }36}GetDecodedKeyboardInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.IO;7using System.Windows.Forms;8using System.Diagnostics;9using System.Text.RegularExpressions;10using System.Threading;11using System.Runtime.InteropServices;12using System.Drawing;13using System.Drawing.Imaging;14using System.Xml;15using System.Xml.Linq;16using System.Xml.XPath;17using System.Xml.Xsl;18using System.Xml.Serialization;19using System.Net;20using System.Net.Sockets;21using System.Net.NetworkInformation;22using System.Runtime.Serialization;23using System.Runtime.Serialization.Formatters.Binary;24using System.Security.Cryptography;25using System.Security.Cryptography.X509Certificates;26using System.Security.Cryptography.Xml;27using System.Security.Cryptography.X509Certificates;28using System.Security.Principal;29using System.Security.AccessControl;30using System.Security.Permissions;31using System.Security;32using System.Reflection;33using System.ComponentModel;34using System.ComponentModel.Design;35using System.ComponentModel.Design.Serialization;36using System.ComponentModel.DataAnnotations;37using System.ComponentModel.DataAnnotations.Schema;38using System.ComponentModel.Composition;39using System.ComponentModel.Composition.Hosting;40using System.ComponentModel.Composition.Primitives;41using System.Runtime.Remoting;42using System.Runtime.Remoting.Channels;43using System.Runtime.Remoting.Channels.Tcp;44using System.Runtime.Remoting.Channels.Http;45using OpenQA.Selenium;46using OpenQA.Selenium.Interactions;47using OpenQA.Selenium.Appium;48using OpenQA.Selenium.Appium.Windows;49using OpenQA.Selenium.Appium.MultiTouch;50using OpenQA.Selenium.Appium.Interfaces;51using OpenQA.Selenium.Appium.Service;52using OpenQA.Selenium.Appium.Service.Options;53using OpenQA.Selenium.Appium.Android;54using OpenQA.Selenium.Appium.iOS;55using OpenQA.Selenium.Appium.Enums;56using OpenQA.Selenium.Remote;57using OpenQA.Selenium.Support.UI;58using OpenQA.Selenium.Support.Events;59using OpenQA.Selenium.Firefox;60using OpenQA.Selenium.Chrome;61using OpenQA.Selenium.IE;62using OpenQA.Selenium.Opera;63using OpenQA.Selenium.PhantomJS;64using OpenQA.Selenium.Safari;65using OpenQA.Selenium.Edge;66using OpenQA.Selenium.Remote;67using OpenQA.Selenium.Appium.Windows;68using OpenQA.Selenium.Appium.MultiTouch;69using WinAppDriverUIRecorder;70{71    {72        public GenerateCSCode()73        {74        }75        public string GetDecodedKeyboardInput(string input)76        {77            string output = "";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!!
