- 浏览: 3049580 次
- 性别:
- 来自: 海外
文章分类
- 全部博客 (430)
- Programming Languages (23)
- Compiler (20)
- Virtual Machine (57)
- Garbage Collection (4)
- HotSpot VM (26)
- Mono (2)
- SSCLI Rotor (1)
- Harmony (0)
- DLR (19)
- Ruby (28)
- C# (38)
- F# (3)
- Haskell (0)
- Scheme (1)
- Regular Expression (5)
- Python (4)
- ECMAScript (2)
- JavaScript (18)
- ActionScript (7)
- Squirrel (2)
- C (6)
- C++ (10)
- D (2)
- .NET (13)
- Java (86)
- Scala (1)
- Groovy (3)
- Optimization (6)
- Data Structure and Algorithm (3)
- Books (4)
- WPF (1)
- Game Engines (7)
- 吉里吉里 (12)
- UML (1)
- Reverse Engineering (11)
- NSIS (4)
- Utilities (3)
- Design Patterns (1)
- Visual Studio (9)
- Windows 7 (3)
- x86 Assembler (1)
- Android (2)
- School Assignment / Test (6)
- Anti-virus (1)
- REST (1)
- Profiling (1)
- misc (39)
- NetOA (12)
- rant (6)
- anime (5)
- Links (12)
- CLR (7)
- GC (1)
- OpenJDK (2)
- JVM (4)
- KVM (0)
- Rhino (1)
- LINQ (2)
- JScript (0)
- Nashorn (0)
- Dalvik (1)
- DTrace (0)
- LLVM (0)
- MSIL (0)
最新评论
-
mldxs:
虽然很多还是看不懂,写的很好!
虚拟机随谈(一):解释器,树遍历解释器,基于栈与基于寄存器,大杂烩 -
HanyuKing:
Java的多维数组 -
funnyone:
Java 8的default method与method resolution -
ljs_nogard:
Xamarin workbook - .Net Core 中不 ...
LINQ的恶搞…… -
txm119161336:
allocatestlye1 顺序为 // Fields o ...
最近做的两次Java/JVM分享的概要
iaimstar 写道
@RednaxelaFX 你最近ruby魔障了?
好吧……那魔障给你看看。
这次的玩法是使用Watir打开浏览器到Google上搜索iaimstar,并用RSpec来确认行为。
首先确保以安装Watir和RSpec这两个gem:
gem install watir rspec
然后编写一个spec。首先确认一下希望看到什么结果,并且达成该结果需要经过怎样的流程:
预期结果:在Google上搜iaimstar应当看到他JavaEye的blog为第一个结果。
流程:
1、打开浏览器;
2、到Google中文站
3、在搜索框中输入“iaimstar”;
4、点击搜索按钮;
5、找到第一个搜索结果;
6、该结果应该指向“http://iaimstar.iteye.com/”;
最后记得浏览器要关掉。
任意新建一个Ruby源文件(本例中叫watir_spec.rb),把上述流程写成代码。
watir_spec.rb:
require 'rubygems' require 'watir' require 'spec' describe Watir::Browser do it "should get iaimstar's JavaEye blog as the first Google search result" do begin browser = Watir::Browser.new browser.goto 'http://g.cn' browser.text_field(:name, 'q').set 'iaimstar' browser.button(:name, 'btnG').click anchor = browser.element_by_xpath 'div#cnt/div#res.med/div/ol/li.g/h3.r/a.l[1]' anchor.href.should == 'http://iaimstar.iteye.com/' ensure browser.close end end end
在命令行使用spec命令来执行测试:
spec watir_spec.rb --format specdoc
一切顺利的话,可以看到输出结果为:
Watir::Browser - should get iaimstar's JavaEye blog as the first Google search result Finished in 6.694 seconds 1 example, 0 failures
如图:
(这个测试是昨晚做的,发帖时Google的搜索结果发生了变化,JavaEye的结果跑到第二位了。有兴趣的同学可以照着做一次这个测试,观察一下测试失败时的输出)
关于Watir支持的操作,可以参考Watir出猫纸
另外,刚装上Nokogiri 1.4.0的同学可能会碰到这种警告:
C:/Ruby/lib/ruby/gems/1.8/gems/nokogiri-1.4.0-x86-mingw32/lib/nokogiri/xml/builder.rb:272: warning: parenthesize argument(s) for future version
正好在JavaEye问答频道有同学问了这个问题,请参考:http://www.iteye.com/problems/29853
===========================================================================
不过既然有要达成某个结果有清晰的流程,那干脆把流程在代码里再写得清晰些。
确保安装上Cucumber、Rake、Win32::Console这几个gem:
gem install cucumber rake win32console
先随便建一个目录,以存放本例的文件;我是放在了D:\temp_code\test_cucumber。
编写一个rakefile来自动化测试的执行:
rakefile:
require 'rubygems' require 'cucumber/rake/task' Cucumber::Rake::Task.new :features do |t| t.cucumber_opts = "features --format pretty" end
然后在rakefile所在目录下再创建一个features目录以存放“功能描述”。
可以在rakefile所在目录运行cucumber试试看:
D:\temp_code\test_cucumber>rake features (in D:/temp_code/test_cucumber) C:/Ruby/bin/ruby.exe -I "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/lib;lib" "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/bin/cucumber" features --format pretty 0 scenarios 0 steps 0m0.000s
这说明rakefile编写正确,而当前尚未有需要测试的功能。
那么就来添加一个功能描述文件。在features目录里创建一个.features文件。Feature块表示一个功能点,其中可以包含一到多个Scenario。Scenario就是使用到某功能点的案例,用Given、When、Then来描述流程。其中Given表示前置条件,在这里可以建立好测试需要的环境;When表示场景中的步骤;Then表示后置条件,也就是检查结果的正确性。如果多个Scenario在开始出含有共同的步骤,可以抽出来放到Background块里。把可能有变化的描述写在双引号内。
features/search_friends.feature
Feature: search for friends on Google I want to know where my friends' blogs are So that I can read their blog posts Background: Given I start up a browser And I goto "http://g.cn" Scenario: search for iaimstar When I enter "iaimstar" in the search field And I click the search button Then the first search result exists And it should read "尘星似海:囧冏有神的java空间- JavaEye技术网站" And it should point to "http://iaimstar.iteye.com/" Scenario: search for RednaxelaFX When I enter "RednaxelaFX" in the search field And I click the search button Then the first search result exists And it should read "Script Ahead, Code Behind - JavaEye技术网站" And it should point to "http://rednaxelafx.iteye.com/"
再回到rakefile所在目录运行一次rake:
D:\temp_code\test_cucumber>rake features (in D:/temp_code/test_cucumber) C:/Ruby/bin/ruby.exe -I "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/lib;lib" "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/bin/cucumber" features --format pretty Feature: search for friends on Google I want to know where my friends' blogs are So that I can read their blog posts Background: # features/search_friends.feature:5 Given I start up a browser # features/search_friends.feature:6 And I goto "http://g.cn" # features/search_friends.feature:7 Scenario: search for iaimstar # features/search_friends.feature:9 When I enter "iaimstar" in the search field # features/search_friends.feature:10 And I click the search button # features/search_friends.feature:11 Then the first search result exists # features/search_friends.feature:12 And it should read "尘星似海:囧冏有神的java空间- JavaEye技术网站" # features/search_friends.feature:13 And it should point to "http://iaimstar.iteye.com/" # features/search_friends.feature:14 Scenario: search for RednaxelaFX # features/search_friends.feature:16 When I enter "RednaxelaFX" in the search field # features/search_friends.feature:17 And I click the search button # features/search_friends.feature:18 Then the first search result exists # features/search_friends.feature:19 And it should read "Script Ahead, Code Behind - JavaEye技术网站" # features/search_friends.feature:20 And it should point to "http://rednaxelafx.iteye.com/" # features/search_friends.feature:21 2 scenarios (2 undefined) 14 steps (14 undefined) 0m0.032s You can implement step definitions for undefined steps with these snippets: Given /^I start up a browser$/ do pending # express the regexp above with the code you wish you had end Given /^I goto "([^\"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end When /^I enter "([^\"]*)" in the search field$/ do |arg1| pending # express the regexp above with the code you wish you had end When /^I click the search button$/ do pending # express the regexp above with the code you wish you had end Then /^the first search result exists$/ do pending # express the regexp above with the code you wish you had end Then /^it should read "([^\"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end Then /^it should point to "([^\"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end If you want snippets in a different programming language, just make sure a file with the appropriate file extension exists where cucumber looks for step definitions.
如图:
可以看到有了描述的场景在还没有代码去实现它的时候是黄色的。
在features目录下创建一个step_definitions目录,并创建search_friends_step.rb文件来实现测试的场景。可以复制Cucumber的提示的实现代码到该文件中:
features/steps/search_friends_step.rb
Given /^I start up a browser$/ do pending # express the regexp above with the code you wish you had end Given /^I goto "([^\"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end When /^I enter "([^\"]*)" in the search field$/ do |arg1| pending # express the regexp above with the code you wish you had end When /^I click the search button$/ do pending # express the regexp above with the code you wish you had end Then /^the first search result exists$/ do pending # express the regexp above with the code you wish you had end Then /^it should read "([^\"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end Then /^it should point to "([^\"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end
再运行rake,可以看到所有步骤都有实现了,不过每一步的实现都是pending,所以测试到第一个pending之后,后面的步骤就被跳过(skip)了:
D:\temp_code\test_cucumber>rake features (in D:/temp_code/test_cucumber) C:/Ruby/bin/ruby.exe -I "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/lib;lib" "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/bin/cucumber" features --format pretty Feature: search for friends on Google I want to know where my friends' blogs are So that I can read their blog posts Background: # features/search_friends.feature:5 Given I start up a browser # features/step_definitions/search_friends_step.rb:1 TODO (Cucumber::Pending) ./features/step_definitions/search_friends_step.rb:2:in `/^I start up a browser$/' features/search_friends.feature:6:in `Given I start up a browser' And I goto "http://g.cn" # features/step_definitions/search_friends_step.rb:5 Scenario: search for iaimstar # features/search_friends.feature:9 When I enter "iaimstar" in the search field # features/step_definitions/search_friends_step.rb:9 And I click the search button # features/step_definitions/search_friends_step.rb:13 Then the first search result exists # features/step_definitions/search_friends_step.rb:17 And it should read "尘星似海:囧冏有神的java空间- JavaEye技术网站" # features/step_definitions/search_friends_step.rb:21 And it should point to "http://iaimstar.iteye.com/" # features/step_definitions/search_friends_step.rb:25 Scenario: search for RednaxelaFX # features/search_friends.feature:16 When I enter "RednaxelaFX" in the search field # features/step_definitions/search_friends_step.rb:9 And I click the search button # features/step_definitions/search_friends_step.rb:13 Then the first search result exists # features/step_definitions/search_friends_step.rb:17 And it should read "Script Ahead, Code Behind - JavaEye技术网站" # features/step_definitions/search_friends_step.rb:21 And it should point to "http://rednaxelafx.iteye.com/" # features/step_definitions/search_friends_step.rb:25 2 scenarios (1 skipped, 1 pending) 14 steps (13 skipped, 1 pending) 0m0.026s
如图:
被跳过的步骤用蓝色表示。
现在只剩把测试的实现与测试对象组装到一起了。为了准备好要用的库,在features目录下创建support目录,在里面创建env.rb文件:
features/support/env.rb
require 'rubygems' require 'watir' require 'iconv'
然后给先前创建的search_friends_step.rb添加逻辑:
features/steps/search_friends_step.rb
Given /^I start up a browser$/ do @browser = Watir::Browser.new end Given /^I goto "([^\"]*)"$/ do |url| @browser.goto url end When /^I enter "([^\"]*)" in the search field$/ do |keyword| @browser.text_field(:name, 'q').set keyword end When /^I click the search button$/ do @browser.button(:name, 'btnG').click end Then /^the first search result exists$/ do @result = @browser.element_by_xpath 'div#cnt/div#res.med/div/ol/li.g/h3.r/a.l[1]' @result.should_not be(nil) end Then /^it should read "([^\"]*)"$/ do |title| Iconv.conv('gbk', 'utf-8', @result.innerText).should == title end Then /^it should point to "([^\"]*)"$/ do |expected_url| @result.href.should == expected_url end After do @browser.close end
此时目录结构应该像这样:
test_cucumber │ rakefile │ └─features │ search_friends.feature │ ├─step_definitions │ search_friends_step.rb │ └─support env.rb
再运行rake,可以看到:
D:\temp_code\test_cucumber>rake features (in D:/temp_code/test_cucumber) C:/Ruby/bin/ruby.exe -I "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/lib;lib" "C:/Ruby/lib/ruby/gems/1.8/gems/cucumber-0.4.4/bin/cucumber" features --format pretty Feature: search for friends on Google I want to know where my friends' blogs are So that I can read their blog posts Background: # features/search_friends.feature:5 Given I start up a browser # features/step_definitions/search_friends_step.rb:1 And I goto "http://g.cn" # features/step_definitions/search_friends_step.rb:5 Scenario: search for iaimstar # features/search_friends.feature:9 When I enter "iaimstar" in the search field # features/step_definitions/search_friends_step.rb:9 And I click the search button # features/step_definitions/search_friends_step.rb:13 Then the first search result exists # features/step_definitions/search_friends_step.rb:17 And it should read "尘星似海:囧冏有神的java空间- JavaEye技术网站" # features/step_definitions/search_friends_step.rb:22 expected: "\263\276星似\272\243:\207鍍子猩馶265膉ava\277占? JavaEye\274\274术网站", got: "iaimstar - ChinaonRails" (using ==) Diff: @@ -1,2 +1,2 @@ -尘星似海:囧冏有神的java空间- JavaEye技术网站 +iaimstar - ChinaonRails (Spec::Expectations::ExpectationNotMetError) ./features/step_definitions/search_friends_step.rb:23:in `/^it should read "([^\"]*)"$/' features/search_friends.feature:13:in `And it should read "尘星似海:囧冏有神的java空间- JavaEye技术网站"' And it should point to "http://iaimstar.iteye.com/" # features/step_definitions/search_friends_step.rb:26 Scenario: search for RednaxelaFX # features/search_friends.feature:16 When I enter "RednaxelaFX" in the search field # features/step_definitions/search_friends_step.rb:9 And I click the search button # features/step_definitions/search_friends_step.rb:13 Then the first search result exists # features/step_definitions/search_friends_step.rb:17 And it should read "Script Ahead, Code Behind - JavaEye技术网站" # features/step_definitions/search_friends_step.rb:22 And it should point to "http://rednaxelafx.iteye.com/" # features/step_definitions/search_friends_step.rb:26 Failing Scenarios: cucumber features/search_friends.feature:9 # Scenario: search for iaimstar 2 scenarios (1 failed, 1 passed) 14 steps (1 failed, 1 skipped, 12 passed) 0m11.862s rake aborted! Command failed with status (1): [C:/Ruby/bin/ruby.exe -I "C:/Ruby/lib/ruby/...] (See full trace by running task with --trace)
如图:
Well .. . . 这次就玩到这里。Have fun ^_^
上面用RSpec和Cucumber的测试用文件打包放在附件里了
(嗯……原来这边用的测试框架是Test::Unit,我还以为会用到RSpec的说……)
评论
11 楼
RednaxelaFX
2010-12-31
Hooopo 写道
watir用来做网页抓取也不错喔 特别是一些ajax的
就是有点慢。如果能从Ruby直接跟一个Webkit的DOM计算+JavaScript引擎的部分连接到一起就好了,抓网页肯定超方便
10 楼
Hooopo
2010-12-31
watir用来做网页抓取也不错喔 特别是一些ajax的
9 楼
try001
2010-01-08
RednaxelaFX 写道
yuan 写道
FX比较过Selenium和Watir没?哪个好用些?
没用过Selenium,只见过简单的介绍。没有深入了解不太好说。
话说Watir的执行速度真是够慢的……如果往文本框里输入字符的话可以看到一个个字符慢慢冒出来 =_=|||
watir耗资源,不过输入字符应该用 .value = '字符' 不应该用 .set('字符')
8 楼
RednaxelaFX
2009-12-04
yuan 写道
FX比较过Selenium和Watir没?哪个好用些?
没用过Selenium,只见过简单的介绍。没有深入了解不太好说。
话说Watir的执行速度真是够慢的……如果往文本框里输入字符的话可以看到一个个字符慢慢冒出来 =_=|||
7 楼
yuan
2009-12-04
FX比较过Selenium和Watir没?哪个好用些?
6 楼
RednaxelaFX
2009-11-22
Oops,发帖的时候复制粘贴错了一段代码……已修正
@coolspeed 什么部分看不懂?
@coolspeed 什么部分看不懂?
5 楼
coolspeed
2009-11-22
这边玩得好High呀。可惜我看不懂。着急ing
4 楼
RednaxelaFX
2009-11-21
Hooopo 写道
irb
irb +1
3 楼
Hooopo
2009-11-21
irb
2 楼
iaimstar
2009-11-21
实际上 受各种蓝光党影响
。。。
今天上午电脑装了ruby 的186-27 >_<
目前正在研究用什么editor比较适合我这种懒人。。
。。。
今天上午电脑装了ruby 的186-27 >_<
目前正在研究用什么editor比较适合我这种懒人。。
1 楼
iaimstar
2009-11-21
你 你。。。确实魔障了
发表评论
-
字符串的一般封装方式的内存布局 (0): 拿在手上的是什么
2013-11-04 18:22 21496(Disclaimer:未经许可请 ... -
字符串的一般封装方式的内存布局
2013-11-01 12:55 0(Disclaimer:未经许可请 ... -
RubyConf notes
2011-11-08 19:10 0symmetric coroutine Fiber#trans ... -
ShanghaiOnRails第八次线下活动——你不需要知道的Ruby实现
2010-11-12 15:39 2843上个月底正好赶上参加了ShanghaiOnRails 第八次线 ... -
JRuby的运行模式
2010-11-01 11:21 0// 老的JRuby还是用org.jruby.evaluato ... -
你不需要知道的Ruby草稿
2010-10-27 11:25 0一些Ruby实现 Ruby 1.8 树遍历解释 Ruby 1 ... -
Ruby里的fiber/coroutine例子
2010-01-26 18:33 0Ruby 1.9开始支持fiber。与“fiber”一词的一般 ... -
JRuby使用技巧收集
2009-12-28 09:35 0java.lang.Thread.new { puts &qu ... -
特殊类型的eigenclass
2009-12-17 03:39 0Fixnum的实例没有eigenclass true、fals ... -
奇怪的参数
2009-12-08 02:25 0默认参数与闭包的组合 真正的问题不是只是默认参数看起来很诡异, ... -
MacRuby的执行模式
2009-12-07 07:41 0mailing-list macruby-devel http ... -
Rubinius的执行模型
2009-12-05 15:22 0Rubinius wants to help YOU make ... -
Ruby 1.8和1.9中String#hash的实现
2009-11-22 18:23 01.8 string.c int rb_str_hash(s ... -
[标题党] MagLev中GC类的真相……
2009-11-21 14:46 0注意到本文的标题:我是说“GC类”的真相,不是说GC的真相哦~ ... -
小试rubyzip的一个脚本
2009-11-17 20:42 3460呼,今天开始3天都是新人培训,总算可以抽点时间发一帖。 现在在 ... -
爬一下Google和百度看口碑对它们做的SEO效果如何
2009-11-09 00:27 0#!/usr/bin/env ruby require ... -
把Mechanize的html_parser改回到Hpricot
2009-11-08 14:45 3117记得我最初开始用Nokogir ... -
使用新的RubyInstaller
2009-11-07 02:37 0Hpricot的安装需要编译,需要devkit -
Ruby metaprogramming tech notes
2009-09-28 15:39 0class Builder def self.build ... -
在Windows上使用Wilson
2009-09-18 19:47 2994之前被NS老兄激起了兴 ...
相关推荐
expect(@browser.title).to include('Watir - Ruby Gem for Web Automation') end end ``` 这个例子展示了如何使用RSpec编写一个测试用例,其中包含了测试前后的浏览器初始化和关闭,以及预期结果的验证。 ### *...
Watir,是Ruby的一个库,它允许通过编写Ruby代码来控制浏览器,进行端到端的Web应用测试。 ### Ruby安装 首先,我们需要在本地环境中安装Ruby。这可以通过Ruby安装器RVM(Ruby Version Manager)或RBEnv来完成。...
开源自动化测试工具组合:Ruby+Watir安装 安装准备: 1. 工具安装包准备: Ruby官方下载地址:http://www.ruby-lang.org/zh_cn/downloads/,稳定版本Ruby 1.8.6 Watir下载地址:...
安装完成后,你可以通过在Ruby环境中运行以下代码来验证Watir是否已成功安装: ```ruby require 'watir' browser = Watir::Browser.new puts "Watir version: #{Watir::VERSION}" browser.close ``` 如果没有...
watir测试框架介绍 watir测试框架介绍 watir测试框架介绍
1. 官方文档:https://watir.com/documentation/ 2. Ruby教程:https://www.ruby-lang.org/zh_cn/documentation/ 3. Cucumber教程:https://cucumber.io/docs/guides/10-minute-tutorial/ 4. Watir+Cucumber示例项目...
总结来说,Watir自动化测试配合Cucumber的BDD理念,可以创建出高效且易于理解的测试方案。而引入AutoIt,能进一步完善自动化测试的场景,确保对Web应用程序进行全面、深入的测试。这样的组合在IT行业的自动化测试...
### Ruby Watir框架自动化介绍 #### 一、Ruby Watir框架概述 Watir(Web Application Testing In Ruby)是一个用于Web应用程序测试的Ruby库。它提供了一种简单且强大的方法来模拟用户与Web页面的交互行为,进而...
- Watir 官方文档:[watir.com](http://watir.com/) - Google Group 支持论坛:[watir-general](http://groups.google.com/group/watir-general) 通过以上步骤,应该能够成功安装并使用 Watir。如果仍然存在问题...
**标题:“Watir-Bonus-1.5.1.1230.zip”** 这个压缩包包含的是Watir的扩展版本1.5.1的1230次更新,名为“Bonus”。Watir(Web Application Testing in Ruby)是一个开源的Ruby库,用于自动化浏览器操作,它使得...
公司自动化测试培训ppt,使用语言ruby,自动化测试工具watir,讲解基础api和常用框架
### 前端自动化测试与Watir技术详解 #### 一、前端自动化测试概述 前端自动化测试是指在软件开发过程中,使用自动化工具和技术对前端应用进行功能验证的一种方法。随着互联网应用的快速发展,前端自动化测试已成为...