14.3 前端性能测试自动化介绍
随着功能测试的发展与壮大,功能自动化测试已经越来越广泛地应用于现行软件系统的测试,那么性能测试方面是否能给实现自动化的控制呢?答案是肯定的,随着行业的发展,性能测试的研究也日趋深入,前面我们向大家介绍了如何自动化控制场景的运行,当然这只是性能测试自动化的冰山一角,其目的也是拓展大家性能测试方面的思路。
本节作者将向大家介绍前端性能测试的自动化控制,据我所知有一些单位已经针对自己公司的业务开发了一些适用其自身的性能测试工具,鉴于其针对行业的特殊性,这里我们不予探讨。我们谨以通用的、被大家认可的前端性能工具为例,讲解前端性能自动化控制的相关内容,在此我们挑选的工具就是HttpWatch,在前面的章节相信大家已经看到了HttpWatch在前端性能测试分析方面是如此强大,肯定也很关心其更深层次的应用,这里我向大家介绍该工具自动化前端性能测试的实现方法,您可以通过访问“http://apihelp.httpwatch.com/”了解更多关于httpwatch自动化方面的内容。
图14-21描述了Controller、Log、Entry和Plugin这4个主要类与浏览器之间的关系,下面我们来看一下这4个主要类都是用来做什么的。
1.控制类(Controller Class)
HttpWatch控制类用来创建一个HttpWatch插件实例,或者是打开一个已经存在的实例文件。可以通过这个控制类的一个“OpenLog”方法打开一个日志文件,并且返回这个日志文件的相应的说明信息,这个说明信息包括的就是录制过中的请求和响应文件信息。
2.插件类(Plugin Class)
HttpWatch分别为IE和Firefox提供了插件类,它主要是针对Http协议交互提供启动和停止方法去控制HttpWatch的录制和停止录制功能,对应的方法名称为“Record”和“Stop”,并且还提供了一些方法和属性去管理和配置自动化录制方式。其中插件类中的“GotoURL”方法可以用于重定向浏览任何指定的URL地址。
3.日志类(Log Class)
HttpWatch日志类用于获取日志信息,这些日志信息就是HttpWatch录制过程记录的请求和响应信息。日志类提供了许多属性和方法,并且允许对这些录制的数据信息进行检索和保存,或者以多种格式导出(支持:HWL、CSV、XML、HAR格式)等。
4.属性类(Entry Class)
HttpWatch属性类的每个日志文件都包含一个属性列表,且这个属性列表中包括详细的HTTP交互信息。这些内容具体包括请求的资源信息和一些返回的信息。这个请求和响应属性信息提供了访问头文件和Cookies文件,这些信息都是在与服务器发生交互过程中产生的。同时最后产生的结果信息也通过该类属性进行输出,如:BytesReceived属性是接收的总字节数。
14.4 HttpWatch前端性能测试自动化脚本
HttpWatch提供了一些脚本示例代码供大家参考,在HttpWatch安装目录下的“api_examples”文件夹下,您会看到2个子文件夹:“ie”和“Firefox”,这里我们希望看到基于IE浏览器的相关API调用方法,所以选择“ie”文件夹,再进入到“page_check”,在该文件夹下还有4个子文件夹,分别是“csharp”、“javascript”、“ruby”、“VBScript”,下面就让我们一起来看一下这4个文件夹下主要的脚本实现代码。
Ruby脚本代码(page_check.rb和page_check_watir.rb文件内容):
#page_check.rb文件 # Page Check Ruby Example # # For more information about this example please refer to the readme.txt file in the # same directory # require 'win32ole' puts "Enter the URL of the page to check (press enter for www.httpwatch.com):\n"; $stdout. flush url = gets.chomp! if url.empty? url = "www.httpwatch.com" end puts "\nChecking " + url + "...\r\n\r\n"; $stdout.flush # Create a new instance of HttpWatch in IE control = WIN32OLE.new('HttpWatch.Controller') plugin = control.IE.New() # Start Recording HTTP traffic plugin.Log.EnableFilter(false) plugin.Record() # Goto to the URL and wait for the page to be loaded plugin.GotoURL(url) control.Wait(plugin, -1) # Stop recording HTTP plugin.Stop() if plugin.Log.Pages.Count != 0 printf "\nPage Title: '%s'\n", plugin.Log.Pages(0).Title # Display summary statistics for page summary = plugin.Log.Pages(0).Entries.Summary printf "Total time to load page (secs): %.3f\n", summary.Time printf "Number of bytes received on network: %d\n", summary.BytesReceived printf "HTTP compression saving (bytes): %d\n", summary.CompressionSavedBytes printf "Number of round trips: %d\n", summary.RoundTrips printf "Number of errors: %d\n", summary.Errors.Count end |
# Close down IE plugin.CloseBrowser() puts "\r\nPress Enter to exit"; $stdout.flush gets # page_check_watir.rb文件 # Page Check Ruby and WATIR Example # # For more information about this example please refer to the readme.txt file in the # same directory # require 'win32ole' require 'watir' puts "Enter the URL of the page to check (press enter for www.httpwatch.com):\n"; $stdout. flush url = gets.chomp! if url.empty? url = "www.httpwatch.com" end puts "\nChecking " + url + "...\r\n\r\n"; $stdout.flush # Attach HttpWatch control = WIN32OLE.new('HttpWatch.Controller') # Open the IE browser plugin = control.IE.New # Attach Watir to IE browser containing HttpWatch plugin Watir::IE::attach_timeout = 30 ie = Watir::IE.attach(:hwnd, plugin.Container.HWND ) # Start Recording HTTP traffic plugin.Clear() plugin.Log.EnableFilter(false) plugin.Record() # Goto to the URL and wait for the page to be loaded ie.goto(url) # Stop recording HTTP plugin.Stop() if plugin.Log.Pages.Count != 0 printf "\nPage Title: '%s'\n", plugin.Log.Pages(0).Title # Display summary statistics for page summary = plugin.Log.Pages(0).Entries.Summary printf "Total time to load page (secs): %.3f\n", summary.Time printf "Number of bytes received on network: %d\n", summary.BytesReceived printf "HTTP compression saving (bytes): %d\n", summary.CompressionSavedBytes printf "Number of round trips: %d\n", summary.RoundTrips printf "Number of errors: %d\n", summary.Errors.Count end # Close down IE plugin.CloseBrowser(); puts "\r\nPress Enter to exit"; $stdout.flush gets |
C#源代码(pagechecker.cs文件内容):
// Page Check C# Example // // For more information about this example please refer to the readme.txt file in the // same directory // using System; using HttpWatch; namespace page_check { class PageChecker { [STAThread] static void Main(string[] args) { Console.WriteLine("Enter the URL of the page to check (press enter for www.httpwatch.com):\r\n"); string url = Console.ReadLine(); if ( url.Length == 0 ) url = "www.httpwatch.com"; Console.WriteLine("\r\nChecking " + url + "...\r\n"); // Create a new instance of HttpWatch in IE Controller control = new Controller(); Plugin plugin = control.IE.New(); // Start Recording HTTP traffic plugin.Log.EnableFilter(false); plugin.Record(); // Goto to the URL and wait for the page to be loaded plugin.GotoURL(url); control.Wait(plugin, -1); // Stop recording HTTP plugin.Stop(); if (plugin.Log.Pages.Count != 0) { Console.WriteLine("\r\nPage Title: '" + plugin.Log.Pages[0].Title + "'"); Console.WriteLine(); // Display summary statistics for page Summary summary = plugin.Log.Pages[0].Entries.Summary; Console.WriteLine("Total time to load page (secs): " + summary.Time); Console.WriteLine("Number of bytes received on network: " + summary.BytesReceived); Console.WriteLine("HTTP compression saving (bytes): " + summary.CompressionSavedBytes); Console.WriteLine("Number of round trips: " + summary.RoundTrips); Console.WriteLine("Number of errors: " + summary.Errors.Count); } |
VBScript脚本代码(page_check.vbs文件内容):
' Page Check VBScript Example ' ' For more information about this example please refer to the readme.txt file in the ' same directory ' WScript.Echo(vbCrLf & "Enter the URL of the page to check (press enter for www.httpwatch.com):" & vbCrLf) Dim url url = WScript.StdIn.ReadLine if Len(url) = 0 then url = "www.httpwatch.com" end if WScript.Echo( vbCrLf & "Checking " & url & "..." & vbCrLf) ' Create a new instance of HttpWatch in IE Dim control Set control = CreateObject("HttpWatch.Controller") Dim plugin Set plugin = control.IE.New ' Start Recording HTTP traffic plugin.Log.EnableFilter false plugin.Record ' Goto to the URL and wait for the page to be loaded plugin.GotoURL url control.Wait plugin, -1 ' Stop recording HTTP plugin.Stop if plugin.Log.Pages.Count <> 0 then WScript.Echo("") WScript.Echo("Page Title: '" & plugin.Log.Pages(0).Title & "'") ' Display summary statistics for page Dim summary Set summary = plugin.Log.Pages(0).Entries.Summary WScript.Echo( "Total time to load page (secs): " & summary.Time) WScript.Echo( "Number of bytes received on network: " & summary.BytesReceived) WScript.Echo( "HTTP compression saving (bytes): " & summary.CompressionSavedBytes) WScript.Echo( "Number of round trips: " & summary.RoundTrips) WScript.Echo( "Number of errors: " & summary.Errors.Count) end if ' Close down IE plugin.CloseBrowser WScript.Echo( vbCrLf & "Press Enter to exit") WScript.StdIn.ReadLine |
JavaScript脚本代码(page_check.js文件内容):
// Page Check Javascript Example // // For more information about this example please refer to the readme.txt file in the // same directory // WScript.Echo("\nEnter the URL of the page to check (press enter for www.httpwatch.com):\n"); var url = WScript.StdIn.ReadLine(); if ( url.length == 0 ) url = "www.httpwatch.com"; WScript.Echo("\nChecking " + url + "...\n"); // Create a new instance of HttpWatch in IE var control = new ActiveXObject('HttpWatch.Controller'); var plugin = control.IE.New(); // Start Recording HTTP traffic plugin.Log.EnableFilter(false); plugin.Record(); // Goto to the URL and wait for the page to be loaded plugin.GotoURL(url); control.Wait(plugin, -1); // Stop recording HTTP plugin.Stop(); if ( plugin.Log.Pages.Count != 0 ) { WScript.Echo( "\nPage Title: '" + plugin.Log.Pages(0).Title + "'"); // Display summary statistics for page var summary = plugin.Log.Pages(0).Entries.Summary; WScript.Echo( "Total time to load page (secs): " + summary.Time); WScript.Echo( "Number of bytes received on network: " + summary.BytesReceived); WScript.Echo( "HTTP compression saving (bytes): " + summary.CompressionSavedBytes); WScript.Echo( "Number of round trips: " + summary.RoundTrips); WScript.Echo( "Number of errors: " + summary.Errors.Count); } // Close down IE plugin.CloseBrowser(); WScript.Echo( "\r\nPress Enter to exit"); WScript.StdIn.ReadLine(); |
上述4个脚本其实现的目标是一致的,都是让您先输入一个要考察的网址,然后其调用httpwatch相关的API函数开始录制、记录过程数据、停止录制、输出结果信息、关闭浏览器这样一个处理过程。这里以JavaScript脚本为例,运行“run.cmd”文件,在弹出的控制台界面输入“www.baidu.com”,将会自动打开“百度”页面,过一会页面将会关闭返回控制台界面,在控制台将产生相关的结果信息,如图14-22所示,按任意键将退出控制台界面。
(未完待续)
版权声明:51Testing软件测试网及相关内容提供者拥有51testing.com内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像。51testing软件测试网欢迎与业内同行进行有益的合作和交流,如果有任何有关内容方面的合作事宜,请联系我们。
相关链接:
精通软件性能测试与LoadRunner最佳实战 连载十四