.NET2005下创建Web测试(C#)

    技术2022-05-11  53

    简单的介绍一下。

    1,创建一个ASP.NET(2.0) 的工程,如下图所示,点击不同按钮,会在文本框中列出所按按钮的内容。

    <script type="text/javascript"> var objmain = document.getElementById("main"); function updatesize() { var bodyw = window.document.body.offsetWidth; if(bodyw <= 790) objmain.style.width="772px"; else if(bodyw >= 1016) objmain.style.width="996px"; else objmain.style.width="100%"; updateimgs(); } function getWidth(ele){ var image=new Image(); image.src=ele.src; return image.width; } function updateimgs() { if(!document.all) { var objimg = document.getElementById("imagee"); var iwidth=getWidth(objimg); if(iwidth>700) { objimg.style.width= "100%"; } } else { var objmains = document.getElementById("main"); var bodyw = window.document.body.offsetWidth; var mainw = objmains.style.width; var objimg = document.getElementById("imagee"); var iwidth=getWidth(objimg); var widths=0; if(mainw=="100%") { var widths=bodyw-100; } else { var w=mainw.substring(0,mainw.lastIndexOf("px")); var ww=parseInt(w); var widths=ww-100; } if(iwidth>widths) objimg.width=widths; } } if(document.all) { updatesize(); window.onresize = updatesize; } </script>  

    具体后代码如下:

     前台

     

    <% @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html  xmlns ="http://www.w3.org/1999/xhtml"   > < head  runat ="server" >      < title > 无标题页 </ title > </ head > < body >      < form  id ="form1"  runat ="server" >      < div >          < asp:Button  ID ="Button1"  runat ="server"  Style ="left: 261px; position: relative; top: 39px"             Text ="C"  OnClick ="Button1_Click"   />          < asp:Button  ID ="Button2"  runat ="server"  Style ="left: 239px; position: relative; top: 78px"             Text ="B"  OnClick ="Button2_Click"   />          < asp:Button  ID ="Button3"  runat ="server"  Style ="left: 218px; position: relative; top: 114px"             Text ="A"  OnClick ="Button3_Click"   />          < asp:TextBox  ID ="TextBox1"  runat ="server"  Style ="left: 136px; position: relative;            top: 163px" ></ asp:TextBox ></ div >      </ form > </ body > </ html >

     

    后台

    using  System; using  System.Data; using  System.Configuration; using  System.Web; using  System.Web.Security; using  System.Web.UI; using  System.Web.UI.WebControls; using  System.Web.UI.WebControls.WebParts; using  System.Web.UI.HtmlControls; public   partial   class  _Default : System.Web.UI.Page  {    protected void Page_Load(object sender, EventArgs e)    {    }    protected void Button3_Click(object sender, EventArgs e)    {        this.TextBox1.Text = Button3.Text;    }    protected void Button2_Click(object sender, EventArgs e)    {        this.TextBox1.Text = Button2.Text;    }    protected void Button1_Click(object sender, EventArgs e)    {        this.TextBox1.Text = Button1.Text;    }}

    第二 编译工程,并运行,记住工程Web地址。如图所示

    第三 创建WEB测试工程,点击新建测试

    创建web测试

    确定之后回弹出下图,但是地址栏中没有内容,把之前工程的web地址填写。结束记录web操作按停止。

    之后会在工程中生成下图,现在可以运行测试了。 <script type="text/javascript"> var objmain = document.getElementById("main"); function updatesize() { var bodyw = window.document.body.offsetWidth; if(bodyw <= 790) objmain.style.width="772px"; else if(bodyw >= 1016) objmain.style.width="996px"; else objmain.style.width="100%"; updateimgs(); } function getWidth(ele){ var image=new Image(); image.src=ele.src; return image.width; } function updateimgs() { if(!document.all) { var objimg = document.getElementById("imagee"); var iwidth=getWidth(objimg); if(iwidth>700) { objimg.style.width= "100%"; } } else { var objmains = document.getElementById("main"); var bodyw = window.document.body.offsetWidth; var mainw = objmains.style.width; var objimg = document.getElementById("imagee"); var iwidth=getWidth(objimg); var widths=0; if(mainw=="100%") { var widths=bodyw-100; } else { var w=mainw.substring(0,mainw.lastIndexOf("px")); var ww=parseInt(w); var widths=ww-100; } if(iwidth>widths) objimg.width=widths; } } if(document.all) { updatesize(); window.onresize = updatesize; } </script> <script type="text/javascript"> var objmain = document.getElementById("main"); function updatesize() { var bodyw = window.document.body.offsetWidth; if(bodyw <= 790) objmain.style.width="772px"; else if(bodyw >= 1016) objmain.style.width="996px"; else objmain.style.width="100%"; updateimgs(); } function getWidth(ele){ var image=new Image(); image.src=ele.src; return image.width; } function updateimgs() { if(!document.all) { var objimg = document.getElementById("imagee"); var iwidth=getWidth(objimg); if(iwidth>700) { objimg.style.width= "100%"; } } else { var objmains = document.getElementById("main"); var bodyw = window.document.body.offsetWidth; var mainw = objmains.style.width; var objimg = document.getElementById("imagee"); var iwidth=getWidth(objimg); var widths=0; if(mainw=="100%") { var widths=bodyw-100; } else { var w=mainw.substring(0,mainw.lastIndexOf("px")); var ww=parseInt(w); var widths=ww-100; } if(iwidth>widths) objimg.width=widths; } } if(document.all) { updatesize(); window.onresize = updatesize; } </script> 测试通过。web测试也可有生成原码,我已经在图中标示,但是大家要注意程序中泛式的使用。 // ------------------------------------------------------------------------------ //  <auto-generated> //      此代码由工具生成。 //      运行库版本:2.0.50727.42 // //      对此文件的更改可能会导致不正确的行为,并且如果 //      重新生成代码,这些更改将会丢失。 //  </auto-generated> // ------------------------------------------------------------------------------ namespace  TestProject1 {    using System;    using System.Collections.Generic;    using System.Text;    using Microsoft.VisualStudio.TestTools.WebTesting;    using Microsoft.VisualStudio.TestTools.WebTesting.Rules;    public class WebTest1Coded : WebTest    {        public WebTest1Coded()        {            this.PreAuthenticate = true;        }        public override IEnumerator<WebTestRequest> GetRequestEnumerator()        {            WebTestRequest request1 = new WebTestRequest("http://localhost:4298/WebSite3/Default.aspx");            request1.ThinkTime = 3;            request1.Encoding = System.Text.Encoding.GetEncoding("gb2312");            ExtractHiddenFields rule1 = new ExtractHiddenFields();            rule1.ContextParameterName = "1";            request1.ExtractValues += new EventHandler<ExtractionEventArgs>(rule1.Extract);            yield return request1;            WebTestRequest request2 = new WebTestRequest("http://localhost:4298/WebSite3/Default.aspx");            request2.ThinkTime = 1;            request2.Method = "POST";            FormPostHttpBody request2Body = new FormPostHttpBody();            request2Body.FormPostParameters.Add("__VIEWSTATE"this.Context["$HIDDEN1.__VIEWSTATE"].ToString());            request2Body.FormPostParameters.Add("Button1""C");            request2Body.FormPostParameters.Add("TextBox1""");            request2Body.FormPostParameters.Add("__EVENTVALIDATION"this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());            request2.Body = request2Body;            ExtractHiddenFields rule2 = new ExtractHiddenFields();            rule2.ContextParameterName = "1";            request2.ExtractValues += new EventHandler<ExtractionEventArgs>(rule2.Extract);            yield return request2;            WebTestRequest request3 = new WebTestRequest("http://localhost:4298/WebSite3/Default.aspx");            request3.ThinkTime = 1;            request3.Method = "POST";            FormPostHttpBody request3Body = new FormPostHttpBody();            request3Body.FormPostParameters.Add("__VIEWSTATE"this.Context["$HIDDEN1.__VIEWSTATE"].ToString());            request3Body.FormPostParameters.Add("Button2""B");            request3Body.FormPostParameters.Add("TextBox1""C");            request3Body.FormPostParameters.Add("__EVENTVALIDATION"this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());            request3.Body = request3Body;            ExtractHiddenFields rule3 = new ExtractHiddenFields();            rule3.ContextParameterName = "1";            request3.ExtractValues += new EventHandler<ExtractionEventArgs>(rule3.Extract);            yield return request3;            WebTestRequest request4 = new WebTestRequest("http://localhost:4298/WebSite3/Default.aspx");            request4.Method = "POST";            FormPostHttpBody request4Body = new FormPostHttpBody();            request4Body.FormPostParameters.Add("__VIEWSTATE"this.Context["$HIDDEN1.__VIEWSTATE"].ToString());            request4Body.FormPostParameters.Add("Button3""A");            request4Body.FormPostParameters.Add("TextBox1""B");            request4Body.FormPostParameters.Add("__EVENTVALIDATION"this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());            request4.Body = request4Body;            yield return request4;        }    }}

    最新回复(0)