總結
Visual Studio的Web Performance Test是基於HTTP協議層的,它不依賴於瀏覽器,通過直接接收,發送HTTP包來和Web服務器交互。Web Performance Test發送和接收的一系列請求和響應之間存在相關性,例如,用戶登錄后,SID被傳遞給客戶端,下一次請求時,需要把SID發送到服務器。因此,Web Perfomance Test 定義了多種提取規則,幫助從服務器響應中提取信息,用於之后的請求。或者保存起來,作為測試結果的一部分。
Web Performance Test提供多種提取規則,以下表格來自MSDN:
提取規則的類型 | 說明 |
Selected Option | 提取列表或組合框中的選定文本。 |
Tag Inner Text | 從指定的 HTML 標記中提取內部文本。 |
Extract Attribute Value | 從指定的 HTML 標記中提取特性的值。 有關以下內容的更多信息使用提取特性值規則的更多信息,請參見演練:向 Web 性能測試添加驗證規則和提取規則。 |
Extract Form Field | 提取響應中指定窗體字段的值。 |
Extract HTTP Header | 提取 HTTP 標頭的值。 |
Extract Regular Expression | 從與正則表達式相匹配的響應中提取文本。 |
Extract Text | 從響應中提取文本。 |
Extract Hidden Fields | 從響應中提取所有的隱藏字段。 |
在 (1)和(2)中,我們講解了系統默認的一些提取規則,本文將講解如何建立自定義提取規則,本文的代碼可以從這里下載。
繼承ExtractionRule
所有的提取規則,包括自定義規則都需要從ExtractionRule繼承,該類在Microsoft.VisualStudio.QualityTools.WebTestFramework.dll中實現。
獨立的Library
我們最好把自定義規則都放到一個獨立的類庫中,這樣方便多個web performance test 工程引用。 web performance test 工程只要引用了該類庫,在右鍵點擊URL,選擇Add Extraction Rule中,在打開的Add Extraction Rule窗口中,就可以看到所有的自定義提取規則,和用法系統默認的規則完全相同。
例子
本文繼續沿用(2)中的例子,那是一個簡單的算術站點:
在(2)中,我們發現Extract Regular Express規則不適合把“等於3。”中的數字提取出來,它提取的值將會包括整個文本。那么,本文將定義一個“Custom Extract Regular Express”,實現通過正則表達式提取其中的數字,而不是整個文本。
看代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using Microsoft.VisualStudio.TestTools.WebTesting; using System.ComponentModel; using System.Text.RegularExpressions; using System.Web; namespace CustomExtractionRule { [DescriptionAttribute("Extracts the specificed group from mached regex")] [DisplayNameAttribute("Custom Extract Regular Expression")] public class CustomExtractRegularExpression : ExtractionRule { [DescriptionAttribute("Whether or not to perfom HTML decoding of extracted strings.")] [DisplayNameAttribute("Html Decode")] [DefaultValue(true)] public bool HtmlDecode { get; set; } [DefaultValue(false)] [DescriptionAttribute("Ignore case during search for matching text.")] [DisplayNameAttribute("Ignore Case")] public bool IgnoreCase { get; set; } [DefaultValue(0)] [DescriptionAttribute("Indicates which occurrence of the string to extract. this is a zero-based index.")] [DisplayNameAttribute("Index")] public int Index { get; set; } [DescriptionAttribute("Specify the regular expression to search for.")] [DisplayNameAttribute("Regular Expression")] public string RegularExpression { get; set; } [DefaultValue(true)] [DescriptionAttribute("If ture, the extraction rule fails if no value is found to extract.")] [DisplayNameAttribute("Required")] public bool Required { get; set; } [DefaultValue(0)] [DescriptionAttribute("Indicates which group of the string to extract in matched regular expression. this is a zero-based index.")] [DisplayNameAttribute("Group Index")] public int GroupIndex { get; set; } public override void Extract(object sender, ExtractionEventArgs e) { String errormessage=""; String result = this.Extract(e.Response.BodyString, ref errormessage); if (!string.IsNullOrEmpty(result)) { if (this.HtmlDecode) { result = HttpUtility.HtmlDecode(result); } e.WebTest.Context[this.ContextParameterName] = result; } else { e.Success = false; e.Message = errormessage; } } internal String Extract(string document,ref string errormessage) { int startat = 0; int num2 = 0; RegexOptions options = RegexOptions.Multiline; if (this.IgnoreCase) { options |= RegexOptions.IgnoreCase; } Regex regex = new Regex(this.RegularExpression, options); Match selectedMatch=null; while (startat < document.Length) { Match match = regex.Match(document, startat); if (!match.Success) { break; } int num3 = match.Index + match.Length; if (num2 == this.Index) { selectedMatch = match; } startat = num3; num2++; } if (selectedMatch == null) { errormessage = "Matched string is not found"; return null; } if (selectedMatch.Groups.Count - 1 < this.GroupIndex) { errormessage = "Matched group is not found"; return null; } return selectedMatch.Groups[GroupIndex].Value; } } }
1) 在CustomExtractRegularExpression 的類和屬性上,我們用到了DisplayNameAttribute,DescriptionAttribute,DefaultValue這些Attribute,他們的作用是在VS的Add Extraction Rule窗口上配置提取規則時,定義規則的顯示名和描述,以及每個屬性的顯示名,描述和默認值。
2)提取規則通過重載void Extract(object sender, ExtractionEventArgs e) 方法來實現。如果提取成功,把e.Success 設置為true,並且把提取的參數值保存在e.WebTest.Context[this.ContextParameterName]中;否則e.Success設置為false,並在e.Message中填入失敗的消息。
3)Custom Extract Regular Expression規則,相對於Extract Regular Expression規則,我們增加了一個Group Index參數,允許用戶從特定的正則表達式匹配中,選中匹配的group,關於正則表達式group,可以參考MSDN。
應用自定義規則
現在,我們把規則添加到web performance test中,我們用Custom Extract Regular Expression來替換在(2)中我們使用的Extract Text規則來提取結果中的數值。屬性配置如下:
注意,"Group Index”參數應該設置為1
知平軟件致力於移動平台自動化測試技術的研究,我們希望通過向社區貢獻知識和開源項目,來促進行業和自身的發展。