waitN
来源:互联网 发布:二战日本胜利知乎 编辑:程序博客网 时间:2024/06/05 16:09
WatiN 是一个源自Watir的工具,用于测试Web页面。WatiN表示Web Application Testing in .NET
TestFixture(ApartmentState = ApartmentState.STA)]
public class TestAgreementPage
{
[Test]
public void TestCanAcceptUserAgreement()
{
IE ie = new IE(ConfigurationManager.AppSettings["DefaultPageUrl"]);
ie.TextField("txtName").TypeText("Mohammad Azam");
ie.CheckBox("chkAgree").Checked = true;
ie.Button("btnAgree").Click();
Assert.AreEqual("Valid", ie.Span("lblMessage").Text);
}
}
这个类有TestFixture特性(Attribute),STA值确保该测试运行于STA(Single Threaded Apartment)状态下。这是因为测试代码要加载IE。
WatiN中的IE类完成了主要工作。IE类打开IE,通过name或id来引用html控件。这一行ie.TextField("txtName").TypeText("Mohammad Azam"),引用了id为“txtName”的文本框。浏览器加载后,WatiN会将值“Mohammad Azam”写入id为“txtName”的文本框。这个过程在测试时你会看到的。然后id为“chkAgree”的复选框会被选中。最后,WatiN会按下提交按钮,窗体被提交。
运行测试,失败。因为名为“lblMessage”的Label从未被赋值为“Valid”。加上这段代码:
protected void btnAgree_Click(object sender, EventArgs e)
{
lblMessage.Text = "Valid";
}
现在,如果你运行测试它会通过。但是,好像不太正确。如果我们把这一行测试代码删掉:
ie.CheckBox("chkAgree").Checked = true;
再次运行测试,依然能通过。这可不对!应当只有在CheckBox选中时才可通过。将页面的Code behind代码改一下。
protected void btnAgree_Click(object sender, EventArgs e)
{
if (chkAgree.Checked)
{
lblMessage.Text = "Valid";
}
}
现在,测试只有在CheckBox选中时才可通过了。
以编程方式运行Web服务器:
在上例中我们需要运行WebServer,要么是通过命令行工具,要么是通过运行Web项目。但有时我们需要单元测试项目能够动态打开一个WebServer。一起来看看。
首先,如果你需要打开ASP.NET内部服务器(WebDev.WebServer),可以使用命令行。语法如下:
WebDev.WebServer.exe /port:1950 /path:"C:/Projects/MyWebApplication"
需要定位到WebDev.WebServer所在的目录,默认情况下它在:
C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/WebDev.WebServer.exe
好了,现在来看看如何在单元测试中打开服务器。首先,添加必要的配置(App.config中)。
<configuration>
<appSettings>
<add key="WebServerExePath" value="C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/WebDev.WebServer.exe"/>
<add key="Port" value="4463"/>
<add key="WebApplicationPath" value="c:/projects/demowatiN/demowatiN" />
<add key="DefaultPageUrl" value="http://localhost:4463/Default.aspx" />
</appSettings>
</configuration>
BaseTestPage类可以通过这些信息运行服务器,所有继承了它的测试类都可以使用这个功能了。
下面是BaseTestPage类的完整代码:
public class BaseTestPage
{
static Process server = null;
static BaseTestPage()
{
if (Process.GetProcessesByName("WebDev.WebServer").Length == 0)
{
string webServerExePath = (string)ConfigurationManager.AppSettings["WebServerExePath"];
server = new Process();
Process.Start(webServerExePath, GetWebServerArguments());
}
}
public static string GetWebServerArguments()
{
string args = String.Format("/port:{0} /path:/"{1}/"", GetPort(), GetWebApplicationPath());
if (String.IsNullOrEmpty(args)) throw new ArgumentNullException("Arguments is not defined");
return args;
}
public static string GetPort()
{
string port = ConfigurationManager.AppSettings["Port"] as String;
if (String.IsNullOrEmpty(port)) throw new ArgumentNullException("Port is null or empty");
return port;
}
public static string GetWebApplicationPath()
{
string webApplicationPath = ConfigurationManager.AppSettings["WebApplicationPath"] as String;
if (String.IsNullOrEmpty(webApplicationPath)) throw new ArgumentNullException("WebApplicationPath is null or empty");
return webApplicationPath;
}
}
如果服务器没有运行,我们会新建一个进程运行它,否则就使用已有的进程。GetWebServerArguments()、GetPort()和GetWebApplicationPath()仅仅是辅助方法,可以提高可读性。
最后,你可以让单元测试类继承该类:
public class TestAgreementPage : BaseTestPage
现在,运行单元测试项目时,它会运行WebServer,然后再执行所有测试。
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="DefaultUrl" value="http://localhost" ></add>
<add key="WebApplicationPath" value="C:/Projects/V100/ePayablesSolution/EV" ></add>
<add key="Port" value="80" ></add>
<add key="WebServer" value="C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/WebDev.WebServer.exe" ></add>
</appSettings>
- waitN
- c# WaitN编写自动化测试
- ASP.NE单元测试Mbunit WaitN Gallio
- c# WaitN NUnit实现单元自动化测试
- 取所有的Header、Attribute及Parameter
- QFD(quality function deployment) HOQ (house of quality)使用指南swf文件
- BAD PASSWORD 解决方法
- C#计算字符串中有多少个“a”
- Oracle中查询同一个日期字段,格式不同的问题
- waitN
- 1003 Square Root
- test
- C++虚函数表与对象布局(转)讲的很透彻
- chapter 2 SYSTEM ARCHITECTURE OVERVIEW
- (转)盗梦终端:如何进入第1000层梦境
- 与专家面对面:Android开发入门问与答
- Win7下导入导出mysql数据库
- BUILD-MAX-HEAP(A) P133