最近开始玩一个叫OGame的web游戏,才玩了几天就发现基建太费时间了,才升到lev12就要等将近2个小时。浏览了游戏的FAQ和讨论区,发现游戏没有提供公开的接口,于是便想写个程序来简化操作,初步考虑了一下,有2个方案可以选择:

A 模拟ie来帮我做一些简单的动作
B 利用ie的接口,在特定情况下直接触发ie的事件

需求整理如下: 

NO 描述 优先度
1

定期刷新页面获取最新的信息

2 根据取得的信息判断,一旦资源满足条件,就开始按照指定策略升级建筑。
3 最新信息mail通知
4 战斗信息自动log,星图信息自动log

准备用ruby来开发,先考察A方案的可行性,使用socket模拟ie访问网站,要解析http的包,工作量比较大。
B方案暂时还没有头绪,ruby下访问com接口只记得有个excel的例子。

于是google了一下,发现Watir(web application testing in ruby)这个东东,是用来自动测试web 应用的一个开源框架。
原理是利用ie的COM接口来操纵ie的一些行为。正好是俺需要的东东。

开始动手了,先翻出一个Watir的sample,是访问google的,很简单,一下就运行通过了。

 1web application testing in ruby (1)require 'watir' 
 2web application testing in ruby (1)# the watir controller 
 3web application testing in ruby (1)# open the IE browser 
 4web application testing in ruby (1)ie = Watir::IE.new 
 5web application testing in ruby (1)# Step 1: go to the test site: http://www.google.com 
 6web application testing in ruby (1)ie.goto("http://www.google.com"
 7web application testing in ruby (1)# Step 2: enter 'pickaxe' in the search text field 
 8web application testing in ruby (1)ie.text_field(:name, "q").set("pickaxe"# q is the name of the search field 
 9web application testing in ruby (1)# Step 3: click the 'Google Search' button 
10web application testing in ruby (1)ie.button(:name, "btnG").click # "btnG" is the name of the Search button 
11web application testing in ruby (1)# Actual Result: Check that the 'Programming Ruby' link appears on the results page 
12web application testing in ruby (1)if ie.contains_text("Programming Ruby"
13web application testing in ruby (1)  puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results." 
14web application testing in ruby (1)else 
15web application testing in ruby (1)  puts "Test Failed! Could not find: 'Programming Ruby'" 
16web application testing in ruby (1)end 
17web application testing in ruby (1)# End of test: Google search

Eclipse的Console里面显示:

web application testing in ruby (1)Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results.
web application testing in ruby (1)

好,开头很顺利,下面进一步了解一下watir怎么用。


相关文章: