using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.VisualStudio.TestTools.UITesting; using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls; using Microsoft.VisualStudio.TestTools.UITesting.SilverlightControls; using Microsoft.VisualStudio.TestTools.UITest.Extension; using SHDocVw; using mshtml; namespace CUITe.Controls { public class CUITe_ControlBaseFactory { public static T Create(string sSearchProperties) where T : ICUITe_ControlBase { Type type = typeof(T); return (T)Activator.CreateInstance(type, new object[] { sSearchProperties }); } } public class CUITe_ControlBase : ICUITe_ControlBase where T : UITestControl { protected T _control; private string _SearchProperties; public CUITe_ControlBase() { } public CUITe_ControlBase(string sSearchProperties) { if (sSearchProperties == null) throw new Exception("Parameter 'SearchProperties' cannot be null"); this._SearchProperties = sSearchProperties.Trim(); /////////////////////////////////////////////////////////////////////////////////////alin if (this._SearchProperties.Substring(this._SearchProperties.Length - 1) == ";") { this._SearchProperties = this._SearchProperties.Substring(0, this._SearchProperties.Length - 1); string[] saKeyValuePairs = this._SearchProperties.Split(';'); foreach (string sKeyValue in saKeyValuePairs) { string[] saKeyVal = sKeyValue.Split('='); if (saKeyVal.Length != 2) throw new CUITe_InvalidSearchParameterFormat(this._SearchProperties); string sKey = saKeyVal[0].ToLower(); string sValue = saKeyVal[1]; sValue = sValue.Replace("=", "equal"); } } /////////////////////////////////////////////////////////////////////////////////////alin } public Type GetBaseType() { return typeof(T); } public virtual void Wrap(object control) { this._control = control as T; this.fillSearchProperties(); this._control.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch); } /// /// UnWraps the CUITe_Html* or CUITe_Sl* controls to expose the underlying UITestControl. /// This helps when you want to use any methods/properties of the underlying UITestControl. /// CUITe_Html* or CUITe_Sl* controls are wrappers/abstractions which hides complexity. UnWrap() helps you break the abstraction. /// /// The underlying UITestControl instance. For example, returns HtmlEdit in case of CUITe_HtmlEdit. public T UnWrap() { return this._control; } public void WrapReady(object control) { this._control = control as T; } public void WaitForControlReady() { this._control.WaitForControlReady(); } public void Click() { this._control.WaitForControlReady(); Mouse.Click(this._control); } public void DoubleClick() { this._control.WaitForControlReady(); Mouse.DoubleClick(this._control); } public bool Enabled { get { this._control.WaitForControlReady(); return this._control.Enabled; } } public bool Exists { get { return this._control.Exists; } } public void SetFocus() { this._control.WaitForControlReady(); this._control.SetFocus(); } public void SetSearchProperty(string sPropertyName, string sValue) { /////////////////////////////////////////////////////////////////////////alin sValue = sValue.Replace("=", "equal"); /////////////////////////////////////////////////////////////////////////alin this._control.SearchProperties.Add(sPropertyName, sValue, PropertyExpressionOperator.EqualTo); } public void SetSearchPropertyRegx(string sPropertyName, string sValue) { /////////////////////////////////////////////////////////////////////////alin sValue = sValue.Replace("=", "equal"); /////////////////////////////////////////////////////////////////////////alin this._control.SearchProperties.Add(sPropertyName, sValue, PropertyExpressionOperator.Contains); } protected void fillSearchProperties() { if (this._SearchProperties != "*" && this._SearchProperties !=null) { string[] saKeyValuePairs = this._SearchProperties.Split(';'); foreach (string sKeyValue in saKeyValuePairs) { string[] saKeyVal = sKeyValue.Split('='); if (saKeyVal.Length != 2) throw new CUITe_InvalidSearchParameterFormat(this._SearchProperties); string sKey = saKeyVal[0].ToLower(); string sValue = saKeyVal[1]; /////////////////////////////////////////////////////////////////////////alin sValue = sValue.Replace("equal", "="); /////////////////////////////////////////////////////////////////////////alin if (sValue != "") { switch (sKey) { case "id": this._control.SearchProperties.Add(HtmlControl.PropertyNames.Id, sValue, PropertyExpressionOperator.EqualTo); break; case "class": this._control.SearchProperties.Add(HtmlControl.PropertyNames.Class, sValue, PropertyExpressionOperator.EqualTo); break; case "title": this._control.SearchProperties.Add(HtmlControl.PropertyNames.Title, sValue, PropertyExpressionOperator.EqualTo); break; case "innertext": this._control.SearchProperties.Add(HtmlControl.PropertyNames.InnerText, sValue, PropertyExpressionOperator.EqualTo); break; case "name": if (typeof(T).Namespace.Contains(".HtmlControls.")) { this._control.SearchProperties.Add(HtmlControl.PropertyNames.Name, sValue, PropertyExpressionOperator.EqualTo); } if (typeof(T).Namespace.Contains(".SilverlightControls.")) { this._control.SearchProperties.Add(SilverlightControl.PropertyNames.Name, sValue, PropertyExpressionOperator.EqualTo); } break; case "value": this._control.SearchProperties.Add(HtmlControl.PropertyNames.ValueAttribute, sValue, PropertyExpressionOperator.EqualTo); break; case "href": this._control.SearchProperties.Add(HtmlHyperlink.PropertyNames.Href, sValue, PropertyExpressionOperator.EqualTo); break; case "absolutepath": this._control.SearchProperties.Add(HtmlImage.PropertyNames.AbsolutePath, sValue, PropertyExpressionOperator.EqualTo); break; case "src": if (typeof(T).Namespace.Contains(".HtmlControls.")) { this._control.SearchProperties.Add(HtmlImage.PropertyNames.Src, sValue, PropertyExpressionOperator.EqualTo); } if (typeof(T).Namespace.Contains(".SilverlightControls.")) { this._control.SearchProperties.Add(SilverlightImage.PropertyNames.Source, sValue, PropertyExpressionOperator.EqualTo); } break; case "automationid": this._control.SearchProperties.Add(SilverlightControl.PropertyNames.AutomationId, sValue, PropertyExpressionOperator.EqualTo); break; case "text": this._control.SearchProperties.Add(SilverlightText.PropertyNames.Text, sValue, PropertyExpressionOperator.EqualTo); break; default: throw new CUITe_InvalidSearchKey(saKeyVal[0], this._SearchProperties); } } } } } protected void RunScript(string sCode) { BrowserWindow _bw = (BrowserWindow)this._control.TopParent; InternetExplorer IE = null; ShellWindows shws = new ShellWindows(); foreach (InternetExplorer shwin in shws) { if (shwin.HWND == _bw.WindowHandle.ToInt32()) { IE = shwin; break; } } IE.Document.parentWindow.execScript(sCode); } #region implementing parent, sibling etc methods as virtual public virtual ICUITe_ControlBase Parent { get { return null; } } public virtual ICUITe_ControlBase PreviousSibling { get { return null; } } public virtual ICUITe_ControlBase NextSibling { get { return null; } } public virtual ICUITe_ControlBase FirstChild { get { return null; } } public virtual List GetChildren() { return null; } #endregion } }