《软件测试自动化之道》读书笔记 之 底层的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控件。

图1 Web UAT

待测程序代码:

 using System;
using System.Collections.Generic; using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
private System.Collections.ArrayList al = new System.Collections.ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
Product p1 = new Product("widgets", "1A11A", 11.11);
Product p2 = new Product("gadgets", "2B22B", 22.22);
Product p3 = new Product("foozles", "3C33C", 33.33); al.Add(p1);
al.Add(p2);
al.Add(p3);
Label3.Text = "Search Complete";
Label3.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
string filter = TextBox1.Text;
ListBox1.Items.Add("ProdName ProdID Price");
ListBox1.Items.Add("====================="); if (RadioButtonList1_1.Checked)
{
foreach (Product p in al)
{
if (p.name.IndexOf(filter) >= )
ListBox1.Items.Add(p.name + ", " + p.id + ", " + p.price);
}
}
else if (RadioButtonList1_2.Checked)
{
foreach (Product p in al)
{
if (p.id.IndexOf(filter) >= )
ListBox1.Items.Add(p.name + ", " + p.id + ", " + p.price);
}
}
Label3.Visible = true;
} public class Product
{
public string name;
public string id;
public double price;
public Product(string name, string id, double price)
{
this.name = name;
this.id = id;
this.price = price;
}
}
}

测试程序

启动IE并连接到这个实例

         static void Main(string[] args)
{
//... SHDocVw.InternetExplorer ie = null;
Console.WriteLine("\nLaunching an instance of IE");
Process p = Process.Start("iexplore.exe", "about:blank");
System.Threading.Thread.Sleep();
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 == )
throw new Exception("Cannot find IE"); Console.WriteLine("Attaching to IE");
int i = ; 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"); //...
}

如何判断待测web程序完全加载到浏览器

         static void Main(string[] args)
{
SHDocVw.InternetExplorer ie = null;
//把ie对象连接到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(); //...
}
private static void ie_DocumentComplete(object pDisp, ref object URL)
{
documentComplete.Set();
}

测试程序有可能在待测程序尚未完全加载的情况下试图对其进行操作,这么做,很可能引发异常。测试程序可通过事件DocumentComplete和AutoResetEvent类,对自动化程序进行同步。

AutoResetEvent类还有带参数的WaitOne()方法,用于指定最大等待时间,超过就不再等待:

//9000毫秒,true标识可以再等待结束之前推出同步域
documentComplete.WaitOne(,true)

操纵并检查IE Shell

         static void Main(string[] args)
{
SHDocVw.InternetExplorer ie = null;
//把ie对象连接到IE程序所在的进行 Console.WriteLine("Setting IE to size 450x360");
ie.Width = ;
ie.Height = ;
Thread.Sleep(); if (ie.StatusText.IndexOf("Done") == -)
Console.WriteLine("could not find 'Done' in status bar");
else
Console.WriteLine("Find 'Done' in status bar");
//...
}

当编写针对Web UI的自动化测试程序师,需要把IE的3个区域考虑在内:

  • 客户区域,即待测页面所在区域;
  • Shell区域,即诸如地址栏和回退按钮等IE控件所在的区域;
  • 以及其他窗口,比如alert对话框等,这些窗口与IE是分开的。

SHDocVw.InternetExplorer对象提供了一些属性和方法用于操纵(可用来模拟用户操作)和检查Shell(可用来判断某个测试场景通过与否)。下面是几种属性和方法:

  • GoBack(), GoForward(), GoHome(), Refresh(), Quit();
  • Height, Width: 设置IE外壳的高度和宽度(以像素为单位)。
  • Top, Left: 设置IE外壳左上角的位置(以像素为单位)。
  • FullScreen:如果IE在全屏模式下运行,则返回true。
  • MenuBar:如果IE菜单栏可见,则返回true。
  • Resizable:如果可以调整IE的大小,则返回true。
  • LocationURL:返回IE当前显示页面的URL。
  • StatusText:返回IE状态栏的文本。

操作待测Web页面上的HTML元素的值

示例代码:

using mshtml;  // .NET component = Microsoft.mshtml. HTML interfaces
static void Main(string[] args)
{
//...
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");
textBox.value = "2B"; Console.WriteLine("Clicking search button");
HTMLInputElement butt = (HTMLInputElement)theDoc.getElementById("Button1");
butt.click(); documentComplete.WaitOne(); //...
}

