创建编码的WebTest
•通常,通过将现有的已记录Web测试转换为编码的Web测试来创建编码的Web测试。记录的Web测试以“Web测试编辑器”中可见的请求树开头。编码的Web测试是一个生成一系列WebTestRequest的.NET类,可以使用C#或Visual Basic编写。可以创建编码的Web测试,但推荐做法是将记录的Web测试转换为编码的Web测试。

•将记录的Web测试转换为编码的Web测试之后,可以像编辑任何其他源代码一样编辑该代码。此外,还可以添加循环和分支结构,动态更改测试中的请求数,以及动态生成测试命中的URL集。

生成的代码如下:
namespace PersonalWebsiteTest
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

    [DataSource("DataSource1", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"E:\\My Documents\\Visual Studio 2005\\" +
        "Test.mdb\";Persist Security Info=False", Microsoft.VisualStudio.TestTools.WebTesting.DataBindingAccessMethod.Sequential, "Customer")]
    public class WebTest1Coded : WebTest
    {
        public WebTest1Coded()
        {
            this.PreAuthenticate = true;
        }
        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
            this.BeginTransaction("Login");
            //Web请求1   
            WebTestRequest request1 = new WebTestRequest("http://localhost:40022/Personal%20Website/");
            ExtractHiddenFields rule1 = new ExtractHiddenFields();
            rule1.ContextParameterName = "1";
            request1.ExtractValues += new EventHandler<ExtractionEventArgs>(rule1.Extract);
            yield return request1;
                        
            WebTestRequest request2 = new WebTestRequest("http://localhost:40022/Personal%20Website/default.aspx");
            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;

            WebTestRequest request3 = new WebTestRequest("http://localhost:40022/Personal%20Website/Resume.aspx");
            request3.ThinkTime = 3;
            yield return request3;

            WebTestRequest request4 = new WebTestRequest("http://localhost:40022/Personal%20Website/Links.aspx");
            request4.ThinkTime = 2;
            yield return request4;

            WebTestRequest request5 = new WebTestRequest("http://localhost:40022/Personal%20Website/Albums.aspx");
            request5.ThinkTime = 3;
            yield return request5;

            WebTestRequest request6 = new WebTestRequest("http://localhost:40022/Personal%20Website/Default.aspx");
            request6.ThinkTime = 1;
            ExtractHiddenFields rule3 = new ExtractHiddenFields();
            rule3.ContextParameterName = "1";
            request6.ExtractValues += new EventHandler<ExtractionEventArgs>(rule3.Extract);
            yield return request6;

            WebTestRequest request7 = new WebTestRequest("http://localhost:40022/Personal%20Website/Default.aspx");
            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;

            this.EndTransaction("Login");
        }
    }
}

生成代码的时机:
•只有在正常的Web测试达到极限后,才能生成进行编码的Web测试。在正常的Web测试中,最明显的限制是循环(您无法多次运行请求的子集)和分支(您无法有条件地执行一组请求)。生成代码的其他原因包括细粒度的事件处理以及以编程方式设置参数值。

