《软件测试自动化之道》读书笔记 之 底层的Web UI 测试
2014-09-29 11:14
274 查看
《软件测试自动化之道》读书笔记 之 底层的Web UI 测试
2014-09-28测试自动化程序的任务
待测程序
测试程序
启动IE并连接到这个实例
如何判断待测web程序完全加载到浏览器
操纵并检查IE Shell
操作待测Web页面上的HTML元素的值
验证Web页面上HTML元素
示例代码
测试自动化程序的任务
底层技术的核心是,通过直接调用mshtml.dll和shdocvw.dll库来访问并且操纵IE客户区域的HTML对象。待测程序
新建一个网站“WebAUT”,删除原来的Default.aspx,创建一个新的Default.aspx。在其中添加3个Label控件、2个RadioButton控件、1个TextBox控件、1个Button控件和1个ListBox控件。// Chapter 7 - Low-Level Web UI Testing // Example Program: LowLevelUITest using System; using SHDocVw; // COM component = Microsoft Internet Controls. IE object using mshtml; // .NET component = Microsoft.mshtml. HTML interfaces using System.Diagnostics; // Process using System.Threading; // Sleep() namespace RunTest { class Class1 { static AutoResetEvent documentComplete = new AutoResetEvent(false); [STAThread] static void Main(string[] args) { try { Console.WriteLine("\nStarting test run"); bool pass = true; // assume test run will pass SHDocVw.InternetExplorer ie = null; Console.WriteLine("\nLaunching an instance of IE"); Process p = Process.Start("iexplore.exe", "about:blank"); System.Threading.Thread.Sleep(2000); if (p == null) throw new Exception("Could not launch IE"); Console.WriteLine("Process handle = " + p.MainWindowHandle.ToString()); SHDocVw.ShellWindows allBrowsers = new SHDocVw.ShellWindows(); Console.WriteLine("Number active browsers = " + allBrowsers.Count); if (allBrowsers.Count == 0) throw new Exception("Cannot find IE"); Console.WriteLine("Attaching to IE"); int i = 0; while (i < allBrowsers.Count && ie == null) { InternetExplorer e = (InternetExplorer)allBrowsers.Item(i); if (e != null) { if (e.HWND == (int)p.MainWindowHandle) ie = e; } ++i; } if (ie == null) throw new Exception("Failed to attach to IE"); ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete); Console.WriteLine("\nNavigating to the Web app"); object nil = new object(); ie.Navigate("http://localhost:30614/WebAUT/Default.aspx", ref nil, ref nil, ref nil, ref nil); documentComplete.WaitOne(); Console.WriteLine("Setting IE to size 450x360"); ie.Width = 450; ie.Height = 360; Thread.Sleep(1000); //if (ie.StatusText.IndexOf("Done") == -1) // Console.WriteLine("could not find 'Done' in status bar"); //else // Console.WriteLine("Find 'Done' in status bar"); HTMLDocument theDoc = (HTMLDocument)ie.Document; Console.WriteLine("\nSelecting 'ID' radio button"); HTMLInputElement radioButton = (HTMLInputElement)theDoc.getElementById("RadioButtonList1_2"); radioButton.@checked = true; Console.WriteLine("Setting text box to '2B'"); HTMLInputElement textBox = (HTMLInputElement)theDoc.getElementById("TextBox1"); Console.WriteLine("The textbox has type" + textBox.GetType().ToString()); textBox.value = "2B"; Console.WriteLine("Clicking search button"); HTMLInputElement butt = (HTMLInputElement)theDoc.getElementById("Button1"); butt.click(); documentComplete.WaitOne(); Console.WriteLine("Seek 'aloha' in <p>[2]"); Console.WriteLine("<p> type is:" + theDoc.getElementsByTagName("p").GetType().ToString()); HTMLParaElement paraElement = (HTMLParaElement)theDoc.getElementsByTagName("p").item(1, null); if (paraElement.innerText.ToString().IndexOf("aloha") >= 0) Console.WriteLine("Found target 'aloha'"); else Console.WriteLine("Target string not found"); Console.WriteLine("Seek 'adios' in <div id='div2'>"); HTMLDivElement divElement = (HTMLDivElement)theDoc.getElementsByTagName("div").item("div2", null); if (divElement.innerText.ToString().IndexOf("adios") >= 0) Console.WriteLine("Found target 'adios'"); else Console.WriteLine("Target string not found"); // non-HTML element Console.WriteLine("Seeking 'Search Complete' in body"); HTMLBody body = (HTMLBody)theDoc.getElementsByTagName("body").item(0, null); if (body.createTextRange().findText("Search Complete", 0, 4) == true) { Console.WriteLine("Found target string"); } else { Console.WriteLine("*Target string not found*"); pass = false; } if (pass) Console.WriteLine("\nTest result = Pass\n"); else Console.WriteLine("\nTest result = *FAIL*\n"); Console.WriteLine("Closing IE in 4 seconds . . . "); Thread.Sleep(4000); ie.Quit(); } catch (Exception ex) { Console.WriteLine("Fatal error: " + ex.Message); Console.ReadLine(); } } // Main() private static void ie_DocumentComplete(object pDisp, ref object URL) { documentComplete.Set(); } } // class Class1 } // ns RunTest
View Code
相关文章推荐
- 《软件测试自动化之道》读书笔记 之 目录导航
- 《软件测试自动化之道》读书笔记 之 请求-响应测试
- 《软件测试自动化之道》读书笔记 之 基于反射的UI测试
- 软件自动化测试流程
- 软件测试自动化解决方案之第一部分(上)(转载)
- 软件开发全过程检测及测试自动化
- 开源软件自动化测试平台
- 软件开发全过程检测及软件测试自动化(转贴)
- [原创]软件自动化测试和.NET中的反射
- Web测试工具对比--自动化软件测试(1)
- 软件开发中的自动化测试
- 浅谈软件测试自动化解决方案(转)
- 软件开发全过程检测及软件测试自动化(转贴)
- 《软件自动化测试开发》出版啦
- [原创]软件自动化测试和.NET中的反射
- 恰当选择软件测试自动化方案2005-09-26 12:06:04
- 软件开发全过程检测及软件测试自动化(转贴)
- [测试自动化]软件自动化测试http://www.cesoo.com/show.php?boardid=school&id=31
- 软件测试自动化的一些具体做法
- 软件测试自动化的成功经验