在上述代码中,要模拟用户选中radio button控件,必须使用@checked,因为checked是C#语言的一个关键字。

在上述代码中,按钮控件和文本控件的类型都是HTMLInputElement,而下拉空间的类型则是HTMLSelectElement。可通过下面代码知道到底该用哪个类:

HTMLInputElement textBox = (HTMLInputElement)theDoc.getElementById("TextBox1");
Console.WriteLine("The textbox has type" + textBox.GetType().ToString());

注意:因为我们是通过getElementById()方法获得HTML元素/控件的引用的,所以这个控件必须要有ID attribute,从而可以唯一的表示这个控件。若Web页面是由Visual Studio .NET UI设计器创建的,那么所有控件都有一个ID attribute。但是如果Web程序是手工创建(比如通过Notepad),那么最好修改这个程序,给他们加上一个ID attribute。

验证Web页面上HTML元素

通过mshtml.dll库里的getElementByTagName()方法和item()方法得到你想要的特定的元素。然后可以通过InnerText属性取回HTML元素的实际值。

设想某个待测页面含有几个<P>元素,和一个ID为“div2”的<div>元素。下面的代码在<p>元素中查找“aloha”,在<div>元素中查找"adios":

                 Console.WriteLine("Seek 'aloha' in <p>[2]");
Console.WriteLine("<p> type is:" + theDoc.getElementsByTagName("p").GetType().ToString());
HTMLParaElement paraElement = (HTMLParaElement)theDoc.getElementsByTagName("p").item(, null);
if (paraElement.innerText.ToString().IndexOf("aloha") >= )
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") >= )
Console.WriteLine("Found target 'adios'");
else
Console.WriteLine("Target string not found");

上述代码中,item()方法:

  • 第一个参数可以为整数(整数时为从0开始的索引值)或字符串(字符串时为tag名字);
  • 第二个参数是个索引值,但只有当item()返回得是一个集合时才用得到。

有时候这些元素的值并不属于任何子的HTML元素,可用以下代码解决:

                 // non-HTML element
Console.WriteLine("Seeking 'Search Complete' in body");
HTMLBody body = (HTMLBody)theDoc.getElementsByTagName("body").item(, null);
if (body.createTextRange().findText("Search Complete", , ) == true)
{
Console.WriteLine("Found target string");
}
else
{
Console.WriteLine("*Target string not found*");
pass = false;
}

上述代码中,findText()方法:

  • 第一个参数是必添的目标字符串
  • 第二个参数用于制定查找起始位置
  • 第二个参数用于指定查找类型

