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

上一篇 / 下一篇  2009-12-25 16:37:33

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/">TestSite</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

焦点论坛自动发帖部分举例:

登录帐号和密码:来自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

TAG:

 

评分:0

我来说两句

Open Toolbar