/V4/Quickstarts/EventAggregation/Desktop/EventAggregation.Tests.AcceptanceTests/Helpers/UIItemExtensions.cs

# · C# · 109 lines · 77 code · 13 blank · 19 comment · 12 complexity · 35f40add669083dbc912713d5e1600a2 MD5 · raw file

  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using Core.UIItems;
  22. using System.Windows.Automation;
  23. using Core.UIItems.TabItems;
  24. namespace EventAggregation.AcceptanceTests.Helpers
  25. {
  26. public static class UIItemExtensions
  27. {
  28. public static void Hover(this UIItem uiItem)
  29. {
  30. Core.InputDevices.Mouse.Instance.Location = Core.C.Center(uiItem.Bounds);
  31. System.Threading.Thread.Sleep(1000);
  32. }
  33. private static List<AutomationElement> automationElementList = new List<AutomationElement>();
  34. public static AutomationElement SearchInRawTreeByName(this AutomationElement rootElement, string name)
  35. {
  36. AutomationElement elementNode = TreeWalker.RawViewWalker.GetFirstChild(rootElement);
  37. while (elementNode != null)
  38. {
  39. if (name.Equals(elementNode.Current.Name, StringComparison.OrdinalIgnoreCase)
  40. || (name.Equals(elementNode.Current.AutomationId, StringComparison.OrdinalIgnoreCase)))
  41. {
  42. return elementNode;
  43. }
  44. AutomationElement returnedAutomationElement = elementNode.SearchInRawTreeByName(name);
  45. if (returnedAutomationElement != null)
  46. {
  47. return returnedAutomationElement;
  48. }
  49. elementNode = TreeWalker.RawViewWalker.GetNextSibling(elementNode);
  50. }
  51. return null;
  52. }
  53. public static List<AutomationElement> FindSiblingsInTreeByName(this AutomationElement rootElement, string name)
  54. {
  55. // Clear the automation element list.
  56. automationElementList.Clear();
  57. AutomationElement parentElement = rootElement;
  58. WalkThroughRawTreeAndPopulateAEList(parentElement, name);
  59. return automationElementList;
  60. }
  61. private static void WalkThroughRawTreeAndPopulateAEList(AutomationElement parentElement, string name)
  62. {
  63. AutomationElement element = SearchInRawTreeByName(parentElement, name);
  64. AutomationElement elementNode = null;
  65. if (element != null)
  66. {
  67. // Add the element to the list;
  68. automationElementList.Add(element);
  69. elementNode = TreeWalker.RawViewWalker.GetNextSibling(element);
  70. while (elementNode != null)
  71. {
  72. // Add the elementNode to the list
  73. if (elementNode.Current.AutomationId.Equals(name))
  74. automationElementList.Add(elementNode);
  75. else
  76. {
  77. WalkThroughRawTreeAndPopulateAEList(elementNode, name);
  78. }
  79. elementNode = TreeWalker.RawViewWalker.GetNextSibling(elementNode);
  80. }
  81. }
  82. }
  83. public static void SelectEmployee(this ListView list, int rowNumber)
  84. {
  85. list.Rows[rowNumber].Select();
  86. }
  87. public static void SelectEmployee(this ListView list, string firstName)
  88. {
  89. list.Rows.Find(r => r.Cells[0].Text.Equals(firstName)).Select();
  90. }
  91. public static void SelectEmployee(this ListView list, string firstName, string lastName)
  92. {
  93. list.Rows.Find(r => r.Cells[0].Text.Equals(firstName) && r.Cells[1].Text.Equals(lastName)).Select();
  94. }
  95. }
  96. }