starky(爱在西元前) 2007 Blog  
去生活就像这个世界便是天堂^_^

       最近开始玩一个叫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的,很简单,一下就运行通过了。

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

Eclipse的Console里面显示:

Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results.

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


posted on 2007-02-20 16:09  爱在西元前  阅读(586)  评论(6编辑  收藏  举报