找回密码

火车采集器软件交流官方论坛

搜索
火车采集器V9版免费下载火车浏览器 - 可视采集,万能群发,全自动脚本工具
查看: 15486|回复: 20

用Ruby的Watir实现自动发贴顶贴

 关闭 [复制链接]
发表于 2008-9-20 22:07:46 | 显示全部楼层 |阅读模式
也就是大家平时所说的自动发贴自动顶贴机器人的一种

Watir的基本语法(Watir Syntax)

1.使用Watir工具,需要在脚本中加上
require 'watir'
2.创建一个IE的测试实例
ie = Watir::IE.new
或者在创建的同时直接转到页面
ie = Watir::IE.start("http://mytestsite")
Watir使用start方法同时创建一个浏览器实例并转到一个页面。
3.页面导航
ie.goto("http://mytestsite")
4.操纵Web页面对象
4.1超链接
4.1.1使用Text属性点击超链接
ie.link(:text , "Pickaxe").click
对应的HTML代码为:
<a href="http://pragmaticprogrammer.com/titles/ruby/">Pickaxe</a>
4.1.2使用URL属性点击超链接
ie.link(:url , "http://pragmaticprogrammer.com/titles/ruby/").click
对应的HTML代码为:
<a href="http://pragmaticprogrammer.com/titles/ruby/">Test Site</a>
4.2复选框
4.2.1使用name属性设置复选框
ie.checkbox(:name, "checkme").set
4.2.2使用name属性清除复选框
ie.checkbox(:name, "checkme").clear
4.2.3使用name和value属性设置复选框
ie.checkbox(:name, "checkme", "1").set
4.2.4使用name和value属性清除复选框
ie.checkbox(:name, "checkme", "1").clear
对应的HTML代码为:
<input type = "checkbox" name = "checkme" value = "1">
4.3单选框
4.3.1使用name属性设置单选框
ie.radio(:name, "clickme").set
4.3.2使用name属性清除单选框
ie.radio(:name, "clickme").clear
4.3.3使用name和id属性设置单选框
ie.radio(:name, "clickme", "1").set
4.3.4使用name和id属性清除单选框
ie.radio(:name, "clickme", "1").clear
对应的HTML代码为:
<input type = "radio" name = "clickme" id = "1">
4.4下拉框
4.4.1使用name属性和值来设置下拉框
ie.select_list( :name , "selectme").select("is fun")
4.4.2使用name属性和值来清除下拉框
ie.select_list( :name , "selectme").clearSelection
对应的HTML代码为:
<select name = "selectme" > <option name=1> <option name=2>Web Testing <option name=3>in Ruby <option name=4>is fun </select>
4.5在Web页面中输入数据
4.5.1使用文本输入框的那么属性设置输入内容
ie.text_field(:name, "typeinme").set("Watir World")
4.5.2清空文本输入框
ie.text_field(:name, "typeinme").clear
对应的HTML代码为:
<input type = "text" name = "typeinme" >
4.6从Web页面上提交数据
4.6.1按钮
4.6.1.1通过值或标题属性点击按钮
ie.button(:value, "Click Me").click
4.6.1.2通过name属性点击按钮
ie.button(:name, "clickme").click
对应的HTML代码为:
<input type = "button" name = "clickme" value = "Click Me">

4.6.2表单
4.6.2.1表单中的按钮
使用value或标题属性
ie.button(:value, "Submit").click
对应的HTML代码为:
<form action = "submit" name = "submitform" method="post"><input type = "submit" value = "Submit"></input></form>
4.6.2.2表单中的图片按钮
使用那么属性
ie.button(:name, "doit").click
对应的HTML代码为:
<form action = "submit" name = "doitform" method="post"><input type="image" src = "images/doit.gif" name = "doit"></form>
4.6.2.3没有按钮的表单
Watir can submit a form by identifying it by its name, action and method attributes.
可以通过name、action以及method属性来提交表单
ie.form(:name, "loginform").submit
ie.form(:action, "login").submit
对应的HTML代码为:
<form action = "login" name = "loginform" method="get"><input name="username" type="text"></input></form>
4.6.3框架
ie.show_frames可以打印出当前页面框架的数量和名称
Watir允许通过名称属性来访问框架,如ie.frame("menu")
如果要访问menu框架中的一个超链接,可以ie.frame("menu").link(:text, "Click Menu Item").click
4.6.4嵌套框架
ie.frame("frame1").frame(:name, "nested_frame")
4.6.5新窗口
一些Web应用会弹出新窗口或打开一个新窗口,可以使用attach方法来访问并控制新窗口。通过标示新窗口的URL或者title来访问。
ie2 = Watir::IE.attach(:url, 'http://mytestsite')
ie3 = Watir::IE.attach(:title, 'Test New Window')
也可以使用正则表达式
ie4 = Watir::IE.attach(:title, /Test New/)
注意:不要把新窗口分配到你的ie变量,最好给新窗口一个不同的名字
5.验证结果
比较好的方法是在测试案例中假如验证点
5.1对象存在
使用Watir方法contains_text
ie.contains_text("Reached test verification point.")
if ie.contains_text("Reached test verification point.")
  puts: "Test passed. Page contains the text: Reached test verification point."
