前段时间做的自动化测试的是Silverlight的,框架都已经搭好。突然测试发现这里还有一个要发送邮件的html页面,并且将另外启动浏览器,于是今天下午把这个html的也写出来。
用法 :
HtmlAutoTestFrameWork htf = new HtmlAutoTestFrameWork();
htf.BrowserDefaultIE();
HtmlTextArea heTo2 = htf.ByPropert<HtmlTextArea>("Id:input-to;TagName:TEXTAREA;Class:text");
heTo2.Text = "admin@cnblogs.com";
HtmlButton hbSend = htf.ByPropert<HtmlButton>("Id:btnSend;TagName:INPUT;ControlType:Button");
Mouse.Click(hbSend);
下面是html的一个弹出层,用的jsalert.js .获取弹出层元素
HtmlDiv htmlDiv = htf.ByPropert<HtmlDiv>("Id:popup_container;Class:ui-draggable");
这是弹出层得一个按钮
HtmlInputButton hbok = htf.ByPropert<HtmlInputButton>("Id:popup_ok;Type:button");
Mouse.Click(hbok);
下面是源码供参考
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.VisualStudio.TestTools.UITesting; using System.Threading; using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls; using System.Diagnostics; using System.Reflection; namespace autoUI { public class HtmlAutoTestFrameWork { private string m_uri = string.Empty; private BrowserWindow browser; public HtmlDocument htmlDoc; public string Title = string.Empty; public string AbsolutePath = string.Empty; public string ClassName = string.Empty; public string FrameDocument = string.Empty; #region HtmlDocument public void BrowserLaunch() { } public void BrowserLaunch(String Url) { m_uri = Url; browser = BrowserWindow.Launch(Url); BuildParams(); } private void BuildParams() { browser.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame"; htmlDoc = new Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument(browser); if (!string.IsNullOrWhiteSpace(m_uri)) htmlDoc.SearchProperties[Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument.PropertyNames.PageUrl] = m_uri; if (!string.IsNullOrWhiteSpace(Title)) htmlDoc.SearchProperties[Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument.PropertyNames.Title] = Title; if (!string.IsNullOrWhiteSpace(AbsolutePath)) htmlDoc.SearchProperties[Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument.PropertyNames.AbsolutePath] = AbsolutePath; if (!string.IsNullOrWhiteSpace(ClassName)) htmlDoc.SearchProperties[Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument.PropertyNames.ClassName] = ClassName; if (!string.IsNullOrWhiteSpace(FrameDocument)) htmlDoc.SearchProperties[Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument.PropertyNames.FrameDocument] = FrameDocument; } public void BrowserDefaultIE() { browser = BrowserWindow.FromProcess(GetProcess("iexplore")); BuildParams(); } public void BrowserFromProcess(Process p) { browser = BrowserWindow.FromProcess(p); BuildParams(); } public Process GetProcess(string browerType) { Process p = Process.GetCurrentProcess(); List<Process> list = new List<Process>(); foreach (var item in Process.GetProcesses()) { if (item.ProcessName.Equals(browerType,StringComparison.CurrentCultureIgnoreCase)) { list.Add(item); } } list.Sort( (a, b) => { return a.TotalProcessorTime.CompareTo(b.TotalProcessorTime); });//浏览器启动时间排序 return list[0];//返回浏览器启动用时最少的 } #endregion #region Find private T GetControl<T>(HtmlControl hc) where T : HtmlControl { T to = null; try { Type t = typeof(T); var types = new Type[1]; types[0] = typeof(HtmlControl); ConstructorInfo ci = t.GetConstructor(types); var obj = new object[1]; obj[0] = hc; to = (T)ci.Invoke(obj); } catch { to = default(T); } return to; } public T ByID<T>(string id) where T : HtmlControl { return ByID<T>(htmlDoc, id); } public T ByID<T>(HtmlControl hc, string id) where T : HtmlControl { return GetHtmlControlByProperty<T>(hc, "Id:"+id); } /// <summary> ///Get Control By Propert. eg HtmlDiv hd=f.ByPropert<HtmlDiv>("Id:IdValue;Name:NamaValue") /// </summary> /// <typeparam name="T">ControlType</typeparam> /// <param name="attributeValue">Id:IdValue;Name:NamaValue</param> /// <returns>T</returns> public T ByPropert<T>(string propertyValue) where T : HtmlControl { return ByPropert<T>(htmlDoc, propertyValue); } public T ByPropert<T>(HtmlControl hc, string propertyValue) where T : HtmlControl { return GetHtmlControlByProperty<T>(hc, propertyValue); } public T GetHtmlControlByProperty<T>(HtmlControl hc, string propertyValue) where T: HtmlControl { T t = GetControl<T>(hc); try { if (!string.IsNullOrWhiteSpace(propertyValue)) { string[] arr1 = propertyValue.Split(';'); for (int i = 0; i < arr1.Length; i++) { string[] arr2 = arr1[i].Split(':'); t.SearchProperties[arr2[0]] = arr2[1]; } } } catch { } return t; } #endregion public void Close() { browser.Close(); Thread.Sleep(5000); } } }