PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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