示例代码

 // 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();
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 == )
throw new Exception("Cannot find IE"); Console.WriteLine("Attaching to IE");
int i = ; 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 = ;
ie.Height = ;
Thread.Sleep(); //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(, null);
if (paraElement.innerText.ToString().IndexOf("aloha") >= )
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") >= )
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(, null);
if (body.createTextRange().findText("Search Complete", , ) == 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();
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

《软件测试自动化之道》读书笔记 之 底层的Web UI 测试的更多相关文章

  1. 《软件测试自动化之道》读书笔记 之 基于Windows的UI测试

    <软件测试自动化之道>读书笔记 之 基于Windows的UI测试 2014-09-25 测试自动化程序的任务待测程序测试程序  启动待测程序  获得待测程序主窗体的句柄  获得有名字控件的 ...

  2. 《软件测试自动化之道》读书笔记 之 基于反射的UI测试

    <软件测试自动化之道>读书笔记 之 基于反射的UI测试 2014-09-24 测试自动化程序的任务待测程序测试程序  启动待测程序  设置窗体的属性  获取窗体的属性  设置控件的属性  ...

  3. 《软件测试自动化之道》读书笔记 之 XML测试

    <软件测试自动化之道>读书笔记 之 XML测试 2014-10-07 待测程序测试程序  通过XmlTextReader解析XML  通过XmlDocument解析XML  通过XmlPa ...

  4. 《软件测试自动化之道》读书笔记 之 SQL 存储过程测试

    <软件测试自动化之道>读书笔记 之 SQL 存储过程测试 2014-09-28 待测程序测试程序   创建测试用例以及测试结果存储  执行T-SQL脚本  使用BCP工具导入测试用例数据  ...

  5. 《黑客攻防技术宝典Web实战篇@第2版》读书笔记1:了解Web应用程序

    读书笔记第一部分对应原书的第一章,主要介绍了Web应用程序的发展,功能,安全状况. Web应用程序的发展历程 早期的万维网仅由Web站点构成,只是包含静态文档的信息库,随后人们发明了Web浏览器用来检 ...

  6. 【哲学角度看软件测试】要想软件“一想之美”,UI 测试少不了

    摘要:软件测试的最高层次需求是:UI测试,也就是这个软件"长得好不好看". 为了让读者更好地理解测试,我们从最基础的概念开始介绍.以一个软件的"轮回"为例,下图 ...

  7. <<google软件测试之道>>读书笔记

    以前一直从开发的角度来看待测试,看完这本书以后感觉错了,难怪之前公司的测试一直搭建不起来 1.开发人员,开发测试人员,测试人员 * 开发人员负责开发 * 开发测试人员近距离接触代码,负责编写测试用例, ...

  8. <微软的软件测试之道>读书笔记3

    一.自动化的标准步骤: 1.环境初始化,并检查环境是否处于正确的状态,能否开始测试 2.执行步骤 3.判断结果,并将结果保存到其它地方以供检查分析 4.环境清理,清理本用例产生的垃圾(临时文件.环境变 ...

  9. VC++编程之道读书笔记(2)

    第三篇 技术细节 第七章:细说开发人员必知必会的39个开发细节 细节36:单例模式的应用 在开发程序时,往往需要在整个工程中只需要一个类的实例.而这个实例一旦被创建就不能被其他的实例再创建了,通常我们 ...

随机推荐

  1. 文件流 io.StringIo()

    import io f = io.StringIO() f.write("") f.getvalue() f.close 二进制 f = io.Bytesio()

  2. curl 文件上传

    curl_file_create (带路径的文件名 [, 文件mimetype , 上传数据里的文件名] ) ; new cURLFile (带路径的文件名 [, 文件mimetype , 上传数据里 ...

  3. SQL SERVER字符串中的空格去除

    1.LTRIM 删除起始空格后返回字符表达式. 语法 LTRIM   (   character_expression   ) 参数 character_expression 是字符或二进制数据表达式 ...

  4. IT 产品 需求 痛点

    英文应该有个 汉语发音 标注 这样的平台软件.罗马音.

  5. 10.29 正睿停课训练 Day11

    目录 2018.10.29 正睿停课训练 Day11 A 线段树什么的最讨厌了(思路 DFS) B 已经没有什么好害怕的了(差分 前缀和) C 我才不是萝莉控呢(DP 贪心 哈夫曼树) 考试代码 A ...

  6. 洛谷.1110.[ZJOI2007]报表统计(Multiset)

    题目链接 主要思路 /* 其实只需要multiset即可 对于询问1,删除.插入差值,输出最小元素 对于询问2,插入后用前驱后继更新 1.注意哨兵元素 2.注意multiset中删除时是删除某元素的一 ...

  7. nginx编译安装on mac

    一.编译安装模块 如果是原有包中就有的模块,编译时, ./configure --with-xxx 就可以, 如果是第三方模块,可使用 --add-module, 如果有多个模块的话,只需要多次使用- ...

  8. Go语言第一深坑:interface 与 nil 的比较

    interface简介 Go 语言以简单易上手而著称,它的语法非常简单,熟悉 C++,Java 的开发者只需要很短的时间就可以掌握 Go 语言的基本用法. interface 是 Go 语言里所提供的 ...

  9. linux命令行下修改系统时间、时区

    date查看时间以及时区 图a是est时区,和HONGkong时间查了一个小时. # 保存设置$ sudo mv /etc/localtime /etc/localtime.old # 设置时区 $ ...

  10. foxmail同步QQ邮箱里的所有文件夹

    随着微信消息的轰炸,我决定重拾邮箱.为了方便管理邮箱,我下载并试用了Foxmail和网易邮箱大师,Foxmail不确定用什么语言开发的,可能是C++或者Delphi(早期应该是Delphi,最新的版本 ...