Best WinAppDriver code snippet using WinAppDriverUIRecorder.GenerateCSCode.SendKeys
RecordedUiTask.cs
Source:RecordedUiTask.cs
...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}...
SendKeys
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7using OpenQA.Selenium.Appium.Windows;8using OpenQA.Selenium.Remote;9using OpenQA.Selenium;10using System.Threading;11using OpenQA.Selenium.Appium;12using OpenQA.Selenium.Appium.Windows;13using System.Windows.Automation;14{15 {16 static void Main(string[] args)17 {
SendKeys
Using AI Code Generation
1using WinAppDriverUIRecorder;2using System.Windows.Automation;3using System.Threading;4using System.Windows.Forms;5using System;6{7 static void Main(string[] args)8 {9 var app = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculator"));10 var button = app.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "One"));11 WinAppDriverUIRecorder.GenerateCSCode.SendKeys(button, "1");12 button = app.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Plus"));13 WinAppDriverUIRecorder.GenerateCSCode.SendKeys(button, "+");14 button = app.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Seven"));15 WinAppDriverUIRecorder.GenerateCSCode.SendKeys(button, "7");16 button = app.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Equals"));17 WinAppDriverUIRecorder.GenerateCSCode.SendKeys(button, "{ENTER}");18 }19}20using WinAppDriverUIRecorder;21using System.Windows.Automation;22using System.Threading;23using System.Windows.Forms;24using System;25{26 static void Main(string[] args)27 {28 var app = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculator"));29 var button = app.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "One"));30 WinAppDriverUIRecorder.GenerateCSCode.Click(button, "1");31 button = app.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Plus"));32 WinAppDriverUIRecorder.GenerateCSCode.Click(button, "+");33 button = app.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Seven"));
SendKeys
Using AI Code Generation
1WinAppDriverUIRecorder.GenerateCSCode.SendKeys("4", "4");2WinAppDriverUIRecorder.GenerateCSCode.ClickOnControl("Button", "Add");3string result = WinAppDriverUIRecorder.GenerateCSCode.GetText("Result");4Assert.AreEqual("7", result);5WinAppDriverUIRecorder.GenerateCSCode.SendKeys("5", "5");6WinAppDriverUIRecorder.GenerateCSCode.ClickOnControl("Button", "Add");7string result = WinAppDriverUIRecorder.GenerateCSCode.GetText("Result");8Assert.AreEqual("10", result);9WinAppDriverUIRecorder.GenerateCSCode.SendKeys("6", "6");10WinAppDriverUIRecorder.GenerateCSCode.ClickOnControl("Button", "Add");11string result = WinAppDriverUIRecorder.GenerateCSCode.GetText("Result");12Assert.AreEqual("12", result);13WinAppDriverUIRecorder.GenerateCSCode.SendKeys("7", "7");14WinAppDriverUIRecorder.GenerateCSCode.ClickOnControl("Button", "Add");15string result = WinAppDriverUIRecorder.GenerateCSCode.GetText("Result");16Assert.AreEqual("15", result);
SendKeys
Using AI Code Generation
1public void TestMethod4()2{3 var windowName = "Window";4 var elementName = "Element";5 var keys = "Keys";6 var delay = "Delay";7 WinAppDriverUIRecorder.GenerateCSCode.SendKeys(windowName, elementName, keys, delay);8}
SendKeys
Using AI Code Generation
1string code = WinAppDriverUIRecorder.GenerateCSCode.SendKeys("Text to be entered in the textbox");2Console.WriteLine(code);3WinAppDriverUIRecorder.GenerateCSCode.ExecuteCode(code);4WinAppDriverUIRecorder.GenerateCSCode.SendKeysAndExecute("Text to be entered in the textbox");5string code = WinAppDriverUIRecorder.GenerateVBCode.SendKeys("Text to be entered in the textbox");6Console.WriteLine(code);7WinAppDriverUIRecorder.GenerateVBCode.ExecuteCode(code);8WinAppDriverUIRecorder.GenerateVBCode.SendKeysAndExecute("Text to be entered in the textbox");9string code = WinAppDriverUIRecorder.GenerateCSharpCode.SendKeys("Text to be entered in the textbox");10Console.WriteLine(code);11WinAppDriverUIRecorder.GenerateCSharpCode.ExecuteCode(code);
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!!