<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>进化的测试 &#187; WebTest</title>
	<atom:link href="http://magustest.com/blog/tag/webtest/feed/" rel="self" type="application/rss+xml" />
	<link>http://magustest.com/blog</link>
	<description>软件测试，自动化测试，白盒测试，Python</description>
	<lastBuildDate>Wed, 04 Jan 2012 09:09:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>自己动手创建Web测试验证规则</title>
		<link>http://magustest.com/blog/automationtesting/adding-web-test-verifaction-rule/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-web-test-verifaction-rule</link>
		<comments>http://magustest.com/blog/automationtesting/adding-web-test-verifaction-rule/#comments</comments>
		<pubDate>Tue, 26 May 2009 02:32:25 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[自动化测试]]></category>
		<category><![CDATA[VSTS]]></category>
		<category><![CDATA[WebTest]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=516</guid>
		<description><![CDATA[”Web测试”是由一系列 HTTP 请求组成，通过发出 HTTP 请求在协议层工作的测试类型。在VSTS中自带了若干个预先定义好的验证规则，例如在返回的页面上寻找某些字符，返回的文档中是否包含某些Tag，等等。前一段时间在测试一个安全过滤器，这个过滤器的基本功能就是过滤用户输入中的一些有可能构成安全隐患的内容，例如&#60;script&#62;标签的内容，JS内容等等。按照预先给出的测试用例执行完测试以后，OK，没有问题，不过在上线一个小时后发现一个问题，就是经过安全过滤器输出的内容会比原来输入的内容多了两个字符&#8211;“rn”，这个问题一下就修复好了，Trim一下。然后我就要给原来的测试加上对于这个问题的回归测试实现。很不巧，Web测试没有一个对返回内容长度进行验证的验证规则，不过微软提供了创建自定义验证规则的接口，我们可以创建一个验证ContentLength的规则。 1. 创建一个新的类库项目，这个项目是一个独立的可重用的类库，所创建的Web测试插件可以在不同的包含有Web测试的项目中使用。其实可以跟上一篇文章说的WebTest Plugin公用一个类库项目 2. 在该类库项目中添加对“Microsoft.VisualStudio.QualityTools.WebTestFramework”的引用 3. 在该类库项目中创建一个从ValidationRule派生出的类 4. 重写基类的Validate方法 5. 打开包含有Web Test的项目，并且在该项目中引用刚才添加的包含有自定义验证规则的类库项目 6. 打开需要调用自定义验证规则的Web测试，点击”Add Validation Rule”，选择刚才编写好的验证规则。 现在看看在自定义验证规则中添加参数，让用户输入相应的参数，从而使得验证规则更加灵活。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class ResponseContentLength : ValidationRule &#123; public int Length &#123; get; set; &#125; //输入的长度 public bool <a href='http://magustest.com/blog/automationtesting/adding-web-test-verifaction-rule/'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>”Web测试”是由一系列 HTTP 请求组成，通过发出 HTTP 请求在协议层工作的测试类型。在VSTS中自带了若干个预先定义好的验证规则，例如在返回的页面上寻找某些字符，返回的文档中是否包含某些Tag，等等。前一段时间在测试一个安全过滤器，这个过滤器的基本功能就是过滤用户输入中的一些有可能构成安全隐患的内容，例如&lt;script&gt;标签的内容，JS内容等等。按照预先给出的测试用例执行完测试以后，OK，没有问题，不过在上线一个小时后发现一个问题，就是经过安全过滤器输出的内容会比原来输入的内容多了两个字符&#8211;“rn”，这个问题一下就修复好了，Trim一下。然后我就要给原来的测试加上对于这个问题的回归测试实现。很不巧，Web测试没有一个对返回内容长度进行验证的验证规则，不过微软提供了创建自定义验证规则的接口，我们可以创建一个验证ContentLength的规则。</p>
<p>1. 创建一个新的类库项目，这个项目是一个独立的可重用的类库，所创建的Web测试插件可以在不同的包含有Web测试的项目中使用。其实可以跟上一篇文章说的WebTest Plugin公用一个类库项目</p>
<p>2. 在该类库项目中添加对“Microsoft.VisualStudio.QualityTools.WebTestFramework”的引用</p>
<p>3. 在该类库项目中创建一个从ValidationRule派生出的类</p>
<p>4. 重写基类的Validate方法</p>
<p>5. 打开包含有Web Test的项目，并且在该项目中引用刚才添加的包含有自定义验证规则的类库项目</p>
<p>6. 打开需要调用自定义验证规则的Web测试，点击”Add Validation Rule”，选择刚才编写好的验证规则。</p>
<p>现在看看在自定义验证规则中添加参数，让用户输入相应的参数，从而使得验证规则更加灵活。<br />
<span id="more-516"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ResponseContentLength <span style="color: #008000;">:</span> ValidationRule
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Length <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>                   <span style="color: #008080; font-style: italic;">//输入的长度</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> PassIfEqualsToContentLength <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Validate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ValidationEventArgs e<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			e<span style="color: #008000;">.</span><span style="color: #0000FF;">IsValid</span> <span style="color: #008000;">=</span> <span style="color: #008000;">!</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">==</span> Length<span style="color: #008000;">&#41;</span> <span style="color: #008000;">^</span> PassIfEqualsToContentLength<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">IsValid</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The length of the ContentLength: {0} is not equal to the expected result: {1}.&quot;</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContentLength</span>, Length<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>如果我们添加该自定义规则，那么会提示我们输入两个参数，一个是Length，另一个是一个布尔值；如果Response中的ContentLength和设置的Length相等，并且该布尔值设置为True，那么这个验证规则就算是通过。如图：</p>
<p><img src="http://magustest.com/blog/wp-content/uploads/2009/05/5-26-2009-10-26-01-am1.png" alt="5-26-2009-10-26-01-am" title="5-26-2009-10-26-01-am" width="560" height="426" class="alignnone size-full wp-image-517" /></p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/automationtesting/adding-web-test-verifaction-rule/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在VSTS中创建Web Test的插件</title>
		<link>http://magustest.com/blog/automationtesting/create-web-test-plugin-in-vsts/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=create-web-test-plugin-in-vsts</link>
		<comments>http://magustest.com/blog/automationtesting/create-web-test-plugin-in-vsts/#comments</comments>
		<pubDate>Fri, 22 May 2009 05:17:47 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[自动化测试]]></category>
		<category><![CDATA[VSTS]]></category>
		<category><![CDATA[WebTest]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=508</guid>
		<description><![CDATA[做过单元测试的朋友都知道，几乎每一个单元测试的框架都提供了类似于TestInitialize、TestCleanup这样的操作，可以在测试的开始和完成测试以后让我们处理一些问题，例如初始化一些数据，或者销毁一些数据等操作。在VSTS中有一种测试类型叫”Web测试”，它由一系列 HTTP 请求组成，通过发出 HTTP 请求在协议层工作。Web测试没有单独的TestInitialize、TestCleanup操作，但是VSTS所提供的Web Test插件能够帮助我们完成这样的工作。以下是MSDN对Web测试的定义： 使用 Web 测试插件，可以隔离代码并在 Web 测试中的主要声明性语句外部重用代码。自定义的 Web 测试插件为在运行 Web 测试时调用某些代码提供了途径。在每个测试迭代中，Web 测试插件都要运行一次。此外，如果重写测试插件中的 PreRequest 或 PostRequest 方法，这些请求插件将分别在每个请求之前或之后运行。 可见Web测试插件给使用者带来了很大的灵活性，下面就看看如何创建一个Web测试插件。 1. 创建一个新的类库项目，这个项目是一个独立的可重用的类库，所创建的Web测试插件可以在不同的包含有Web测试的项目中使用。 2. 在该类库项目中添加对“Microsoft.VisualStudio.QualityTools.WebTestFramework”的引用，该Dll是在.NET选项卡上 3. 在该类库项目中从WebTestPlugin派生出一个自定义的Web测试插件类 4. 重写相关的基类方法，例如 PreRequest、 PreWebTest、 PostWebTest等 5. 打开包含有Web Test的项目，并且在该项目中引用刚才添加的包含有自定义Web测试插件的类库项目 6. 打开需要调用Web测试插件的Web测试，点击”Add Web Test Plug-in”，选择刚才编写好的Web插件，完成。 以下是一个Web测试插件的实例代码，该插件重写了PreWebTest方法，所以会在Web测试执行之前运行一次。改插件的作用就是在每次运行Web测试之前，会读取一个配置文件，然后把相关的配置写到Web测试的Context中。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <a href='http://magustest.com/blog/automationtesting/create-web-test-plugin-in-vsts/'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>做过单元测试的朋友都知道，几乎每一个单元测试的框架都提供了类似于TestInitialize、TestCleanup这样的操作，可以在测试的开始和完成测试以后让我们处理一些问题，例如初始化一些数据，或者销毁一些数据等操作。在VSTS中有一种测试类型叫”Web测试”，它由一系列 HTTP 请求组成，通过发出 HTTP 请求在协议层工作。Web测试没有单独的TestInitialize、TestCleanup操作，但是VSTS所提供的Web Test插件能够帮助我们完成这样的工作。以下是MSDN对Web测试的定义：</p>
<blockquote><p>使用 Web 测试插件，可以隔离代码并在 Web 测试中的主要声明性语句外部重用代码。自定义的 Web 测试插件为在运行 Web 测试时调用某些代码提供了途径。在每个测试迭代中，Web 测试插件都要运行一次。此外，如果重写测试插件中的 PreRequest 或 PostRequest 方法，这些请求插件将分别在每个请求之前或之后运行。</p></blockquote>
<p>可见Web测试插件给使用者带来了很大的灵活性，下面就看看如何创建一个Web测试插件。</p>
<p>1. 创建一个新的类库项目，这个项目是一个独立的可重用的类库，所创建的Web测试插件可以在不同的包含有Web测试的项目中使用。</p>
<p>2. 在该类库项目中添加对“Microsoft.VisualStudio.QualityTools.WebTestFramework”的引用，该Dll是在.NET选项卡上</p>
<p>3. 在该类库项目中从WebTestPlugin派生出一个自定义的Web测试插件类</p>
<p>4. 重写相关的基类方法，例如 PreRequest、 PreWebTest、 PostWebTest等</p>
<p>5. 打开包含有Web Test的项目，并且在该项目中引用刚才添加的包含有自定义Web测试插件的类库项目</p>
<p>6. 打开需要调用Web测试插件的Web测试，点击”Add Web Test Plug-in”，选择刚才编写好的Web插件，完成。<br />
<span id="more-508"></span><br />
以下是一个Web测试插件的实例代码，该插件重写了PreWebTest方法，所以会在Web测试执行之前运行一次。改插件的作用就是在每次运行Web测试之前，会读取一个配置文件，然后把相关的配置写到Web测试的Context中。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> FilterRuleWebPlugin <span style="color: #008000;">:</span> Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">VisualStudio</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TestTools</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WebTesting</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WebTestPlugin</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">string</span> key <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;RuleName&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> PreWebTest<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, PreWebTestEventArgs e<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PreWebTest</span><span style="color: #008000;">&#40;</span>sender, e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">//Add the new context</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">WebTest</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Context</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			e<span style="color: #008000;">.</span><span style="color: #0000FF;">WebTest</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Context</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>key, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> filter <span style="color: #008000;">=</span> GetFilter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>filter<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			e<span style="color: #008000;">.</span><span style="color: #0000FF;">WebTest</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Context</span><span style="color: #008000;">&#91;</span>key<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;default&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #008000;">&#123;</span>
			e<span style="color: #008000;">.</span><span style="color: #0000FF;">WebTest</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Context</span><span style="color: #008000;">&#91;</span>key<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> filter<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetFilter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> filter <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
		XmlDocument doc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			doc<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Config.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>doc <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				filter <span style="color: #008000;">=</span> doc<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectSingleNode</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Settings/Filter&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;LoadFilterError&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> filter<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>我们还可以在Web测试插件中定义一些公开的属性（Property），利用公开的属性，可以在运行Web测试之前对一些参数的输入进行指定。</p>
<p>VSTS的Web测试用起来挺方法，功能也挺强大，如果大家在使用的过程中遇到什么不爽的事情，可以尝试自己编写Web测试插件来改变那些不爽的状态。;-)Web测试插件还有很多的应用场景，今天抛砖引玉啊。</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/automationtesting/create-web-test-plugin-in-vsts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

