用Visual Studio做自动化测试

 

So you want to replay an IIS web server log?

A few months ago, a group in Microsoft wanted to be able to play back a large IIS log as a Visual Studio web test.  They started off with a converter that converted the IIS log into a gigantic coded web test.  The 118MB .cs file that resulted was a bit ridiculous and didn't perform very well at design time or run time.

I took a different approach by reading the IIS log from within the web test.  It depends on the the handy LogReader 2.2 download to handle all the log parsing and keep the code short and simple.

Here's a sample WebTest that plays back an IIS log:

public class IISLogCodedWebTest : WebTest
{
    public IISLogCodedWebTest()
    {
        this.PreAuthenticate = true;
    }

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    {
        IISLogReader reader = new IISLogReader(@"d:\download\ex060209.log");
        foreach (WebTestRequest request in reader.GetRequests())
        {
            yield return request;
        }
    }
}

The code for the IISLogReader class used above is below:

using System;
using System.Collections.Generic;
using System.Text;
using MSUtil;
using LogQuery = MSUtil.LogQueryClassClass;
using IISLogInputFormat = MSUtil.COMIISW3CInputContextClassClass;
using LogRecordSet = MSUtil.ILogRecordset;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace IISLogToWebTest
{
    public class IISLogReader
    {
        private string _iisLogPath;

        public IISLogReader(string iisLogPath)
        {
            _iisLogPath = iisLogPath;
        }

        public IEnumerable<WebTestRequest> GetRequests()
        {
            LogQuery logQuery = new LogQuery();
            IISLogInputFormat iisInputFormat = new IISLogInputFormat();

            string query = @"SELECT s-ip, s-port, cs-method, cs-uri-stem, cs-uri-query FROM " + _iisLogPath;

            LogRecordSet recordSet = logQuery.Execute(query, iisInputFormat);
            while (!recordSet.atEnd())
            {
                ILogRecord record = recordSet.getRecord();
                if (record.getValueEx("cs-method").ToString() == "GET")
                {
                    string server = record.getValueEx("s-ip").ToString();
                    string path = record.getValueEx("cs-uri-stem").ToString();
                    string querystring = record.getValueEx("cs-uri-query").ToString();

                    StringBuilder urlBuilder = new StringBuilder();
                    urlBuilder.Append("http://");
                    urlBuilder.Append(server);
                    urlBuilder.Append(path);
                    if (!String.IsNullOrEmpty(querystring))
                    {
                        urlBuilder.Append("?");
                        urlBuilder.Append(querystring);
                    }

                    WebTestRequest request = new WebTestRequest(urlBuilder.ToString());
                    yield return request;
                }

                recordSet.moveNext();
            }
 
            recordSet.close();
        }
    }
}

Let me know if you find this useful or if you have any problems.

 

Better HTML parsing and validation with HtmlAgilityPack

Let's face it; sometimes the Microsoft.VisualStudio.TestTools.WebTesting.HtmlDocument class just doesn't cut it when you're writing custom extraction and validation code.  HtmlDocument was originally designed as an internal class to very efficiently parse URLs for dependent requests (such as images) out of HTML response bodies.  Before VS 2005 RTM, we made HtmlDocument part of the public WebTestFramework API, but scheduling and resource constraints prevented us from adding more general purpose DOM features like InnerHtml, InnerText, and GetElementById.  You could always parse the HTML string yourself, but fortunately there's a better option: HtmlAgilityPack.

HtmlAgilityPack is an open source project on CodePlex.  It provides standard DOM APIs and XPath navigation -- even when the HTML is not well-formed!

Here's a sample web test that uses the HtmlAgilityPack.HtmlDocument instead of the one in WebTestFramework.  It simply validates that Microsoft's home page lists Windows as the first item in the navigation sidebar.  Download HtmlAgilityPack and add a reference to it from your test project to try out this coded web test.

 

using System;

 

using System.Collections.Generic;

 

using System.Text;

 

using Microsoft.VisualStudio.TestTools.WebTesting;

 

using HtmlAgilityPack;

 

public class WebTest1Coded : WebTest

 

{

public override IEnumerator<WebTestRequest> GetRequestEnumerator()

{

WebTestRequest request1 = new WebTestRequest("http://www.microsoft.com/");

request1.ValidateResponse += new EventHandler<ValidationEventArgs>(request1_ValidateResponse);

yield return request1;

}

void request1_ValidateResponse(object sender, ValidationEventArgs e)

{

//load the response body string as an HtmlAgilityPack.HtmlDocument

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

doc.LoadHtml(e.Response.BodyString);

//locate the "Nav" element

HtmlNode navNode = doc.GetElementbyId("Nav");

//pick the first <li> element

HtmlNode firstNavItemNode = navNode.SelectSingleNode(".//li");

//validate the first list item in the Nav element says "Windows"

e.IsValid = firstNavItemNode.InnerText == "Windows";

}

}


转载于:https://www.cnblogs.com/starspace/archive/2008/10/17/1313698.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值