开源Web自动化测试工具.doc_第1页
开源Web自动化测试工具.doc_第2页
开源Web自动化测试工具.doc_第3页
开源Web自动化测试工具.doc_第4页
开源Web自动化测试工具.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

亿能测试技术 开源Web自动化测试工具WatirWatir(发音与 water相近)全称是Web Application Testing in Ruby。Watir是一款用Ruby脚本语言驱动浏览器的自动化测试工具。 Watir可以驱动那些作为Html页面被发送到Web浏览器端的应用程序。Watir对下列组件不起作用:ActiveX、Java Applets、Macromedia Flash或者其他的应用程序插件。判断Watir是否可用的方法:在页面上点击右键-查看页面源代码,如果可以看到Html源代码,就说明页面上的对象可以被Watir识别,来实现自动化。要使用Watir进行Web自动化测试,至少要掌握以下内容1、HTML的知识。2、编程的基本常识,如变量的定义与使用,基本的控制语句,如If、for等。3、Ruby的基本编程知识。4、IE Developer Toolbar或是其他类似的浏览器辅助工具,在以后的开发中,将非常有效的帮你识别页面对象的属性。Watir是基于ruby的自动化测试框架,采用ruby作为脚本语言,因此在安装Watir之前需要下载并安装ruby:/frs/download.php/29263/ruby186-26.exe首先打开ruby的包管理器(RubyGems Package Manager),用以下命令更新gem:gem update -system然后用以下命令连线安装watir:gem install watir安装完后编写一个HelloWorld.rb来尝试Watir是否生效,代码如下所示:require watirtest_site = /testing_is_believing/# open the IE browserie = Watir:IE.new# print some commentsputs # Beginning of testputs puts Step 1: go to the test site: + test_siteie.goto(test_site)puts Action: entered + test_site + in the address bar.上面的ruby脚本中引用了watir包,打开IE并导航到指定页面地址。把脚本存成后缀为rb的文件,编辑时将打开ruby的编辑工具SciTE,编辑好脚本后按F5执行,如图所示:为了方便脚本开发,可下载安装Watir的录制器WatirRecorder+:/blog/content/binary/WatirRecorder_Setup_lite.msiWatirRecorder+是针对Watir开发的浏览器脚本录制器,可以把Web页面操作录制下来并生成Watir脚本,如图所示:要使用Watir进行Web自动化测试脚本开发,需要熟悉Watir的提供的API(如图所示),安装Watir后,可以在安装目录找到watir的API文档,例如:C:rubylibrubygems1.8docwatir-1.6.5rdoc使用Watir进行自动化测试的本质就是利用这些API来操作浏览器,例如使用“link”方法来点击指定链接:require watirie = Watir:IE.start(/testing_is_believing)ie.link(:url , /Testing_is_believing/category/647265.aspx).click又例如使用“contains_text”方法检查页面中是否包含指定的文本:require watirie = Watir:IE.start(/testing_is_believing)ie.link(:url , /Testing_is_believing/category/647265.aspx).clickif ie.contains_text(HelloWorld - Watir) puts Test passed. Page contains the text: HelloWorld - Watirelse puts Test failed! Page didnt contain text: HelloWorld - Watirend使用“test/unit”包,还可以将watir测试与单元测试框架整合,例如:require watirrequire test/unitclass TC_myTest Test:Unit:TestCase def setup puts starting a testcase. end def test_1_blog ie = Watir:IE.new ie.goto(/testing_is_believing) ie.link(:url , /Testing_is_believing/category/647265.aspx).click assert(ie.contains_text(HelloWorld - Watir) end def test_2_google ie = Watir:IE.new ie.goto(.hk) ie.text_field(:name,q).set(Watir) ie.button(:name,btnG).click end def teardown puts just finish running a testcase! endend最后,给读者展示一个相对完整的使用Watir测试的小例子,这个例子测试QTP的WEB样例程序(/),其中使用了screen_capture包来实现屏幕截取:require test/unitrequire watirrequire watir/screen_captureclass Demo Test:Unit:TestCaseinclude Watirinclude Watir:ScreenCapture def setup ie = Watir:IE.new_process ie.goto(/) end def test_logon ie = Watir:IE.attach(:url,/ ) ie.maximize() assert_equal(ie.title, Welcome: Mercury Tours) ie.text_field(:name, userName).set test ie.text_field(:name, password).set test ie.button(:name,login).click screen_capture(logon.jpg,true) assert_equal(ie.title, Find a Flight: Mercury Tours:) assert(ie.radio(:name,tripType, oneway).exists?) assert(ie.select_list(:name,passCount).exists?) assert(ie.select_list(:name,fromPort).exists?) assert(ie.select_list(:name,fromMonth).exists?) assert(ie.select_list(:name,fromDay).exists?) assert(ie.select_list(:name,toPort).exists?) assert(ie.radio(:name, servClass,Business).exists?) assert(ie.select_list(:name,airline).exists?) assert(ie.form(:name, findflight).exists?) # booking page 1 ie.radio(:name,tripType, oneway).set ie.select_list(:name,passCount).select 1 ie.select_list(:name,fromPort).select Acapulco ie.select_list(:name,fromMonth).select March ie.select_list(:name,fromDay).select 10 ie.select_list(:name,toPort).select Frankfurt ie.radio(:name, servClass,Business).set ie.select_list(:name,airline).select Unified Airlines ie.form(:name, findflight).submit screen_capture(page1.jpg,true) # booking page 2 assert(ie.radio(:name, outFlight,Blue Skies Airlines$360$270$5:03).exists?) assert(ie.radio(:name, outFlight,Blue Skies Airlines$361$271$7:10).exists?) assert(ie.radio(:name, outFlight,Pangea Airlines$362$274$9:17).exists?) assert(ie.radio(:name, outFlight,Unified Airlines$363$281$11:24).exists?) ie.radio(:name, outFlight,Pangea Airlines$362$274$9:17).set assert(ie.radio(:name, inFlight,Blue Skies Airlines$631$273$14:30).exists?) ie.radio(:name, inFlight,Blue Skies Airlines$631$273$14:30).set assert(ie.form(:name,results).exists?) ie.form(:name,results).submit screen_capture(page2.jpg,true) #booking page 3 - purchase assert(ie.text_field(:name,passFirst0).exists?) assert(ie.text_field(:name,passLast0).exists?) assert(ie.text_field(:name,creditnumber).exists?) assert(ie.form(:name,bookflight).exists?) ie.text_field(:name,passFirst0).set samuel ie.text_field(:name,passLast0).set luo ie.text_field(:name,creditnumber).set 1234 ie.form(:name,bookflight).submit screen_capture(page3.jpg,true) end def teardown ie.close() endendWatiNWatiN(/)可以说是Watir的.NET版,受到Watir的启发,WatiN于2005年开始开发,希望做成类似watir一样的Web应用程序测试工具,但是使用.NET语言作为脚本开发语言,支持IE浏览器和FireFox浏览器。目前最新版本是WatiN 2.0 RC 1。下载地址:/releasenotes-2-0-20-1089.htmlWatiN支持的测试环境如下表所示:Windows XPWindows VistaWindows 2003Windows 2008IE 6OK-IE 7OK-OKIE 8OK-Firefox 2OK-Firefox 3OK-OK另外,为了方便WatiN的脚本开发,可以下载一个WatiN 的脚本录制器 - WatiN Test Recorder:/ WatiN Test Recorder提供了一个集成的界面(如图所示)用于WatiN脚本的录制,包括内嵌了浏览器、脚本编辑、脚本运行、HTML DOM查看器、测试对象查看器等。* 注意:2.0 beta版本仅支持IE7以上的浏览器的录制。下面介绍一下WatiN的基本使用方法:1、首先把下载的WatiN的安装包解压到一个目录,然后打开Visaul Studio 2005,新建一个控制台的项目。2、在项目中引用WatiN的核心库DLL(WatiN的Bin文件夹中的WatiN.Core.dll),然后编写如下代码:using System;using System.Collections.Generic;using System.Text;using WatiN.Core;using System.Threading;namespace ConsoleApplication1 class Program STAThread static void Main(string args) / 打开一个新的IE窗口并导航到google IE ie = new IE(.hk, true); / 找到查询输入框并输入“WatiN” ie.TextField(Find.ByName(q).TypeText(WatiN); / 点击“Google 搜索”按钮 ie.Button(Find.ByValue(Google 搜索).Click(); / 如果希望马上关闭浏览器和控制台,则需要下面这句: /ie.Close(); 这段代码使用WatiN打开一个新的IE窗口并导航到google网站,找到查询输入框并输入“WatiN”,然后点击“Google 搜索”按钮。注意,由于IE浏览器多线程安全的原因,需要添加“STAThread”。3、直接运行上面的代码,就能通过WatiN驱动IE浏览器进行Google的简单测试了。接下来再深入一点,获取Google查询结果页面的元素内容: IE ie = new IE(.hk,true); ie.TextField(Find.ByName(q).TypeText(WatiN); ie.Button(Find.ByValue(Google 搜索).Click(); Element element = ie.Element(Find.By(id, resultStats); string resultStats = element.Text; Console.WriteLine(resultStats); /ie.Close();可以看到,使用WatiN可以利用Visual Studio和C#语言,以比较简洁的方式进行Web自动化测试,结合IE Developer ToolBar和WatiN Test Recorder,也不失为一个不错的Web自动化测试工具的选择。WatiN中常用的类有WatiN.Core.Find和WatiN.Core.IE。最普遍使用的类就是Find类的,它是一个工厂类,主要使用它的静态方法来实现一些查询条件。比如像上面的例子中的这个语句“ie.Button(Find.ByName(btnG).Click();”就调用了Find的静态方法ByName来查询一个name属性为指定值的HTML元素,然后再调用IE对象的Button方法把这个元素转换为按钮对象。WatiN.Core.IE是最关键的类,常用的方法包括AttachToIE(与一个已经打开的IE浏览器关联)、Button、TextField、Image、Div 等一系列方法,还有与Find对象共同使用用于返回IE中特定的按钮,输入框,等HTML元素。WatiN支持的HTML页面元素以及其对应的测试类如表所示:Html element WatiN Class WatiN Collection Example Version Link LinkCollection Ie.Link(linkId) 0.7 Area AreaCollection ie.Area(Find.ByAlt(alttext) 1.2 Button ButtonCollection Ie.Button(buttonId) 0.9 Div DivCollection Ie.Div(divId) 0.7 Form FormCollection Ie.Form(formId) 0.7 Frame FrameCollection Ie.Frame(frameId) 0.7 - FrameCollection Ie.Frames 0.7 Frame FrameCollection Ie.Frame(iframeId) 0.9 Image ImageCollection Ie.Image(imageId) 0.7 Button ButtonCollection Ie.Button(buttonId) 0.7 CheckBox CheckBoxCollection Ie.CheckBox(checkboxId) 0.7 FileUpload FileUploadCollection Ie.FileUpload(fileuploadId) 0.9 TextField TextFieldCollection Ie.TextField(hiddenId) 0.7 no longer supported as of 2.0 beta 1 Image ImageCollection Ie.Image(imageId) 0.9.5 TextField TextFieldCollection Ie.TextField(passwordId) 0.7 RadioButton RadioButtonCollection Ie.RadioButton(radioId) 0.7 Button ButtonCollection Ie.Button(resetId) 0.7 Button ButtonCollection Ie.Button(submitId) 0.7 TextField TextFieldCollection Ie.TextField(textId) 0.7 Label LabelCollection Ie.Label(elementId) 0.7 Option OptionCollection Ie.Select(selectId).Options 1.0 Para ParaCollection Ie.Para(pId) 0.7 Select SelectCollection Ie.Select(selectId) 0.7 Span SpanCollection Ie.Span(spanId) 0.7 Table TableCollection Ie.Table(tableId) 0.7 TableBody TableBodyCollection Ie.TableBody(tablebodyId) Ie.Table(tableid).TableBodies 1.0 TableCell TableCellCollection Ie.TableCell(tablecellId) or Ie.Table(TableId).TableRows0.TableCells0 0.7 TextField TextFieldCollection Ie.TextField(textareaId) 0.7 TableRow TableRows Ie.TableRow(tablerowId) or Ie.Table(TableId).TableRows0 0.7 All elements, also the ones not mentioned in this list Element and ElementsContainer ElementCollection Ie.Element(elementId) Ie.Element(tagname, elementId) 0.9 1.2 参考:HTML Mapping Table/htmlelementmapping.html由于WatiN的核心是C#编写的类库,因此可以被QTP通过DotNetFactory来调用,可以作为QTP的补充和辅助工具来使用,下面是一个简单的QTP整合WatiN的例子:Set IE = DotNetFactory.CreateInstance( WatiN.Core.IE , D:WatiN-089-net-2.0BinWatiN.Core.dll , True )Set Find = DotNetFactory.CreateInstance( WatiN.Core.Find , D: WatiN-089-net-2.0BinWatiN.Core.dll )IE.GoTo /testing_is_believingIE.WaitForCompleteSet LinkObj = IE.Link( Find.ByUrl( /Testing_is_believing/category/647265.aspx ) )IF LinkObj.Exists Then LinkObj.Click()ElsePrint Fail!End IFIE.CloseQAliberWatiN提供了一个小型的稳定的web自动化测试框架,用于测试IE和Firefox上的Web页面(将来还可能会支持Chrome),下面介绍一个工具名为“QAliber”,这个工具提供了一个WatiN的插件(Plug-in),可以在QAliber的框架中使用WatiN的语法,结合QAliber提供的工具,可以更快地编写和部署WatiN测试。QAliber的主页:/index.phpQAliber包括两个版本,QAliber Test Developer和QAliber Test Builder,其中QAliber Test Developer版本作为VS.NET的插件使用,QAliber Test Builder可以用界面的方式创建和执行测试。QAliber支持GUI的录制和回放,支持Win32 、 .NET forms 、WPF 、WEB的界面测试,可以在支持.NET framework 3.0的所有windows平台上运行,可以用C#或VB.NET编写测试脚本。下载地址:/projects/qaliber/files/下面主要介绍QAliber Test Developer作为VS.NET的插件创建WatiN测试脚本的方法:1、首先下载并安装QAliber,然后打开VS2005,新建QAliber类型的测试项目,如图所示:2、通过VS2005的菜单项找到界面控件浏览器“UI Control Browser”(UI Testing - UI Control Browser),打开后选择“Web(Watin)”,如图所示。这时将会显示IE浏览器或Firefox浏览器,以及浏览器中加载的页面对象。选择指定的对象,可以在属性窗口查看到该对象的各种属性(如图所示),包括“Code Path”,也就是WatiN中访问对象的代码。另外,还可以用“Control Finder”选定Web页面上的对象进行查看。这些功能都能帮助我们降低编写WatiN测试脚本的难度,例如可以直接把“Code Path”的代码粘贴到脚本编辑器中,再适当添加对象的操作方法,就可以形成一个测试步骤,例如:Desktop.Watin.IE(/).TextField(Find.ById(kw).TypeText(QAliber);这句脚本用于在百度搜索框输入“QAliber”。3、在QAliber创建的框架代码中,默认在“MyTestCase”类中编写测试用例,例如:using System;using System.Collections.Generic;using System.Text;using System.Windows;using System.Windows.Forms;using QAliber.Engine;using QAliber.Engine.Controls;using QAliber.Engine.Controls.UIA;using QAliber.Engine.Controls.Web;using QAliber.Engine.Controls.WPF;using QAliber.Logger;using QAliber.TestModel;using QAliber.RemotingModel;using WatiN.Core;namespace TestCasesPackage1 Serializable QAliber.TestModel.Attributes.VisualPath(My Test Cases) public class MyTestCase : TestCase public MyTestCase() name = My Test Case; description = ; expectedResult = TestCaseResult.None; icon = null; public override void Body() Desktop.Watin.IE(/).TextField(Find.ById(kw).TypeText(QAliber); WatiN.Core.Button btn = Desktop.Watin.IE(/).Button(Find.ById(su); btn.Click(); 测试用例的脚本一般直接放到Body方法中。Runner类的代码是测试执行框架的代码:using System;using System.Collections.Generic;using System.Text;namespace TestCasesPackage1 / / This class provides a way for debugging your test cases code. / Before deploying your test cases project, please remove this file, and / set the project properties to Class Library / public class Runner STAThread public static void Main() using (QAliber.Logger.Log.Default) QAliber.Logger.Log.Default.Filename = TestCasesLog.log; try Console.WriteLine(Debugging Test Cases.); /Enter here the test cases you want to debug new MyTestCase().Body(); catch (Exception ex) Console.WriteLine(Exception caught : + ex.Message + n + ex.StackTrace); finally Console.WriteLine(. Debugging Ended); Console.WriteLine(Press any enter to exit.); Console.ReadLine(); #region Temporary Recording Code private void RecordedTest1() #endregion 最后在VS2005中直接运行整个项目,QAliber将启动并执行编写的WatiN测试用例代码。SeleniumSelenium(/)是一款开源的Web自动化测试工具,主要由3部分组成:1、Selenium IDE:Firefox的插件,可用于录制回放脚本。2、Selenium Remote Control(RC):可以采用各种编程语言编写自动化测试脚本并运行在多种浏览器上。Selenium RC的核心是一个HTTP代理(proxy)、以及一个可以由各种编程语言调用的类库,Selenium RC的架构示意图如图所示:3、Selenium Grid:可以让Selenium Remote Control的自动化测试脚本并行在多个服务器上执行,节省大量运行测试的时间。Selenium支持多款浏览器的各种版本,包括:Firefox 3、Firefox 2、IE8、IE7、Safari 3、Safari 2、Opera 9、Opera 8等。并且支持使用多种编程语言开发脚本,包括:C#、Java、Perl、PHP、Python、Ruby等。下面介绍如何使用Selenium开发Web自动化测试脚本。1、下载Firefox的插件Selenium IDE(/download/)后直接打开Firefox并把Selenium IDE安装文件拖拽进去,重启Firefox后,就可以在Tools-Selenium IDE菜单项中找到并打开Selenium IDE进行脚本的录制,如图所示。录制的脚本可以保存成TestCase,还可以导出成指定语言的Selenium RC脚本,例如,如果想把刚才录制的测试用例导出成C#脚本,则可以选择Selenium IDE中的菜单“File-Export Test Case As - C# - Selenium RC”,导出的C#脚本如下所示:using System;using System.Text;using System.Text.RegularExpressions;using System.Threading;using NUnit.Framework;using Selenium;namespace SeleniumTestsTestFixturepublic class GoogleTestprivate ISelenium selenium;private StringBuilder verificationErrors;SetUppublic void SetupTest()selenium = new DefaultSelenium(localhost, 4444, *chrome, http:/change-this-to-the-site-you-are-testing/);selenium.Start();verificationErrors = new StringBuilder();TearDownpublic void TeardownTest()tryselenium.Stop();catch (Exception)/ Ignore errors if unable to close the browserAssert.AreEqual(, verificationErrors.ToString();Testpublic void TheGoogleTest()selenium.Open(/);selenium.Type(q, Selenium);selenium.Click(btnG);这些代码可以作为基本的录制脚本,在VS2005中进行后续的编辑和修改。2、下载Selenium RC。在安装之前需要确保1.5版本以上的JDK已经正确安装。把Selenium RC的文件包解压缩到某个目录,然后在命令行中转到其中的selenium-server-0.9.2目录,运行java -jar selenium-server.jar来启动Selenium RC,如图所示。图 启动Selenium RC启动后即可以编写测试类来使用Selenium RC,从而访问和控制各种浏览器进行Web页面的功能测试。技巧:如果碰到启动不成功的情况,则很可能是因为端口号问题,可尝试使用不同的端口号来启动Selenium RC,默认端口号为4444,可以使用参数-port,改成其它端口号,如图所示。图 指定端口启动Selenium RC3、Selenium RC支持在多种编程语言下编写测试脚本,如果想使用C#进行脚本的编写,则可以在Selenium RC的安装目录下找到selenium-dotnet-client-driver-0.9.2目录中的ThoughtWorks.Selenium.Core.dll,并加到C#的项目中,然后在C#代码中添加如下脚本:using System;using System.Collections.Generic;using System.Windows.Forms;/ 使用Selenium 的接口using Selenium;namespace SeleniumTest1 static class Program static void Main() / 创建Selenium的对象实例 ISelenium selenium; / 启动firefox浏览器并导航到Google网站 selenium = new DefaultSelenium(localhost, 5555, *firefox C:Program FilesMozilla Firefoxfirefox.exe, .hk/); / 开始测试 selenium.Start(); / 打开指定的页面 selenium.Open(.hk/webhp); / 检查页面的Title属

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论