创建编码的WebTest
?通常,通过将现有的已记录Web测试转换为编码的Web测试来创建编码的Web测试。记录的Web测试以“Web测试编辑器”中可见的请求树开头。编码的Web测试是一个生成一系列WebTestRequest的.NET类,可以使用C#或Visual
Basic编写。可以创建编码的Web测试,但推荐做法是将记录的Web测试转换为编码的Web测试。
if (LastResponse.StatusCode !=
System.Net.HttpStatusCode.OK)
{
WebTestRequest request4 = new
WebTestRequest("http://testserver/website/register.aspx");
request4.ThinkTime = 9;
ExtractHiddenFields
rule2 = new ExtractHiddenFields();
rule2.ContextParameterName = "1";
request4.ExtractValues += new
EventHandler(rule2.Extract);
yield return
request4;
WebTestRequest request5 = new
WebTestRequest("http://testserver/website/register.aspx");
request5.ThinkTime = 5;
request5.Method =
"POST";
FormPostHttpBody request5Body = new
FormPostHttpBody();
request5Body.FormPostParameters.Add("__VIEWSTATE",
this.Context["$HIDDEN1.__VIEWSTATE"].ToString());
request3Body.FormPostParameters.Add("username",
this.Context["DataSource1.Credentials.UserName"].ToString());
request3Body.FormPostParameters.Add("password",
this.Context["DataSource1.Credentials.Password"].ToString());
request5Body.FormPostParameters.Add("confirmpassword",
this.Context["DataSource1.Credentials.Password"].ToString());
request5.Body = request5Body;
yield return
request5;
}
循环
Web
测试执行搜索,然后在搜索结果中连接每个链接。
using System;
using
System.Collections.Generic;
using System.Text;
using
Microsoft.VisualStudio.TestTools.WebTesting;
public class LoopingCoded : WebTest
{
public
LoopingCoded()
{
}
public override IEnumerator GetRequestEnumerator()
{
// Issue a search for the term "Microsoft"
WebTestRequest request7 = new WebTestRequest("http://testserver/website/Search.aspx");
request7.ThinkTime = 20;
request7.Method =
"POST";
FormPostHttpBody request7Body = new
FormPostHttpBody();
request7Body.FormPostParameters.Add("txtSearch",
"Microsoft");
request7.Body =
request7Body;
yield return request7;
// Loop through each anchor tag in the search result and issue a request to each tag‘s target url (href)
foreach (HtmlTag tag in
this.LastResponse.HtmlDocument.GetFilteredHtmlTags("a"))
{
WebTestRequest loopRequest = new
WebTestRequest(tag.GetAttributeValueAsString("href"));
yield return loopRequest;
}
}
}
细粒度的事件处理
Web
测试将两个请求的响应体记录到磁盘,以用于调试和基准调整。
// Log this response out to a
file
WebTestRequest request2 = new WebTestRequest("http://testserver/website/products.aspx");
request2.ThinkTime = 2;
request2.QueryStringParameters.Add("CategoryID", "14", false,
false);
request2.PostRequest += new
EventHandler(request2_PostRequest);
yield return
request2;
WebTestRequest request3 = new WebTestRequest("http://testserver/website/products.aspx");
request3.ThinkTime = 2;
request3.QueryStringParameters.Add("CategoryID", "15", false,
false);
yield return request3;
// Log this response out to a file, too
WebTestRequest request4 = new WebTestRequest("http://testserver/website/products.aspx");
request4.ThinkTime = 1;
request4.QueryStringParameters.Add("CategoryID", "20", false,
false);
request4.PostRequest += new
EventHandler(request4_PostRequest);
yield return
request4;
}
void request2_PostRequest(object sender, PostRequestEventArgs
e)
{
File.WriteAllBytes("c:\\request2.html",
e.Response.BodyBytes);
}
void request4_PostRequest(object sender, PostRequestEventArgs
e)
{
File.WriteAllBytes("c:\\request4.html",
e.Response.BodyBytes);
}
粗粒度:表示类别级,即仅考虑对象的类别(the type
of
object),不考虑对象的某个特定实例。比如,用户管理中,创建、删除,对所有的用户都一视同仁,并不区分操作的具体对象实例。
细粒度:表示实例级,即需要考虑具体对象的实例(the
instance of
object),当然,细粒度是在考虑粗粒度的对象类别之后才再考虑特定实例。比如,合同管理中,列表、删除,需要区分该合同实例是否为当前用户所创建。
一般权限的设计是解决了粗粒度的问题,因为这部分具有通用性,而细粒度可以看成业务部分,因为其具有不确定性。
模拟
Web Javascrīpt
Web 测试引擎工作在
HTTP 层并且不运行 Javascrīpt。以影响 HTTP 层的方式依赖于 Javascrīpt 的 Web 站点,可使用编码的 Web 测试模拟通常由
Javascrīpt 执行的逻辑。例如下面一段JS
<scrīpt type="text/javascrīpt">
//get rid
of some common non-digit characters
// in the phone number string function
FixPhoneNumber()
{
var phoneNumberElement =
document.getElementById(‘phoneNumber‘);
var number =
phoneNumberElement.value;
number = number.replace(‘-‘, ‘‘);
number =
number.replace(‘(‘, ‘‘);
number = number.replace(‘)‘, ‘‘);
number =
number.replace(‘ ‘, ‘‘);
phoneNumberElement.value = number;
return
true;
}
<div>
<input name="phoneNumber" id="phoneNumber"
type="text"/>
<input name="submit" type="submit" value="Submit"
ōnclick="FixPhoneNumber()" />
</div>
通过编程后
public override
IEnumerator GetRequestEnumerator()
{
WebTestRequest request1 = new
WebTestRequest("http://testserver/testwebsite/default.aspx");
yield return request1;
WebTestRequest request2 = new
WebTestRequest("http://testserver/testwebsite/showparameters.aspx");
request2.Method = "POST";
FormPostHttpBody
request2Body = new
FormPostHttpBody();
//get the databound phone number from the context
and
// strip out certain characters to
simulate Javascrīpt
string phoneNumber =
this.Context["PhoneNumber"].ToString();
phoneNumber = phoneNumber.Replace("-", "");
phoneNumber = phoneNumber.Replace("(", "");
phoneNumber = phoneNumber.Replace(")", "");
phoneNumber = phoneNumber.Replace(" ", "");
request2Body.FormPostParameters.Add("phoneNumber",
phoneNumber);
request2Body.FormPostParameters.Add("submit",
"Submit");
request2.Body =
request2Body;
yield return
request2;
}
运行并调试编码的Web
测试
对WebTest编程后,需要运行编码的WebTest,验证是否通过
在 Test View 或 Test Manager
窗口中选择该测试,并单击 Run 工具栏按钮
转:创建编码的WebTest,码迷,mamicode.com
原文地址:http://www.cnblogs.com/lci05/p/3700069.html