How to use ConstVariables class of WinAppDriverUIRecorder package

Best WinAppDriver code snippet using WinAppDriverUIRecorder.ConstVariables

RecordedUiTask.cs

Source:RecordedUiTask.cs Github

copy

Full Screen

...134 XmlNodeList uiTasks = xmlDocument.GetElementsByTagName("UiTask");135 if (uiTasks.Count == 1)136 {137 string strTask = uiTasks[0].Attributes["task"].Value;138 _uiTaskName = ConstVariables.FromStringTaskName(strTask);139 if (_uiTaskName == UiTaskName.KeyboardInput)140 {141 _strBase64Text = uiTasks[0].Attributes["base64String"].Value;142 var varCapLock = uiTasks[0].Attributes["CapsLock"].Value;143 if (varCapLock != null)144 {145 _bCapsLock = Convert.ToBoolean(varCapLock);146 }147 var varNumLock = uiTasks[0].Attributes["NumLock"].Value;148 if (varNumLock != null)149 {150 _bNumLock = Convert.ToBoolean(varNumLock);151 }152 var varScrollLock = uiTasks[0].Attributes["ScrollLock"].Value;153 if (varScrollLock != null)154 {155 _bScrollLock = Convert.ToBoolean(varScrollLock);156 }157 var keyboardTaskDescription = GenerateCSCode.GetDecodedKeyboardInput(_strBase64Text, _bCapsLock, _bNumLock, _bScrollLock);158 StringBuilder sb = new StringBuilder();159 foreach (var strLine in keyboardTaskDescription)160 {161 sb.Append(strLine);162 }163 _strDescription = $"{_uiTaskName} VirtualKeys=\"{sb.ToString()}\" CapsLock={_bCapsLock} NumLock={_bNumLock} ScrollLock={_bScrollLock}";164 }165 else166 {167 var vleft = uiTasks[0].Attributes["x"];168 _strLeft = vleft != null ? vleft.Value : "";169 var vtop = uiTasks[0].Attributes["y"];170 _strTop = vtop != null ? vtop.Value : "";171 string name = uiTasks[0].LastChild.Attributes[ConstVariables.Name].Value;172 if (string.IsNullOrEmpty(name))173 {174 name = uiTasks[0].LastChild.Attributes[ConstVariables.AutomationId].Value;175 if (string.IsNullOrEmpty(name))176 {177 name = uiTasks[0].Name;178 }179 }180 if (_uiTaskName == UiTaskName.Drag || _uiTaskName == UiTaskName.MouseWheel)181 {182 _strDescription = $"{_uiTaskName} on \"{name}\" at ({_strLeft},{_strTop}) drag ({_nDeltaX},{_nDeltaY})";183 }184 else185 {186 _strDescription = $"{_uiTaskName} on \"{name}\" at ({_strLeft},{_strTop})";187 }188 }189 }190 }191 return _strDescription;192 }193 public override string ToString()194 {195 if (_strDescription == null)196 {197 _strDescription = GetDescription();198 }199 return _strDescription;200 }201 public string GetCSCode(int nOrder, int nOrderFocused)202 {203 StringBuilder sb = new StringBuilder();204 sb.AppendLine("// " + GetDescription());205 string consoleWriteLine = "Console.WriteLine(\"" + GetDescription().Replace("\"", "\\\"") + "\");";206 sb.AppendLine(consoleWriteLine);207 if (!string.IsNullOrEmpty(GetRootSessionXPath()))208 {209 sb.AppendLine($"// xpath excluding session root: {GetXPathExcludingSessinoRoot()}");210 }211 if (GetTask() == UiTaskName.LeftClick)212 {213 sb.AppendLine(GenerateCSCode.LeftClick(this, nOrder));214 }215 else if (GetTask() == UiTaskName.RightClick)216 {217 sb.AppendLine(GenerateCSCode.RightClick(this, nOrder));218 }219 else if (GetTask() == UiTaskName.LeftDblClick)220 {221 sb.AppendLine(GenerateCSCode.DoubleClick(this, nOrder));222 }223 else if (GetTask() == UiTaskName.MouseWheel)224 {225 sb.AppendLine(GenerateCSCode.Wheel(this, nOrder));226 }227 else if (GetTask() == UiTaskName.Drag)228 {229 sb.AppendLine(GenerateCSCode.Drag(this, nOrder));230 }231 else if (GetTask() == UiTaskName.MouseHover)232 {233 sb.AppendLine(GenerateCSCode.MouseHover(this, nOrder));234 }235 else if (GetTask() == UiTaskName.KeyboardInput)236 {237 sb.AppendLine(GenerateCSCode.SendKeys(this, nOrder, nOrderFocused));238 }239 else240 {241 sb.AppendLine(GenerateCSCode.FindByXPath(this, nOrder));242 }243 return sb.ToString();244 }245 static string ReplaceLastWith(string strScript, string strOld, string strNew)246 {247 int nPos = strScript.LastIndexOf(strOld);248 if (nPos < 0)249 {250 return strScript;251 }252 return strScript.Remove(nPos, strOld.Length).Insert(nPos, strNew);253 }254 public void ChangeClickToDoubleClick()255 {256 _strXmlNode = ReplaceLastWith(_strXmlNode, UiTaskName.LeftClick.ToString(), UiTaskName.LeftDblClick.ToString());257 _uiTaskName = UiTaskName.LeftDblClick;258 _strDescription = null;259 }260 public void DragComplete(int deltaX, int deltaY)261 {262 _nDeltaX = deltaX;263 _nDeltaY = deltaY;264 string strDeltaX = deltaX.ToString();265 string strDeltaY = deltaY.ToString();266 string strUpdated = ReplaceLastWith(_strXmlNode, ConstVariables.DELTAX, strDeltaX);267 _strXmlNode = ReplaceLastWith(strUpdated, ConstVariables.DELTAY, strDeltaY);268 _uiTaskName = UiTaskName.Drag;269 _strDescription = null;270 }271 public void UpdateWheelData(int delta)272 {273 string wdata = ConstVariables.WHEEL;274 int nPos1 = _strXmlNode.IndexOf("wheel=\"") + "wheel=\"".Length;275 int nPos2 = _strXmlNode.IndexOf("\">", nPos1);276 if (nPos2 > nPos1)277 {278 wdata = _strXmlNode.Substring(nPos1, nPos2 - nPos1);279 int nCurData = 0;280 if (int.TryParse(wdata, out nCurData))281 {282 delta += nCurData;283 }284 }285 _nDeltaX += 1;286 _nDeltaY = delta;287 string strdelta = $"wheel=\"{delta}\"";288 wdata = $"wheel=\"{wdata}\"";289 _strXmlNode = _strXmlNode.Replace(wdata, strdelta);290 _uiTaskName = UiTaskName.MouseWheel;291 _strDescription = null;292 }293 }294 class GenerateCSCode295 {296 public static string LeftClick(RecordedUiTask uiTask, int nOrder)297 {298 return $"string xp{nOrder} = {uiTask.GetXPath()};\n" +299 $"var winElem{nOrder} = MyDesktopSession.FindElementByXPath(xp{nOrder});\n" +300 $"if (winElem{nOrder} != null)\n" +301 "{\n" +302 $" winElem{nOrder}.Click();\n" +303 "}\n" +304 "else\n" +305 "{\n" +306 " Console.WriteLine($\"Failed to find element {xp" + $"{nOrder}" + "}\");\n" +307 " return;\n" +308 "}\n";309 }310 public static string DoubleClick(RecordedUiTask uiTask, int nOrder)311 {312 return $"string xp{nOrder} = {uiTask.GetXPath()};\n" +313 $"var winElem{nOrder} = MyDesktopSession.FindElementByXPath(xp{nOrder});\n" +314 $"if (winElem{nOrder} != null)\n" +315 "{\n" +316 $" MyDesktopSession.DesktopSession.Mouse.MouseMove(winElem{nOrder}.Coordinates);\n" +317 $" MyDesktopSession.DesktopSession.Mouse.DoubleClick(null);\n" +318 "}\n" +319 "else\n" +320 "{\n" +321 " Console.WriteLine($\"Failed to find element {xp" + $"{nOrder}" + "}\");\n" +322 " return;\n" +323 "}\n";324 }325 public static string RightClick(RecordedUiTask uiTask, int nOrder)326 {327 return $"string xp{nOrder} = {uiTask.GetXPath()};\n" +328 $"var winElem{nOrder} = MyDesktopSession.FindElementByXPath(xp{nOrder});\n" +329 $"if (winElem{nOrder} != null)\n" +330 "{\n" +331 $" MyDesktopSession.DesktopSession.Mouse.MouseMove(winElem{nOrder}.Coordinates);\n" +332 " MyDesktopSession.DesktopSession.Mouse.ContextClick(null);\n" +333 "}\n" +334 "else\n" +335 "{\n" +336 " Console.WriteLine($\"Failed to find element {xp" + $"{nOrder}" + "}\");\n" +337 " return;\n" +338 "}\n";339 }340 public static string FindByXPath(RecordedUiTask uiTask, int nOrder)341 {342 return $"string xp{nOrder} = {uiTask.GetXPath()};\n" +343 $"var winElem{nOrder} = MyDesktopSession.FindElementByXPath(xp{nOrder});\n" +344 $"if (winElem{nOrder} != null)\n" +345 "{\n" +346 $" //TODO: Add UI task at ({uiTask.GetLeft()},{uiTask.GetTop()}) on winElem{nOrder}\n" +347 "}\n" +348 "else\n" +349 "{\n" +350 " Console.WriteLine($\"Failed to find element {xp" + $"{nOrder}" + "}\");\n" +351 " return;\n" +352 "}\n";353 }354 public static string Drag(RecordedUiTask uiTask, int nOrder)355 {356 return $"string xp{nOrder} = {uiTask.GetXPath()};\n" +357 $"var winElem{nOrder} = MyDesktopSession.FindElementByXPath(xp{nOrder});\n" +358 $"if (winElem{nOrder} != null)\n" +359 "{\n" +360 $" //TODO: Drag from ({uiTask.GetLeft()},{uiTask.GetTop()}) on winElem{nOrder} by ({uiTask.GetDeltaX()},{uiTask.GetDeltaY()})\n" +361 "}\n" +362 "else\n" +363 "{\n" +364 " Console.WriteLine($\"Failed to find element {xp" + $"{nOrder}" + "}\");\n" +365 " return;\n" +366 "}\n";367 }368 public static string Wheel(RecordedUiTask uiTask, int nOrder)369 {370 return $"string xp{nOrder} = {uiTask.GetXPath()};\n" +371 $"var winElem{nOrder} = MyDesktopSession.FindElementByXPath(xp{nOrder});\n" +372 $"if (winElem{nOrder} != null)\n" +373 "{\n" +374 $" //TODO: Wheel at ({uiTask.GetLeft()},{uiTask.GetTop()}) on winElem{nOrder}, Count:{uiTask.GetDeltaX()}, Total Amount:{uiTask.GetDeltaY()}\n" +375 "}\n" +376 "else\n" +377 "{\n" +378 " Console.WriteLine($\"Failed to find element {xp" + $"{nOrder}" + "}\");\n" +379 " return;\n" +380 "}\n";381 }382 public static string MouseHover(RecordedUiTask uiTask, int nOrder)383 {384 return $"string xp{nOrder} = {uiTask.GetXPath()};\n" +385 $"var winElem{nOrder} = MyDesktopSession.FindElementByXPath(xp{nOrder});\n" +386 $"if (winElem{nOrder} != null)\n" +387 "{\n" +388 $" //TODO: Hover at ({uiTask.GetLeft()},{uiTask.GetTop()}) on winElem{nOrder}\n" +389 "}\n" +390 "else\n" +391 "{\n" +392 " Console.WriteLine($\"Failed to find element {xp" + $"{nOrder}" + "}\");\n" +393 " return;\n" +394 "}\n";395 }396 public static List<string> GetDecodedKeyboardInput(string strBase64, bool bCapsLock, bool bNumLock, bool ScrollLock)397 {398 byte[] data = Convert.FromBase64String(strBase64);399 int i = 0;400 bool shift = false;401 StringBuilder sb = new StringBuilder();402 List<string> lines = new List<string>();403 while (i < data.Length / 2)404 {405 int n1 = i * 2;406 int n2 = i * 2 + 1;407 i++;408 bool bIsKeyDown = data[n1] == 0;409 VirtualKeys vk = (VirtualKeys)data[n2];410 if (vk == VirtualKeys.VK_SHIFT || vk == VirtualKeys.VK_LSHIFT || vk == VirtualKeys.VK_RSHIFT)411 {412 shift = bIsKeyDown;413 continue;414 }415 if (vk == VirtualKeys.VK_CAPITAL)416 {417 if (bIsKeyDown)418 {419 bCapsLock = !bCapsLock;420 }421 continue;422 }423 char ch = ConstVariables.Vk2char((int)vk, shift || bCapsLock);424 // Create lines like winElem1.SendKeys(Keys.Control+ "a" + Keys.Control);425 bool bOutputVK = false;426 if (vk == VirtualKeys.VK_CONTROL ||427 vk == VirtualKeys.VK_LCONTROL ||428 vk == VirtualKeys.VK_RCONTROL ||429 vk == VirtualKeys.VK_MENU ||430 vk == VirtualKeys.VK_LMENU ||431 vk == VirtualKeys.VK_RMENU ||432 vk == VirtualKeys.VK_LWIN ||433 vk == VirtualKeys.VK_RWIN)434 {435 bOutputVK = true;436 }437 if (ch != 0)438 {439 if (bIsKeyDown)440 {441 sb.Append(ch);442 }443 }444 else445 {446 if (bIsKeyDown && sb.Length > 0)447 {448 lines.Add("\"" + sb.ToString() + "\"");449 sb.Clear();450 }451 string vkStr = vk.ToString();452 string vkSendKey = ConstVariables.Vk2string(vkStr);453 if (bOutputVK)454 {455 if (bIsKeyDown)456 {457 sb.Append("Keys." + vkSendKey + "+ \"");458 }459 else460 {461 lines.Add(sb.ToString() + "\" + Keys." + vkSendKey);462 sb.Clear();463 }464 }465 else466 {...

Full Screen

Full Screen

GenerateXPath.cs

Source:GenerateXPath.cs Github

copy

Full Screen

...43 }44 public char char1;45 public char char2;46 }47 class ConstVariables48 {49 public const int DragDeltaOffset = 12345678;50 public const string DELTAX = "DELTAX";51 public const string DELTAY = "DELTAY";52 public const string WHEEL = "WHEEL";53 public const string ClassName = "ClassName";54 public const string Name = "Name";55 public const string AutomationId = "AutomationId";56 public const string UiTask = "UiTask";57 public const string Pos = "Pos";58 public static Dictionary<string, string> s_Vk2String = new Dictionary<string, string>();59 public static Dictionary<int, CharPair> s_Vk2CharPair = new Dictionary<int, CharPair>();60 public static UiTaskName FromStringTaskName(string strTask)61 {62 foreach (UiTaskName val in Enum.GetValues(typeof(UiTaskName)))63 {64 if (strTask == val.ToString())65 {66 return val;67 }68 }69 return UiTaskName.UnknownUiTask;70 }71 public static void InitVk2StringMap()72 {73 s_Vk2String.Add(VirtualKeys.VK_NUMPAD0.ToString(), "NumberPad0");74 s_Vk2String.Add(VirtualKeys.VK_NUMPAD1.ToString(), "NumberPad1");75 s_Vk2String.Add(VirtualKeys.VK_NUMPAD2.ToString(), "NumberPad2");76 s_Vk2String.Add(VirtualKeys.VK_NUMPAD3.ToString(), "NumberPad3");77 s_Vk2String.Add(VirtualKeys.VK_NUMPAD4.ToString(), "NumberPad4");78 s_Vk2String.Add(VirtualKeys.VK_NUMPAD5.ToString(), "NumberPad5");79 s_Vk2String.Add(VirtualKeys.VK_NUMPAD6.ToString(), "NumberPad6");80 s_Vk2String.Add(VirtualKeys.VK_NUMPAD7.ToString(), "NumberPad7");81 s_Vk2String.Add(VirtualKeys.VK_NUMPAD8.ToString(), "NumberPad8");82 s_Vk2String.Add(VirtualKeys.VK_NUMPAD9.ToString(), "NumberPad9");83 s_Vk2String.Add(VirtualKeys.VK_MULTIPLY.ToString(), "Multiply");84 s_Vk2String.Add(VirtualKeys.VK_ADD.ToString(), "Add");85 s_Vk2String.Add(VirtualKeys.VK_SEPARATOR.ToString(), "Separator");86 s_Vk2String.Add(VirtualKeys.VK_OEM_NEC_EQUAL.ToString(), "Equal");87 s_Vk2String.Add(VirtualKeys.VK_SUBTRACT.ToString(), "Subtract");88 s_Vk2String.Add(VirtualKeys.VK_DIVIDE.ToString(), "Divide");89 s_Vk2String.Add(VirtualKeys.VK_F1.ToString(), "F1");90 s_Vk2String.Add(VirtualKeys.VK_F2.ToString(), "F2");91 s_Vk2String.Add(VirtualKeys.VK_F3.ToString(), "F3");92 s_Vk2String.Add(VirtualKeys.VK_F4.ToString(), "F4");93 s_Vk2String.Add(VirtualKeys.VK_F5.ToString(), "F5");94 s_Vk2String.Add(VirtualKeys.VK_F6.ToString(), "F6");95 s_Vk2String.Add(VirtualKeys.VK_F7.ToString(), "F7");96 s_Vk2String.Add(VirtualKeys.VK_F8.ToString(), "F8");97 s_Vk2String.Add(VirtualKeys.VK_F9.ToString(), "F9");98 s_Vk2String.Add(VirtualKeys.VK_F10.ToString(), "F10");99 s_Vk2String.Add(VirtualKeys.VK_F11.ToString(), "F11");100 s_Vk2String.Add(VirtualKeys.VK_F12.ToString(), "F12");101 s_Vk2String.Add(VirtualKeys.VK_DECIMAL.ToString(), "Decimal");102 //s_Vk2String.Add(VirtualKeys., "Meta");103 s_Vk2String.Add(VirtualKeys.VK_OEM_1.ToString(), "Semicolon");104 s_Vk2String.Add(VirtualKeys.VK_INSERT.ToString(), "Insert");105 s_Vk2String.Add(VirtualKeys.VK_CANCEL.ToString(), "Cancel");106 s_Vk2String.Add(VirtualKeys.VK_HELP.ToString(), "Help");107 s_Vk2String.Add(VirtualKeys.VK_BACK.ToString(), "Backspace");108 s_Vk2String.Add(VirtualKeys.VK_TAB.ToString(), "Tab");109 s_Vk2String.Add(VirtualKeys.VK_CLEAR.ToString(), "Clear");110 s_Vk2String.Add(VirtualKeys.VK_RETURN.ToString(), "Return");111 //s_Vk2String.Add(VirtualKeys.VK_RETURN.ToString(), "Enter");112 s_Vk2String.Add(VirtualKeys.VK_LSHIFT.ToString(), "Shift");113 s_Vk2String.Add(VirtualKeys.VK_RSHIFT.ToString(), "RightShift");114 s_Vk2String.Add(VirtualKeys.VK_LCONTROL.ToString(), "Control");115 s_Vk2String.Add(VirtualKeys.VK_RCONTROL.ToString(), "RightControl");116 s_Vk2String.Add(VirtualKeys.VK_LMENU.ToString(), "Alt");117 s_Vk2String.Add(VirtualKeys.VK_RMENU.ToString(), "RightAlt");118 s_Vk2String.Add(VirtualKeys.VK_DELETE.ToString(), "Delete");119 s_Vk2String.Add(VirtualKeys.VK_PAUSE.ToString(), "Pause");120 s_Vk2String.Add(VirtualKeys.VK_SPACE.ToString(), "Space");121 s_Vk2String.Add(VirtualKeys.VK_PRIOR.ToString(), "PageUp");122 s_Vk2String.Add(VirtualKeys.VK_NEXT.ToString(), "PageDown");123 s_Vk2String.Add(VirtualKeys.VK_END.ToString(), "End");124 s_Vk2String.Add(VirtualKeys.VK_HOME.ToString(), "Home");125 s_Vk2String.Add(VirtualKeys.VK_LEFT.ToString(), "Left");126 //s_Vk2String.Add(VirtualKeys.VK_LEFT.ToString(), "ArrowLeft");127 s_Vk2String.Add(VirtualKeys.VK_UP.ToString(), "Up");128 //s_Vk2String.Add(VirtualKeys.VK_UP.ToString(), "ArrowUp");129 s_Vk2String.Add(VirtualKeys.VK_RIGHT.ToString(), "Right");130 //s_Vk2String.Add(VirtualKeys.VK_RIGHT.ToString(), "ArrowRight");131 s_Vk2String.Add(VirtualKeys.VK_DOWN.ToString(), "Down");132 //s_Vk2String.Add(VirtualKeys.VK_DOWN.ToString(), "ArrowDown");133 s_Vk2String.Add(VirtualKeys.VK_ESCAPE.ToString(), "Escape");134 s_Vk2String.Add(VirtualKeys.VK_LWIN.ToString(), "Command");135 s_Vk2String.Add(VirtualKeys.VK_RWIN.ToString(), "Command");136 s_Vk2CharPair.Add(0x30, new CharPair('0', ')'));137 s_Vk2CharPair.Add(0x31, new CharPair('1', '!'));138 s_Vk2CharPair.Add(0x32, new CharPair('2', '@'));139 s_Vk2CharPair.Add(0x33, new CharPair('3', '#'));140 s_Vk2CharPair.Add(0x34, new CharPair('4', '$'));141 s_Vk2CharPair.Add(0x35, new CharPair('5', '%'));142 s_Vk2CharPair.Add(0x36, new CharPair('6', '^'));143 s_Vk2CharPair.Add(0x37, new CharPair('7', '&'));144 s_Vk2CharPair.Add(0x38, new CharPair('8', '*'));145 s_Vk2CharPair.Add(0x39, new CharPair('9', '('));146 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_3, new CharPair('`', '~'));147 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_MINUS, new CharPair('-', '_'));148 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_PLUS, new CharPair('=', '+'));149 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_4, new CharPair('[', '{'));150 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_6, new CharPair(']', '}'));151 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_5, new CharPair('\\', '|'));152 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_1, new CharPair(';', ':'));153 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_7, new CharPair(',', '<'));154 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_PERIOD, new CharPair('.', '>'));155 s_Vk2CharPair.Add((int)VirtualKeys.VK_OEM_2, new CharPair('/', '?'));156 s_Vk2CharPair.Add(0x41, new CharPair('a', 'A'));157 s_Vk2CharPair.Add(0x42, new CharPair('b', 'B'));158 s_Vk2CharPair.Add(0x43, new CharPair('c', 'C'));159 s_Vk2CharPair.Add(0x44, new CharPair('d', 'D'));160 s_Vk2CharPair.Add(0x45, new CharPair('e', 'E'));161 s_Vk2CharPair.Add(0x46, new CharPair('f', 'F'));162 s_Vk2CharPair.Add(0x47, new CharPair('g', 'G'));163 s_Vk2CharPair.Add(0x48, new CharPair('h', 'H'));164 s_Vk2CharPair.Add(0x49, new CharPair('i', 'I'));165 s_Vk2CharPair.Add(0x4A, new CharPair('j', 'J'));166 s_Vk2CharPair.Add(0x4B, new CharPair('k', 'K'));167 s_Vk2CharPair.Add(0x4C, new CharPair('l', 'L'));168 s_Vk2CharPair.Add(0x4D, new CharPair('m', 'M'));169 s_Vk2CharPair.Add(0x4E, new CharPair('n', 'N'));170 s_Vk2CharPair.Add(0x4F, new CharPair('o', 'O'));171 s_Vk2CharPair.Add(0x50, new CharPair('p', 'P'));172 s_Vk2CharPair.Add(0x51, new CharPair('q', 'Q'));173 s_Vk2CharPair.Add(0x52, new CharPair('r', 'R'));174 s_Vk2CharPair.Add(0x53, new CharPair('s', 'S'));175 s_Vk2CharPair.Add(0x54, new CharPair('t', 'T'));176 s_Vk2CharPair.Add(0x55, new CharPair('u', 'U'));177 s_Vk2CharPair.Add(0x56, new CharPair('v', 'V'));178 s_Vk2CharPair.Add(0x57, new CharPair('w', 'W'));179 s_Vk2CharPair.Add(0x58, new CharPair('x', 'X'));180 s_Vk2CharPair.Add(0x59, new CharPair('y', 'Y'));181 s_Vk2CharPair.Add(0x5A, new CharPair('z', 'Z'));182 }183 public static string Vk2string(string vkString)184 {185 if (s_Vk2String.ContainsKey(vkString))186 {187 return s_Vk2String[vkString];188 }189 else190 {191 return vkString;192 }193 }194 public static char Vk2char(int vk, bool bShiftDown)195 {196 if (s_Vk2CharPair.ContainsKey(vk))197 {198 CharPair chaiPaire = s_Vk2CharPair[vk];199 return bShiftDown ? chaiPaire.char2 : chaiPaire.char1;200 }201 return (char)0;202 }203 }204 class GenerateXPath205 {206 const string sNameValue = "[@{0}=\\\"{1}\\\"]";207 const string sNameStartsWithValue = "[starts-with(@{0},\\\"{1}\\\")]";208 public static string GenerateXPathToUiElement(string strXmlNodes)209 {210 XmlDocument xmlDocument = new XmlDocument();211 try212 {213 xmlDocument.LoadXml(strXmlNodes);214 }215 catch (Exception ex)216 {217 return ex.ToString();218 }219 string strRet = "";220 foreach (XmlElement uiTask in xmlDocument.GetElementsByTagName(ConstVariables.UiTask))221 {222 strRet += GetXPathFromUiTaskNode(uiTask);223 }224 if (strRet.Length > 2)225 {226 return strRet.Substring(0, strRet.Length - 2) + "\n"; // remove last ,227 }228 else229 {230 return strRet;231 }232 }233 public static string XmlEncode(string strData)234 {235 strData = strData.Replace("&", "&amp;");236 strData = strData.Replace("\"", "&quot;");237 strData = strData.Replace("\'", "&apos;");238 strData = strData.Replace("<", "&lt;");239 strData = strData.Replace(">", "&gt;");240 return strData;241 }242 static string CheckAndFixNoneStaticValue(string strValue)243 {244 const string strGUIDPtn = @"[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}";245 if (strValue.StartsWith("HwndWrapper[") || strValue.StartsWith("starts-with:HwndWrapper["))246 {247 strValue = "starts-with:HwndWrapper";248 }249 else if (strValue.StartsWith("ATL:") || strValue.StartsWith("starts-with:ATL:"))250 {251 strValue = "starts-with:ATL";252 }253 else if (strValue.StartsWith("WindowsForms10.") || strValue.StartsWith("starts-with:WindowsForms10."))254 {255 strValue = "starts-with:WindowsForms10";256 }257 else if (System.Text.RegularExpressions.Regex.Match(strValue, strGUIDPtn).Success)258 {259 strValue = "";260 }261 return strValue;262 }263 static string GetXPathFromUiTaskNode(XmlElement uiTaskNode)264 {265 if (uiTaskNode == null || uiTaskNode.ChildNodes.Count < 1)266 {267 return "";268 }269 string tag, ClassName, Name, AutomationId, Pos;270 string xPath = "";271 for (int i = 0; i < uiTaskNode.ChildNodes.Count; i++)272 {273 XmlNode childNode = uiTaskNode.ChildNodes[i];274 tag = childNode.Name != "Unknown" ? childNode.Name : "*";275 AutomationId = XmlEncode(childNode.Attributes[ConstVariables.AutomationId].Value);276 AutomationId = CheckAndFixNoneStaticValue(AutomationId);277 Name = XmlEncode(childNode.Attributes[ConstVariables.Name].Value);278 ClassName = childNode.Attributes[ConstVariables.ClassName].Value;279 ClassName = CheckAndFixNoneStaticValue(ClassName);280 Pos = childNode.Attributes[ConstVariables.Pos].Value;281 xPath += $"/{tag}";282 int nPos = xPath.Length;283 // AutomationId (like UIs on Cortana search result list) created at runtime may end with digits284 if (!string.IsNullOrEmpty(AutomationId) && !AutomationId.StartsWith("starts-with:"))285 {286 string patAutoIdEndsWithDigits = @"^([^\d]*)[\d]+$";287 System.Text.RegularExpressions.Regex regAutoId = new System.Text.RegularExpressions.Regex(patAutoIdEndsWithDigits, System.Text.RegularExpressions.RegexOptions.IgnoreCase);288 if (regAutoId != null)289 {290 System.Text.RegularExpressions.Match matchAutoId = regAutoId.Match(AutomationId);291 if (matchAutoId.Success && matchAutoId.Groups.Count > 1)292 {293 if (matchAutoId.Groups[1].Length > 0)294 {295 AutomationId = "starts-with:" + matchAutoId.Groups[1].ToString();296 }297 else298 {299 AutomationId = null;300 }301 }302 }303 }304 if (!string.IsNullOrEmpty(AutomationId))305 {306 if (AutomationId.StartsWith("starts-with:"))307 {308 AutomationId = AutomationId.Remove(0, "starts-with:".Length);309 xPath += string.Format(sNameStartsWithValue, ConstVariables.AutomationId, AutomationId);310 }311 else312 {313 xPath += string.Format(sNameValue, ConstVariables.AutomationId, AutomationId);314 }315 }316 if (!string.IsNullOrEmpty(Name))317 {318 if (Name.StartsWith("starts-with:"))319 {320 Name = Name.Remove(0, "starts-with:".Length);321 xPath += string.Format(sNameStartsWithValue, ConstVariables.Name, Name);322 }323 else324 {325 xPath += string.Format(sNameValue, ConstVariables.Name, Name);326 }327 }328 if (!string.IsNullOrEmpty(ClassName) && string.IsNullOrEmpty(AutomationId))329 {330 if (ClassName.StartsWith("starts-with:"))331 {332 ClassName = ClassName.Remove(0, "starts-with:".Length);333 xPath += string.Format(sNameStartsWithValue, ConstVariables.ClassName, ClassName);334 }335 else336 {337 xPath += string.Format(sNameValue, ConstVariables.ClassName, ClassName);338 }339 }340 if (!string.IsNullOrEmpty(Pos) && string.IsNullOrEmpty(AutomationId) && string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(ClassName))341 {342 //TODO: verify343 xPath = xPath.Insert(nPos, $"[position()={Pos}]");344 }345 }346 return "\"" + xPath + "\",\n";347 }348 }349}...

Full Screen

Full Screen

XmlNodePathRecorder.cs

Source:XmlNodePathRecorder.cs Github

copy

Full Screen

...133 int nLeft = Convert.ToInt32(left) - Convert.ToInt32(lastLeft);134 int nTop = Convert.ToInt32(top) - Convert.ToInt32(lastTop);135 if (mouseAction == UiTaskName.Drag.ToString())136 {137 sb.Insert(0, string.Format("<UiTask task=\"{0}\" x=\"{1}\" y=\"{2}\" deltaX=\"{3}\" deltaY=\"{4}\">\n", mouseAction, nLeft, nTop, ConstVariables.DELTAX, ConstVariables.DELTAY));138 }139 else if (mouseAction == UiTaskName.MouseWheel.ToString())140 {141 sb.Insert(0, string.Format("<UiTask task=\"{0}\" x=\"{1}\" y=\"{2}\" wheel=\"{3}\">\n", mouseAction, nLeft, nTop, ConstVariables.WHEEL));142 }143 else144 {145 sb.Insert(0, string.Format("<UiTask task=\"{0}\" x=\"{1}\" y=\"{2}\">\n", mouseAction, nLeft, nTop));146 }147 sb.AppendLine("</UiTask>");148 sb.AppendLine();149 return sb.ToString();150 }151 public static IntPtr ProcessMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)152 {153 handled = false;154 string left, top;155 if (msg == (int)UiTaskName.Inspect)156 {157 var nodesRootToLeaf = GetRootToLeafNodes(strUiToRootXmlNodesInspect, out left, out top);158 if (nodesRootToLeaf != null)159 {160 string strXmlNodes = CreateUiTaskXmlNodeString(nodesRootToLeaf, ((UiTaskName)msg).ToString(), left, top);161 if (string.IsNullOrEmpty(strXmlNodes) == false)162 {163 MainWindow.AddRecordedUi(new RecordedUiTask(strXmlNodes, 0, (UiTaskName)msg));164 }165 }166 return IntPtr.Zero;167 }168 if (MainWindow.IsRecording() == false)169 {170 return IntPtr.Zero;171 }172 lock (lockUiPath)173 {174 if (msg == (int)UiTaskName.KeyboardInput)175 {176 var keyboardTaskDescription = GenerateCSCode.GetDecodedKeyboardInput(strBase64KeyboardInput, bCapsLock, bNumLock, bScrollLock);177 StringBuilder sb = new StringBuilder();178 foreach (var strLine in keyboardTaskDescription)179 {180 sb.Append(GenerateXPath.XmlEncode(strLine));181 }182 var keyboarTask = new RecordedUiTask($"<UiTask task=\"{UiTaskName.KeyboardInput}\" VirtualKeys=\"{sb.ToString()}\" base64String=\"{strBase64KeyboardInput}\" CapsLock=\"{bCapsLock}\" NumLock=\"{bNumLock}\" ScrollLock=\"{bScrollLock}\"/>\n", (int)wParam, (UiTaskName)msg);183 MainWindow.AddRecordedUi(keyboarTask);184 }185 else if ((int)UiTaskName.LeftClick <= msg && msg <= (int)UiTaskName.MouseHover)186 {187 RecordedUiTask lastRecordedUi = null;188 if (MainWindow.s_listRecordedUi.Count > 0)189 {190 lastRecordedUi = MainWindow.s_listRecordedUi.Last();191 }192 string strXmlNodes = null;193 List<string> nodesRootToLeaf = GetRootToLeafNodes(strUiToRootXmlNodes, out left, out top);194 if (nodesRootToLeaf != null && nodesRootToLeaf.Count > 1)195 {196 strXmlNodes = CreateUiTaskXmlNodeString(nodesRootToLeaf, ((UiTaskName)msg).ToString(), left, top);197 }198 bool bAddNewTask = true;199 // Completing last UI200 if (msg == (int)UiTaskName.LeftDblClick && lastRecordedUi != null)201 {202 lastRecordedUi.ChangeClickToDoubleClick();203 bAddNewTask = false;204 }205 else if (msg == (int)UiTaskName.DragStop && lastRecordedUi != null)206 {207 int deltaX = wParam.ToInt32() - ConstVariables.DragDeltaOffset;208 int deltaY = lParam.ToInt32() - ConstVariables.DragDeltaOffset;209 lastRecordedUi.DragComplete(deltaX, deltaY);210 bAddNewTask = false;211 }212 else if (msg == (int)UiTaskName.MouseWheel)213 {214 int delta = wParam.ToInt32() - ConstVariables.DragDeltaOffset;215 if (strXmlNodes != null)216 {217 if (lastRecordedUi == null || lastRecordedUi.GetTask() != UiTaskName.MouseWheel)218 {219 lastRecordedUi = new RecordedUiTask(strXmlNodes, 0, (UiTaskName)msg);220 MainWindow.AddRecordedUi(lastRecordedUi);221 }222 }223 if (lastRecordedUi != null && lastRecordedUi.GetTask() == UiTaskName.MouseWheel)224 {225 lastRecordedUi.UpdateWheelData(delta);226 }227 bAddNewTask = false;228 }...

Full Screen

Full Screen

ConstVariables

Using AI Code Generation

copy

Full Screen

1using WinAppDriverUIRecorder;2using WinAppDriverUIRecorder;3using WinAppDriverUIRecorder;4using WinAppDriverUIRecorder;5using WinAppDriverUIRecorder;6using WinAppDriverUIRecorder;7using WinAppDriverUIRecorder;8using WinAppDriverUIRecorder;9using WinAppDriverUIRecorder;10using WinAppDriverUIRecorder;11using WinAppDriverUIRecorder;12using WinAppDriverUIRecorder;13using WinAppDriverUIRecorder;14using WinAppDriverUIRecorder;15using WinAppDriverUIRecorder;16using WinAppDriverUIRecorder;17using WinAppDriverUIRecorder;18using WinAppDriverUIRecorder;19using WinAppDriverUIRecorder;20using WinAppDriverUIRecorder;

Full Screen

Full Screen

ConstVariables

Using AI Code Generation

copy

Full Screen

1using WinAppDriverUIRecorder;2{3 {4 public static string AppPath = @"C:\Windows\System32\calc.exe";5 public static string AppTitle = "Calculator";6 }7}8using WinAppDriverUIRecorder;9{10 {11 public static string AppPath = @"C:\Windows\System32\calc.exe";12 public static string AppTitle = "Calculator";13 }14}15using WinAppDriverUIRecorder;16{17 {18 public static string AppPath = @"C:\Windows\System32\calc.exe";19 public static string AppTitle = "Calculator";20 }21}22using WinAppDriverUIRecorder;23{24 {25 public static string AppPath = @"C:\Windows\System32\calc.exe";26 public static string AppTitle = "Calculator";27 }28}29using WinAppDriverUIRecorder;30{31 {32 public static string AppPath = @"C:\Windows\System32\calc.exe";33 public static string AppTitle = "Calculator";34 }35}36using WinAppDriverUIRecorder;37{38 {39 public static string AppPath = @"C:\Windows\System32\calc.exe";40 public static string AppTitle = "Calculator";41 }42}43using WinAppDriverUIRecorder;44{45 {46 public static string AppPath = @"C:\Windows\System32\calc.exe";

Full Screen

Full Screen

ConstVariables

Using AI Code Generation

copy

Full Screen

1using WinAppDriverUIRecorder;2var app = WinAppDriverUIRecorder.ConstVariables.App;3var driver = WinAppDriverUIRecorder.ConstVariables.Driver;4var wait = WinAppDriverUIRecorder.ConstVariables.Wait;5var test = WinAppDriverUIRecorder.ConstVariables.Test;6using WinAppDriverUIRecorder;7var app = WinAppDriverUIRecorder.ConstVariables.App;8var driver = WinAppDriverUIRecorder.ConstVariables.Driver;9var wait = WinAppDriverUIRecorder.ConstVariables.Wait;10var test = WinAppDriverUIRecorder.ConstVariables.Test;11using WinAppDriverUIRecorder;12var app = WinAppDriverUIRecorder.ConstVariables.App;13var driver = WinAppDriverUIRecorder.ConstVariables.Driver;14var wait = WinAppDriverUIRecorder.ConstVariables.Wait;15var test = WinAppDriverUIRecorder.ConstVariables.Test;16using WinAppDriverUIRecorder;17var app = WinAppDriverUIRecorder.ConstVariables.App;18var driver = WinAppDriverUIRecorder.ConstVariables.Driver;19var wait = WinAppDriverUIRecorder.ConstVariables.Wait;20var test = WinAppDriverUIRecorder.ConstVariables.Test;21using WinAppDriverUIRecorder;22var app = WinAppDriverUIRecorder.ConstVariables.App;

Full Screen

Full Screen

ConstVariables

Using AI Code Generation

copy

Full Screen

1using WinAppDriverUIRecorder;2{3 {4 public static string PathToSaveRecordedScript = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";5 public static string PathToSaveRecordedScriptForCSharp = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";6 }7}8using WinAppDriverUIRecorder;9{10 {11 public static string PathToSaveRecordedScript = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";12 public static string PathToSaveRecordedScriptForCSharp = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";13 }14}15using WinAppDriverUIRecorder;16{17 {18 public static string PathToSaveRecordedScript = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";19 public static string PathToSaveRecordedScriptForCSharp = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";20 }21}22using WinAppDriverUIRecorder;23{24 {25 public static string PathToSaveRecordedScript = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";26 public static string PathToSaveRecordedScriptForCSharp = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";27 }28}29using WinAppDriverUIRecorder;30{31 {32 public static string PathToSaveRecordedScript = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";33 public static string PathToSaveRecordedScriptForCSharp = "C:\\Users\\Public\\Documents\\WinAppDriverUIRecorder";34 }35}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run WinAppDriver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful