/V1/trunk/Source/StockTraderRI/StockRI.Tests.AcceptanceTests/Helpers/UIItemExtensions.cs

# · C# · 174 lines · 124 code · 14 blank · 36 comment · 7 complexity · 53d82365b34dbbb4a2e8d042e3d1b819 MD5 · raw file

  1. //===============================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation
  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. using StockTraderRI.AcceptanceTests.TestInfrastructure;
  25. using Core.UIItems.WindowItems;
  26. using Core.UIItems.Finders;
  27. using System.Globalization;
  28. namespace StockTraderRI.AcceptanceTests.Helpers
  29. {
  30. public static class UIItemExtensions
  31. {
  32. public static void Hover(this UIItem uiItem)
  33. {
  34. Core.InputDevices.Mouse.Instance.Location = Core.C.Center(uiItem.Bounds);
  35. System.Threading.Thread.Sleep(1000);
  36. }
  37. /// <summary>
  38. /// Finds out whether an object exists in the raw tree with the name or automation id.
  39. /// </summary>
  40. /// <param name="rootElement"></param>
  41. /// <param name="name">Either name or automation id of the control</param>
  42. /// <returns></returns>
  43. public static AutomationElement SearchInRawTreeByName(this AutomationElement rootElement, string name)
  44. {
  45. AutomationElement elementNode = TreeWalker.RawViewWalker.GetFirstChild(rootElement);
  46. while (elementNode != null)
  47. {
  48. if (name.Equals(elementNode.Current.Name, StringComparison.OrdinalIgnoreCase)
  49. ||(name.Equals(elementNode.Current.AutomationId, StringComparison.OrdinalIgnoreCase)))
  50. {
  51. return elementNode;
  52. }
  53. AutomationElement returnedAutomationElement = elementNode.SearchInRawTreeByName(name);
  54. if (returnedAutomationElement != null)
  55. {
  56. return returnedAutomationElement;
  57. }
  58. elementNode = TreeWalker.RawViewWalker.GetNextSibling(elementNode);
  59. }
  60. return null;
  61. }
  62. #region Overloaded TryGet methods
  63. public static TItem TryGet<TItem>(this Window wind) where TItem : UIItem
  64. {
  65. try
  66. {
  67. return wind.Get<TItem>();
  68. }
  69. catch
  70. {
  71. return null;
  72. }
  73. }
  74. public static TItem TryGet<TItem>(this Window wind, SearchCriteria searchCriteria) where TItem : UIItem
  75. {
  76. try
  77. {
  78. return wind.Get<TItem>(searchCriteria);
  79. }
  80. catch
  81. {
  82. return null;
  83. }
  84. }
  85. public static IUIItem TryGet(this Window wind, SearchCriteria searchCriteria)
  86. {
  87. try
  88. {
  89. return wind.Get(searchCriteria);
  90. }
  91. catch
  92. {
  93. return null;
  94. }
  95. }
  96. public static TItem TryGet<TItem>(this Window wind, string primaryIdentification) where TItem : UIItem
  97. {
  98. try
  99. {
  100. return wind.Get<TItem>(primaryIdentification);
  101. }
  102. catch
  103. {
  104. return null;
  105. }
  106. }
  107. #endregion
  108. public static UIItem GetWatchListRegionHeader(this UIItemContainer rootElement)
  109. {
  110. Tab tab = rootElement.Get<Tab>(TestDataInfrastructure.GetControlId("CollapsibleRegion"));
  111. UIItem watchListTab = tab.Pages.Find(x => x.NameMatches(TestDataInfrastructure.GetControlId("WatchListHeader"))) as UIItem;
  112. return watchListTab;
  113. }
  114. public static UIItem GetCollapsibleRegionHeader(this UIItemContainer rootElement, string controlId)
  115. {
  116. Tab tab = rootElement.Get<Tab>(TestDataInfrastructure.GetControlId("CollapsibleRegion"));
  117. UIItem watchListTab = tab.Pages.Find(x => x.NameMatches(TestDataInfrastructure.GetControlId(controlId))) as UIItem;
  118. return watchListTab;
  119. }
  120. /// <summary>
  121. /// Get the required field data from the specified row of the Position Table
  122. /// </summary>
  123. /// <param name="list">the position table UI object</param>
  124. /// <param name="rowNumber">Row number from which data needs to fished out of the Position table</param>
  125. /// <param name="header">column header of data that is expected</param>
  126. /// <returns>value in the specified field of the specified row of the Position table</returns>
  127. public static object GetData(this ListView list, int rowNumber, PositionTableColumnHeader header)
  128. {
  129. switch (header)
  130. {
  131. case PositionTableColumnHeader.Symbol:
  132. return list.Rows[rowNumber].Cells[TestDataInfrastructure.GetTestInputData("PositionTableSymbol")].Text;
  133. case PositionTableColumnHeader.NumberOfShares:
  134. return Convert.ToInt32(list.Rows[rowNumber].Cells[TestDataInfrastructure.GetTestInputData("PositionTableShares")].Text, CultureInfo.CurrentCulture);
  135. default:
  136. return null;
  137. }
  138. }
  139. /// <summary>
  140. /// Get the required field data for the specified symbol from the Position Table
  141. /// </summary>
  142. /// <param name="list">the position table UI object</param>
  143. /// <param name="forSymbol">symbol for which data is required</param>
  144. /// <param name="header">column header of data that is expected</param>
  145. /// <returns>value in the specified field of the specified symbol row of the Position table</returns>
  146. public static object GetData(this ListView list, string forSymbol, PositionTableColumnHeader header)
  147. {
  148. switch (header)
  149. {
  150. case PositionTableColumnHeader.Symbol:
  151. return forSymbol;
  152. case PositionTableColumnHeader.NumberOfShares:
  153. return Convert.ToInt32(list.Rows.Find(r => r.Cells[TestDataInfrastructure.GetTestInputData("PositionTableSymbol")].Text.Equals(forSymbol))
  154. .Cells[TestDataInfrastructure.GetTestInputData("PositionTableShares")].Text, CultureInfo.CurrentCulture);
  155. default:
  156. return null;
  157. }
  158. }
  159. }
  160. }