else
  puts: "Test failed! Page didn't contain text: Reached test verification point."
end
5.2使用test::unit Assertions
5.2.1需要test::unit
require 'test/unit'
5.2.2创建测试实例
class TC_myTest < Test::Unit::TestCase
  ...fill in Test Case methods here...
end
5.2.3创建测试用例方法
在测试类中,需要声明象下面的方法:
def test_myTestCase
  fill in method body with Watir code and assertion here
end
方法必须以test开头,ruby会随机运行测试案例,如果需要顺序执行,需要在test后加上字母或数字来强迫它顺序执行,比如“test_a_mytest”
定义测试方法的类:
class TC_myTest < Test::Unit::TestCase
  def test_ myTestCase
    Watir code and assertion here...
  end
  def test_anotherTestCase
    Watir code and assertion here...
  end
  def test_aTestCase
    Watir code and assertion here...
  end
end
5.2.4使用Assertions
Watir通过在一个asert覆写Watir方法支持assertions。
assert(ie.contains_text("Reached test verification point.")
5.2.5Setup and Teardown
def setup
  fill in code that will run before every test case here...
end
def teardown
  fill in code that will run after every test case here...
end
6.Tips and Tricks
Running Tests With the Browser Not Visible
Run the tests with a "-b" option if you don't want the browser to be visible. ex. myTest.rb -b

[ 本帖最后由 沦陷今生 于 2008-9-20 22:40 编辑 ]
 楼主| 发表于 2008-9-20 22:26:51 | 显示全部楼层
焦点论坛自动发帖部分举例:
登录帐号和密码:来自user.txt
登录的论坛地址:来自论坛地址.txt


  1. require 'watir'
  2. include Watir

  3. def login(ie, users)
  4. if ie.text.include? '密码'
  5. ie.text_field(:name, 'email').set(users[rand(users.size)][:user])
  6. ie.text_field(:name, 'password').set(users[rand(users.size)][:password])
  7. ie.button(:value, '登 录').click
  8. end
  9. end

  10. def logout(ie)
  11. if ie.text.include? '退出登录'
  12. ie.link(:text, '退出登录').click
  13. end
  14. end

  15. #初始化需要发布到的论坛地址
  16. urls = []
  17. File.open('论坛地址.txt') do |f|
  18. f.each_line {|line| urls << line}
  19. end


  20. #初始化登录文件
  21. users = []
  22. File.open('user.txt') do |f|
  23. f.each_line do |line|
  24. words = line.split(' ')
  25. users << {:user => words[0], :password => words[1]}
  26. end
  27. end
复制代码

[ 本帖最后由 沦陷今生 于 2008-9-21 00:56 编辑 ]
发表于 2008-9-20 23:41:27 | 显示全部楼层
好大的沙发 精华贴 置顶
 楼主| 发表于 2008-9-21 09:15:47 | 显示全部楼层
发表于 2008-9-21 09:29:24 | 显示全部楼层
打酱油路过此地,
发表于 2008-9-21 09:55:05 | 显示全部楼层
楼主不要忘记免费升级一次的承诺。
 楼主| 发表于 2008-9-21 10:08:59 | 显示全部楼层
出现什么具体问题给我留言就成,提供一次免费升级服务,多留言几个,有时候消息比较多就忘记了,下周时间比较空闲
发表于 2008-9-21 10:15:21 | 显示全部楼层
发表于 2008-9-21 16:32:53 | 显示全部楼层
这个跟火车头有什么关系?怎么用?
发表于 2008-9-26 06:05:34 | 显示全部楼层
说实话,当成天书在看了。
您需要登录后才可以回帖 登录 | 加入会员

本版积分规则

温馨提示:建议您联系官方定制服务,通过官方支付方式完成支付。您与其他非官方账号发生的交易,我方概不承担责任。网络有风险,交易需谨慎

QQ| 手机版|Archiver| 火车采集器官方站

Copyright © 2001-2013 Comsenz Inc.  Template by Comeings! All Rights Reserved.

Powered by Discuz! X3.4( 皖ICP备06000549 )

快速回复 返回顶部 返回列表