•使用图形化的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的更多相关文章

  1. mysql数据库创建编码及排序

    编码 utf-8 排序 utf8_general_ci 不区分大小写,这个你在注册用户名和邮箱的时候就要使用. utf8_general_cs 区分大小写,如果用户名和邮箱用这个 就会照成不良后果 u ...

  2. 转:WebTest的常见问题与解决

    WebTest的常见问题与解决录制好一个WebTest,加上各种规则,编辑后运行并不会像我们想象的那么顺利成功,往往会碰到很多问题,运行不成功的情况比较多,这样我们就遇到了如何解决这些问题的情形.1. ...

  3. 前端学HTTP之实体和编码

    前面的话 每天都有各种媒体对象经由HTTP传送,如图像.文本.影片以及软件程序等.HTTP要确保它的报文被正确传送,识别.提取以及适当处理.为了实现这些目标,HTTP使用了完善的标签来描述承载内容的实 ...

  4. 解析大型.NET ERP系统 单据编码功能实现

    单据编码是ERP系统中必备的功能,用于生成各种单据的流水号,常常借助于日期时间等字符来生成一个唯一的单据号码.从软件的角度来说,就是为生成数据表的主键值(参考编号),从用户的角度来说,就是给业务单据制 ...

  5. 使用VideoToolbox硬编码H.264<转>

    文/落影loyinglin(简书作者)原文链接:http://www.jianshu.com/p/37784e363b8a著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. ======= ...

  6. [SAP ABAP开发技术总结]字符编码与解码、Unicode

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. 通用的业务编码规则设计实现[转:http://www.cnblogs.com/xqin/p/3708367.html]

    一.背景 每一个企业应用中不可避免的都会涉及到业务编码规则的问题,比如订单管理系统中的订单编号,比如商品管理系统中的商品编码,比如项目管理系统中的项目编码等等,这一系列的编码都需要管理起来,那么它们的 ...

  8. HTTP - 内容编码

    HTTP 应用程序有时在发送之前需要对内容进行编码.例如,在把很大的 HTML 文档发送给通过慢速连接上来的客户端之前,服务器可能就会对它进行压缩,这样有助于减少传输实体的时间. 内容编码过程 内容编 ...

  9. 嵌入式 视频编码(H264)

    这几天在编写视频录制模块,所以,闲暇之余,又粗粗的整理了一下,主要是API,以备不时之用    摄像头获取的模拟信号通过经芯片处理(我们使用的是CX25825),将模拟信号转成数字信号,产生标准的IT ...

随机推荐

  1. EXCEL应用:高级筛选里的条件或和与的条件怎么写 例:不包含,包含等

    ============================================================= a列包含b列,在c列中显示b列信息, =INDEX(B:B,MIN(IF(I ...

  2. Javascript和HTML dom

    今天在看DOM那一章的时候突然想到一个问题,众所周知的js的数据类型有两种:原始类型和对象类型.其中原始类型又包括以下几种类型:数字型.字符串型.布尔值.null和undefined.其中对象类型包括 ...

  3. RESTful架构3--开发实战

    转自:REST服务开发实战 如果要说什么是REST的话,那最好先从Web(万维网)说起. 什么是Web呢?读者可以查看维基百科的词条(http://zh.wikipedia.org/zh-cn/Web ...

  4. JavaScript高级程序设计:第三章

    基本概念 一.语法: 1.区分大小写: 2.标识符:指变量.函数.属性的名字,或者函数的参数.标识符可以是按照下列格式规则组合起来的一个或多个字符: (1)第一个字符必须是一个字母.下划线(_).或者 ...

  5. oc汉子转拼音

    oc中可以不使用第三方库直接吧数组转成拼音: 代码如下: NSString *str = @"中国abc人民共和国"; CFStringRef aCFString=(__bridg ...

  6. GsonFormat 报错

    GsonFormat原来也有bug 我是用GsonFormat来生成java bean的,但是运行起来居然报 Caused by: java.lang.NumberFormatException: E ...

  7. PHP问答题大全

    答案在题目后面,文字与背景同色,连续单机三次鼠标一行出答案哦: 1.PHP有几种原始数据类型,分别是什么?答:八种,分别是:int,float,string,bool,array,object,res ...

  8. chrome浏览器调试功能之后端篇

    作为后端开发人员,可能有很多同学不怎么了解chrome调试功能,而即将成为大神的我们,怎么也得会,知其然更要知其所以然,今天我带领大家好好的梳理一下,chrome浏览器调试,个人把它分成了前端功能和后 ...

  9. 注意题目条件!!! 团问题 HDU 5952

    题目大意:团的定义就是,团内的所有点,两两之间各有一条边,团的大小就是点的个数.现给你一个n个点,m条边的图.问,该图中有多少点的个数为s的团. (题目保证每个点的度数不超过20,n<=100, ...

  10. 用GDB调试程序的设置 Segmentation fault(Core Dump)调试

    在写wifi库的时候碰见一个 Segmentation fault(Core Dump) 所以需要用GDB调试下. 在cmake的时候,修改CMakeLists.txt set(CMAKE_C_FLA ...