转:创建编码的WebTest
创建编码的WebTest
•通常,通过将现有的已记录Web测试转换为编码的Web测试来创建编码的Web测试。记录的Web测试以“Web测试编辑器”中可见的请求树开头。编码的Web测试是一个生成一系列WebTestRequest的.NET类,可以使用C#或Visual Basic编写。可以创建编码的Web测试,但推荐做法是将记录的Web测试转换为编码的Web测试。
生成的代码如下:
namespace PersonalWebsiteTest
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
"Test.mdb\";Persist Security Info=False", Microsoft.VisualStudio.TestTools.WebTesting.DataBindingAccessMethod.Sequential, "Customer")]
public class WebTest1Coded : WebTest
{
{
this.PreAuthenticate = true;
}
{
this.BeginTransaction("Login");
//Web请求1
ExtractHiddenFields rule1 = new ExtractHiddenFields();
rule1.ContextParameterName = "1";
request1.ExtractValues += new EventHandler<ExtractionEventArgs>(rule1.Extract);
yield return request1;
request2.ThinkTime = 14;
request2.Method = "POST";
FormPostHttpBody request2Body = new FormPostHttpBody();
request2Body.FormPostParameters.Add("__EVENTTARGET", "");
request2Body.FormPostParameters.Add("__EVENTARGUMENT", "");
request2Body.FormPostParameters.Add("__VIEWSTATE", this.Context["$HIDDEN1.__VIEWSTATE"].ToString());
request2Body.FormPostParameters.Add("ctl00$Main$LoginArea$Login1$UserName", "oscarxie");
request2Body.FormPostParameters.Add("ctl00$Main$LoginArea$Login1$Password", "hae867@!");
request2Body.FormPostParameters.Add("__EVENTVALIDATION", this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());
request2Body.FormPostParameters.Add("ctl00$Main$LoginArea$Login1$LoginButton.x", "110");
request2Body.FormPostParameters.Add("ctl00$Main$LoginArea$Login1$LoginButton.y", "8");
request2.Body = request2Body;
if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.High))
{
ValidationRuleFindText rule2 = new ValidationRuleFindText();
rule2.FindText = "oscarxie";
rule2.IgnoreCase = false;
rule2.UseRegularExpression = false;
rule2.PassIfTextFound = true;
request2.ValidateResponse += new EventHandler<ValidationEventArgs>(rule2.Validate);
}
yield return request2;
request3.ThinkTime = 3;
yield return request3;
request4.ThinkTime = 2;
yield return request4;
request5.ThinkTime = 3;
yield return request5;
request6.ThinkTime = 1;
ExtractHiddenFields rule3 = new ExtractHiddenFields();
rule3.ContextParameterName = "1";
request6.ExtractValues += new EventHandler<ExtractionEventArgs>(rule3.Extract);
yield return request6;
request7.Method = "POST";
FormPostHttpBody request7Body = new FormPostHttpBody();
request7Body.FormPostParameters.Add("__EVENTTARGET", "ctl00$LoginStatus1$ctl00");
request7Body.FormPostParameters.Add("__EVENTARGUMENT", "");
request7Body.FormPostParameters.Add("__VIEWSTATE", this.Context["$HIDDEN1.__VIEWSTATE"].ToString());
request7Body.FormPostParameters.Add("__EVENTVALIDATION", this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());
request7.Body = request7Body;
yield return request7;
}
}
}
生成代码的时机:
•只有在正常的Web测试达到极限后,才能生成进行编码的Web测试。在正常的Web测试中,最明显的限制是循环(您无法多次运行请求的子集)和分支(您无法有条件地执行一组请求)。生成代码的其他原因包括细粒度的事件处理以及以编程方式设置参数值。
可以参考文章:Web 测试的创作与调试技术
分支
Web 测试使用数据绑定的用户凭据登录到 Web 站点,如果用户在该系统中不存在,必须创建一个新用户帐户。
// If the login failed, create a new user account
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的更多相关文章
- mysql数据库创建编码及排序
编码 utf-8 排序 utf8_general_ci 不区分大小写,这个你在注册用户名和邮箱的时候就要使用. utf8_general_cs 区分大小写,如果用户名和邮箱用这个 就会照成不良后果 u ...
- 转:WebTest的常见问题与解决
WebTest的常见问题与解决录制好一个WebTest,加上各种规则,编辑后运行并不会像我们想象的那么顺利成功,往往会碰到很多问题,运行不成功的情况比较多,这样我们就遇到了如何解决这些问题的情形.1. ...
- 前端学HTTP之实体和编码
前面的话 每天都有各种媒体对象经由HTTP传送,如图像.文本.影片以及软件程序等.HTTP要确保它的报文被正确传送,识别.提取以及适当处理.为了实现这些目标,HTTP使用了完善的标签来描述承载内容的实 ...
- 解析大型.NET ERP系统 单据编码功能实现
单据编码是ERP系统中必备的功能,用于生成各种单据的流水号,常常借助于日期时间等字符来生成一个唯一的单据号码.从软件的角度来说,就是为生成数据表的主键值(参考编号),从用户的角度来说,就是给业务单据制 ...
- 使用VideoToolbox硬编码H.264<转>
文/落影loyinglin(简书作者)原文链接:http://www.jianshu.com/p/37784e363b8a著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. ======= ...
- [SAP ABAP开发技术总结]字符编码与解码、Unicode
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 通用的业务编码规则设计实现[转:http://www.cnblogs.com/xqin/p/3708367.html]
一.背景 每一个企业应用中不可避免的都会涉及到业务编码规则的问题,比如订单管理系统中的订单编号,比如商品管理系统中的商品编码,比如项目管理系统中的项目编码等等,这一系列的编码都需要管理起来,那么它们的 ...
- HTTP - 内容编码
HTTP 应用程序有时在发送之前需要对内容进行编码.例如,在把很大的 HTML 文档发送给通过慢速连接上来的客户端之前,服务器可能就会对它进行压缩,这样有助于减少传输实体的时间. 内容编码过程 内容编 ...
- 嵌入式 视频编码(H264)
这几天在编写视频录制模块,所以,闲暇之余,又粗粗的整理了一下,主要是API,以备不时之用 摄像头获取的模拟信号通过经芯片处理(我们使用的是CX25825),将模拟信号转成数字信号,产生标准的IT ...
随机推荐
- CSS3秘笈:第十一章
表格和表单的格式化 1.表格的各种标签提供了许多有用的“钩子”,可以再上面挂CSS样式.如果创建了<th>标签样式,那么每一个列的标题——<th>标签——看起来就有可能与其他的 ...
- 交互式shell和非交互式shell的区别
交互式模式就是shell等待你的输入,并且执行你提交的命令.这种模式被称作交互式是因为shell与用户进行交互.这种模式也是大多数用户非常熟悉的:登录.执行一些命令.签退.当你签退后,shell也终止 ...
- regular expression tutorial
\d represent any number \D represents everything but a number \s represents any space \S Anything bu ...
- JavaScript学习总结(十七)——Javascript原型链的原理
一.JavaScript原型链 ECMAScript中描述了原型链的概念,并将原型链作为实现继承的主要方法.其基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.在JavaScript中, ...
- MySQL事件【转载】
在系统管理或者数据库管理中,经常要周期性的执行某一个命令或者SQL语句.对于linux系统熟悉的人都知道linux的cron计划任务,能很方便地实现定期运行指定命令的功能.Mysql在5.1以后推出了 ...
- mongodb状态
基本信息 spock:PRIMARY>db.serverStatus() { "host" :"h6.corp.yongche.org", //主机名 & ...
- python 邮件报警
为了以后方便使用邮件报警 我这边直接写入一推报警模板 方便以后使用 加入模块 import smtplib from email.mime.text import MIMEText from emai ...
- 两种方法将oracle数据库中的一张表的数据导入到另外一个oracle数据库中
oracle数据库实现一张表的数据导入到另外一个数据库的表中的方法有很多,在这介绍两个. 第一种,把oracle查询的数据导出为sql文件,执行sql文件里的insert语句,如下: 第一步,导出sq ...
- Object.setPrototypeOf 方法的使用
将一个指定的对象的原型设置为另一个对象或者null(既对象的[[Prototype]]内部属性). 语法 Object.setPrototypeOf(obj, prototype) 参数 obj 将被 ...
- hdu1950 Bridging signals 最长递增子序列
用一个数组记下递增子序列长度为i时最小的len[i],不断更新len数组,最大的i即为最长递增子序列的长度 #include<cstdio> #include<algorithm&g ...