首页 Watir学习手册

Watir学习手册

举报
开通vip

Watir学习手册 TIB 自动化测试工作室 http://www.cnblogs.com/testware/ 简介 ..................................................................................................................................................2 Watir vs. SilkTest ...................................

Watir学习手册
TIB 自动化测试工作室 http://www.cnblogs.com/testware/ 简介 ..................................................................................................................................................2 Watir vs. SilkTest ......................................................................................................................3 watir vs. Selenium.....................................................................................................................4 Watir vs. Selenium vs. Sahi.......................................................................................................5 Goodbye Selenium,Hello Watir ................................................................................................6 Watir and Selenium - Bret Pettichord .......................................................................................6 安装 ..................................................................................................................................................8 ruby1.9.1+watir1.6.5.................................................................................................................8 ruby1.8.6+watir1.6.5...............................................................................................................12 ruby1.8.6+watir1.5.2(成功!) ................................................................................................13 Fxri – Interactive Ruby Help & Console ................................................................................14 帮助文档.................................................................................................................................14 录制器.....................................................................................................................................15 WatirRecorder++.............................................................................................................15 testwise(iTest2) ..........................................................................................................15 Watir WebRecorder ......................................................................................................16 GlobalWatch Script Recorder .....................................................................................16 中文问 快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题 .................................................................................................................................16 基础使用.........................................................................................................................................17 HelloWorld..............................................................................................................................17 google测试例子......................................................................................................................18 link ..........................................................................................................................................18 检查点.....................................................................................................................................18 contains_text ...................................................................................................................18 assert ...............................................................................................................................19 Rspec...............................................................................................................................21 命令行运行.............................................................................................................................22 IE窗口处理.............................................................................................................................23 JavaScript窗口处理 ................................................................................................................23 整合AutoIt处理JavaScript弹出窗口..............................................................................24 获取IE浏览器版本和操作系统版本 .....................................................................................25 Watir可读取和与之交互的HTML页面元素.........................................................................27 高级使用.........................................................................................................................................28 用Watir测试Mercury Tours ....................................................................................................28 Excel .......................................................................................................................................33 Object Map(对象映射)......................................................................................................34 How do I create an application/object map?.......................................................35 创建Map类 .....................................................................................................................35 JavaScript ................................................................................................................................36 调用JavaScript脚本 ........................................................................................................36 How do I trigger JavaScript events?..........................................................................37 How do I interact with a JavaScript tree view? ........................................................37 Watir+XPath............................................................................................................................38 Watir框架、扩展、工具 ................................................................................................................38 数据驱动.................................................................................................................................39 关键字驱动.............................................................................................................................39 Rspec.......................................................................................................................................39 WatirCraft................................................................................................................................39 WET........................................................................................................................................40 360WebTester..........................................................................................................................41 简介 Watir is a testing tool for automating browser-based tests of web applications. It is a Ruby library that works with Internet Explorer and Firefox. http://rubyforge.org/projects/wtr/ 目前最新版本是:watir1.6.5 November 10, 2009 下载地址:http://rubyforge.org/frs/?group_id=104&release_id=41300 Watir 简介 "Watir" (发音与 water 相近) 全写是 "Web Application Testing in Ruby"。Watir 是一款用 Ruby 脚本语言驱动浏览器的自动化测试工具。Wait 是基于 Web 的自动化测试开发的工具箱。 Watir 对什么有效? Watir 可以驱动那些作为 Html 页面被发送到 Web 浏览器端的应用程序。 Watir 对下列组件不起作用:ActiveX、Java Applets、Macromedia Flash 或者其他的应用程序 插件。 判断 Watir 是否可用的方法:在页面上点击右键-查看页面源代码,如果可以看到 Html 源代 码,就说明页面上的对象可以被 Watir 识别,来实现自动化。 使用的前提条件 要使用 Watir,至少要掌握以下内容 1. HTML: 2. 编程的基本常识,如变量的定义与使用,基本的控制语句,如 If、for 等 3. Ruby:Ruby 的基本常识。 4. IE Development 或是其他类似的浏览器辅助工具,在以后的开发中,将非常有效的帮你识 别页面对象的属性。 Watir vs. SilkTest 参考: http://wiki.openqa.org/display/WTR/Comparison+of+Watir+with+Silk+Test 这篇文档从脚本运行时间、可靠性、可维护性、代码行数这几个方面比较了一下 Watir 和 SilkTest。不过由于文章是 2005 年写的,今天无论是 Watir 还是 SilkTest 都有了更多的改进, 因此仅作参考用。 Two scripts are being used to do the comparison. --> What type of an app was being tested? Where are the samples of these 2 scripts? We used the following criteria to compare the two tools: 1. Elapsed time 2. Reliability 3. Maintainability 4. Lines of Code Each script was run 15 times to get the elapsed time and test out reliability. Import script Watir SilkTest Lines of Codes 60 lines of code 200+ Elapsed Time (average of 15 runs) 25 seconds 60 seconds Reliability No failure when ran 15 times in a row 87% with 15 runs Maintainability All changes are readily "seen" via View Source or getHTML command. Compare files can be done and changes can easily be made. Changes in B1 code require updates to the page declarations. This can take between 10 and 30 minutes. Analysis/Positions script Watir SilkTest Lines of Codes 105 lines of code 250+ Elapsed Time (average of 15 runs) 52 seconds 120 seconds Reliability No failure when ran 15 times in a row 80% with 15 runs Maintainability All changes are readily "seen" via View Source or getHTML command. Compare files can be done and changes can easily be made. Changes in B1 code require updates to the page declarations. This can take between 10 and 30 minutes. watir vs. Selenium 参考:http://www.newsqa.com/watir-vs-selenium/ Watir 本质上就是封装了 IE COM 接口的 Ruby 库。 Watir is a Ruby library that wraps the COM interface to Internet Explorer. COM is a long-standing Windows-based technology for making libraries accessible to various languages. This allows access to the Document Object Model (DOM) so it doesn’t matter where on the page an object exists, what matters is how you identify it (id, name, title, etc). Selenium 驱动浏览器的方式与 Watir 不一样,Selenium 的自动化引擎是用 JavaScript 写的, 并且运行在浏览器内。 Selenium uses a unique mechanism for driving browsers. Selenium’s automation engine is written in JavaScript and runs inside a browser. The engine, called a browser bot, is embedded in a page that accesses the application under test in a separate frame. Because of cross-site scripting, Selenium’s browser bot has to be served from the same site as the application under test – requiring installation on the server. Selenium 支持命令语言,称为“Selenese”。 Selenium supports a command language, called Selenese. Tests can be embedded in an HTML table, which will be read in by the browser bot and then executed. Watir 和 Selenium 都是直接在浏览器中执行测试,并且可以在浏览器最小化的状态下运行。 Both tools run tests directly in a browser, and both do it in a way that allows the browser to be minimized while the tests are running, which means you don’t have to dedicate a machine to running tests. Watir 采用 Ruby 语言,容易学习,起先仅支持 IE,现在支持 FF。 Watir was designed to be easy to learn, it allows page elements to be identified by index, name, ID, value or adjacent text. Complaint: Watir is currently limited to IE browsers on Windows. There are efforts underway to support Firefox in the future. Selenium 一开始就设计成支持多浏览器和多平台。Selenium 需要服务器端安装。 Selenium was designed for breadth of coverage- multiple browsers and platforms. It was expected to be used by the same developers who built the application. Complaint: Selenium requires a server-side installation. Selenium 支持多种编程语言。 You can also express Selenium tests in a programming language, taking advantage of language-specific drivers that communicate in Selenese to the browser bot. Watir vs. Selenium vs. Sahi 参考:http://stackoverflow.com/questions/606550/watir-vs-selenium-vs-sahi Watir Pros: * It’s a Ruby library * Multi browser (& OS) support * Has a rich API * Has a ‘Simple’ class (for non-tech users) * Watij & Watin (Java & .NET) Cons: * Have to learn Ruby (unless you choose Watij or Watin) * Every browser requires a different library Selenium Pros: * Multi browser, OS & language support * Install server-side or as FF add-on * Has its own IDE * Record and playback tests Cons: * Have to learn a vendorscript => Selenese (Unless you write tests in another language which it supports. Then you just have to use the API reference which is straightforward.) Sahi Pros: * Multi browser support * Has its own IDE * Record and playback tests Cons: * Confusing interface * Least developed/smallest community Goodbye Selenium,Hello Watir Jason Darling http://blogs.dovetailsoftware.com/blogs/jason_darling/archive/2007/03/23/goodbye-selenium-hell o-watir.aspx Watir and Selenium - Bret Pettichord Watir 和 Selenium 都是可用于网页自动化测试的开源工具,两者都非常优秀,它们之间有何 异同,到底要如何取舍呢? 这两者都是直接在浏览器中运行脚本,并允许在脚本运行时将浏览器最小化,这意味着你不 需要因为自动化测试而占用一台机器的资源。Watir 和 Selenium 都可应用于网页自动化测试, 两者差异也十分明显,这些 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 现上的区别是由它们最初的设计思想和设定目标所导致的。 Watir 是由”Scripting for Testers”类演化而来,我们最初的构想是这个类可以帮助测试人员 跨越自动化测试的障碍。Ruby 简单的语法和交互式命令行处理程序(IRB)成就了它易于 教授学习的特点。而在各种各样的自动化任务中,我们发现测试人员最感兴趣的是直接对浏 览器进行操作,所以这个类逐渐沉淀为网页测试。Watir 就是这个类对浏览器进行操作的最 新版类库。举例来说,它使页面元素可被 index,name,id,value 或邻近 text 所识别。但是,我 们开发 watir 所运用的技术方法使得它只能运行于 windows 的 IE 浏览器上(现在已经可以 支持 FireFox)。 Selenium 的作者 Jason Huggins 却有着非常不同的目标设定。他最初的想法是开发一个能让 他测试浏览器兼容性的工具,他曾在 ThoughtWorks 开发过一个时间监控系统。对于大多数 公司来说,大多数开发组都会先选择一种浏览器,使用其开发、测试应用,然后告诉这个应 用的用户使用方法。这些工作在 ThoughtWorks 并不是如看上去那么简单。开发们是固执的 一群人,他们不喜欢由别人来告诉他们使用什么工具。尽管公司提供的是使用 Windows 平 台的笔记本,很多人还是会坚持使用他们更喜欢的 Mac 或者 Linux。所以对于 Jason 来说, 能测试不同平台上的不同浏览器是很重要的。 ThoughtWorks 的其他 Selenium 开发者也希望 将他们的 web 测试需求增加到这个工具中。 简单说来,Watir 的设计思想是便于使用,它最初设计为一个教学工具,尽管有浏览器和平 台的限制,还是可被用户接受的。Selenium 为覆盖范围所设计,目标用户是开发了这个应用 的开发者们。 许多 Watir 用户报告说他们可以快速入门 Watir 并使用其编写、执行用例,尽管他们并不熟 知 ruby。而公众对 Selenium 的认知是它的确是一个有前途的工具。 对 Watir 最大的抱怨来源于它对 Windows 和 IE 的限制,对 Selenium 的反对声音来自于需要 在服务器端进行安装。在其他方面,这两个工具的最主要区别在于架构。Watir 的架构已经 常被使用,它得益于类似工具的开发经验。Selenium 是作为一个新发现的代表。迄今为止, 从没有任何类似于 Selenium 的工具。这使其发展伴随着阵痛,因为人们需要熟悉使用他独 特的方法。 从技术上说,Watir 是一个封装了 COM 接口的 Ruby 类库。COM 是长期存在的基于 windows 上的技术,用来使类库可被多种语言调用(OLE 和 ActiveX 是和 COM 类似的概念)。和大 多数语言类似,ruby 可以访问 COM 接口。IE 的 COM 接口允许程序访问浏览器的文档对象 模型(DOM)。由于使用 DOM 技术,我们不需要担心屏幕位置,或希望访问的元素是不是 被另一窗口遮挡,所以基于 Watir 的测试可以在最小化窗口中运行。许多人也创建了凭借 IE 的 COM 接口来访问 DOM 的工具,我称它们为 COM/DOM 驱动,Samie 和 Jiffie 就是两种 开源的 COM/DOM 驱动,分别使用 Perl 和 Java 语言。 使 Watir 在众多 COM/DOM 驱动中脱颖而出的是它可以满足不会技术的用户的期望,这是 Paul Rogers 和我的初衷。这不像 Samie,当它应该等待浏览器载入下一个界面时,程序会自 动过早进行下一步操作。Watir 提供了可使用 ruby 交互式命令行(IRB——一种基于 Ruby 和 Python 的语言特性,但并不支持其他主流编程语言)的几种命令。从命令行中调用命令 可以帮助了解他们为之编写脚本的网页的结构。Watir 还有一个技术交流社区,会产出新的 特性和文档等。 Selenium 使用一种独特的机制来驱动浏览器。 Selenium 的自动化引擎使用 Javascript 编写, 嵌入在浏览器的内部执行。所有主流的浏览器都支持 Javascript,因此它们都可以为 Selenium 所驱动。这个自动化引擎(称为 Browser Bot),被嵌入在一个可以在测试中以独特的框架访 问应用的页面中。这在实施过程中有一个障碍,因为跨站脚本的存在,Selenium 一定要在服 务端进行安装。 尽管 Selenium 是 Javascript 编写的,Selenium 的测试脚本却并不如此。Selenium 支持一种叫 Selenese 的命令语言。它们可以被嵌入在一个 HTML 的 Table 中,并被 browser bot 所读取、 执行。 你也可以使用编程语言来编写 Selenium 脚本,这得益于 Selenese 和 browser bot 通信时使用 的特定语言驱动。目前支持使用 Java/Ruby/.net/Python 等语言来编写脚本。 Selenium 的服务器安装使得很多测试人员退却。开发者们似乎却并不介意。熟悉服务端配 置的开发者们在他们特定的服务器上安装配置 Selenium 时并不会遇到很多麻烦。逐渐我们 会看到越来越多关于如何在典型的网站应用架构中安装 Selenium 的文档。我们也会看到 Selenium 将被封装的更易于安装。但是测试人员的拒绝似乎并不只是因为安装。一些测试人 员不愿意使用 Selenium 只是因为他们并不总会有访问他们所测试应用的服务端的权限。我 想其他人之所以拒绝,是因为这样一种方式会干扰测试结果的验证。 我也不会奇怪如果我们看到一种将 Selenium 从服务端卸载的方法,但迄今还没有发布。它 的原型所建立的机制就是在使浏览器认为 web 应用是由 Selenium 服务器发起的,甚至当这 个应用有他自己的 host 时。 Selenium 开始支持越来越多的浏览器,例如 Safari 和 Opera。尽管 Browser bot 可以在这些 浏览器中运行,也仍然需要为不同的浏览器进行特殊定制。因为各浏览器之间的 DOM 总是 有轻微的区别。Selenium 比较人性化的一点是他的所有定制都在 browser bot 本身,Selenese 命令在所有支持浏览器中都可运行。 我的 计划 项目进度计划表范例计划下载计划下载计划下载课程教学计划下载 是允许 Watir 调用 Selenium 的 Ruby 驱动来驱动浏览器,以此实现 Watir 对多浏览 器多平台的支持。这当中有一个难题是 IE/COM 比 Selenium 提供了更细粒度的接口,我预 料这个项目进展时会需要更多新的 Selenese 命令的支持,会需要一些开发时间。 同时,用户必须在两者之间进行一次抉择,是对测试人员友好,但是有运行浏览器/平台限 制的 Watir,还是支持多浏览器,使用和安装却没那么简单的 Selenium。 安装 ruby1.9.1+watir1.6.5 1、安装 Ruby http://rubyforge.org/ 下载 Windows 下的 Ruby 安装包: http://rubyforge.org/projects/rubyinstaller/ A self-contained installer that includes the Ruby language, several popular extensions, an HTML Help based API reference, and the "Book of Ruby" courtesy of Huw Collingbourne. Platforms: Windows 2000/XP/Vista/7. 下载 1.9.1 版本: http://rubyforge.org/frs/download.php/66872/rubyinstaller-1.9.1-p243-rc1.exe 安装过程: 2、安装 Watir 参考: http://www.advidea.cn/biancheng/2009413211418.html gem install watir 不成功,提示 ERROR: Failed to build gem native extension. 换这个 gem 安装成功 http://www.advidea.cn/biancheng/200943135232.html 但是如何使用最新版本的 watir 呢?!而且编写 watir 脚本后运行提示找不到 “msvcrt-ruby18.dll”: ruby1.8.6+watir1.6.5 参考: http://www.51testing.com/?uid-21468-action-viewspace-itemid-95660 卸载 ruby1.9.1 1、安装 ruby1.8.6 2、安装 watir1.6.5 转到 watir1.6.5 所在的目录进行安装: gem install watir-1.6.5.gem 还是不行,提示相同的错误! 转而用在线安装方式: gem update –system gem install watir 也不行,提示相同的错误!! 还是用 advidea.bat 才行!!! 但是这次执行 watir 脚本还是有问题,提示“watir(loadError)”: ruby1.8.6+watir1.5.2(成功!) 下载并安装这个 ruby 安装包: http://rubyforge.org/frs/download.php/29263/ruby186-26.exe 下载并安装 watir1.5.2: http://rubyforge.org/frs/download.php/24880/watir-1.5.2.gem 更新 gem: gem update –system 去到 watir1.5.2 所在的目录,执行: gem install watir-1.5.2.tar 编写一个 HelloWorld.rb: require 'watir' test_site = 'http://blog.csdn.net/testing_is_believing/' # open the IE browser ie = Watir::IE.new # print some comments puts "## Beginning of test" puts " " puts "Step 1: go to the test site: " + test_site ie.goto(test_site) puts " Action: entered " + test_site + " in the address bar." 尝试运行成功! Fxri – Interactive Ruby Help & Console 帮助文档 C:\ruby\lib\ruby\gems\1.8\doc\watir-1.5.2\rdoc\index.html http://wiki.openqa.org/display/WTR/Tutorial http://wiki.openqa.org/display/WTR/Project+Home 录制器 WatirRecorder++ http://www.hanselman.com/blog/content/binary/WatirRecorder_Setup_lite.msi WatirMaker is a prototype of an action recorder for Watir, written in C#. Public domain. http://www.hanselman.com/blog/WatirMakerVersion001Source.aspx testwise(iTest2) http://itest2.com/ Watir WebRecorder WebRecorder is an action recorder for web applications. A version that creates Watir scripts was released in February 2006. A handy tool to help learn Watir. A horrible idea if you think this is going to allow you to create scripts without understanding the code. Free, but not open source. http://www.mjtnet.com/watir_webrecorder.htm 现在好像没了! GlobalWatch Script Recorder The GlobalWatch Script Recorder allows users to record and playback their navigation through a series of steps on any website or application to generate a simple, human readable, Watir-compatible script. 中文问题 参考: http://hi.baidu.com/xieoy/blog/item/2774ef34730634b3d0a2d3dd.html http://www.cnblogs.com/slaughter/archive/2007/10/18/929385.html 1.打开 watir.rb 2.在 class TextField 中加入一个新的 method: def characters_in(value) index = 0 while index < value.length len = value[index] > 128 ? 2 : 1 yield value[index, len] index += len end end 3.更改 class TextField 的 doKeyPress( value )方法部分代码,将下面代码 ------------------------------------------- for i in 0 .. value.length-1 sleep @container.typingspeed c = value[i,1] @container.log " adding c.chr " + c @o.value = @o.value.to_s + c @o.fireEvent("onKeyDown") @o.fireEvent("onKeyPress") @o.fireEvent("onKeyUp") end 替换为如下代码 characters_in(value) {|c| sleep @container.typingspeed @o.value = @o.value.to_s + c @o.fireEvent("onKeyDown") @o.fireEvent("onKeyPress") @o.fireEvent("onKeyUp") } 现在脚本支持中文了: require 'watir' check_text = "Watir 测试框架" #定义变量 ie = Watir::IE.new # 打开一个 IE 浏览器 ie.goto("http://www.google.cn") #打开 google 首页 ie.text_field(:name,"q").set(check_text) #输入搜索内容 sleep(1) ie.button(:name,"btnG").click # 点击按钮"Google 搜索" sleep(1) if ie.contains_text("搜索 #{check_text} 获得约") #判断页面是否包含相应文字 puts "搜索成功!" else puts "搜索失败!" end ie.close #关闭 IE 窗口 基础使用 HelloWorld require 'watir' test_site = 'http://blog.csdn.net/
本文档为【Watir学习手册】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_788441
暂无简介~
格式:pdf
大小:754KB
软件:PDF阅读器
页数:42
分类:互联网
上传时间:2012-03-21
浏览量:18