日历
| |||||||||
| 日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
| 1 | 2 | 3 | 4 | 5 | 6 | ||||
| 7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
| 14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
| 21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
| 28 | 29 | 30 | 31 | ||||||
搜索标题
最新来客
最新留言
统计信息
- 访问量: 596
- 日志数: 9
- 文件数: 1
- 书签数: 2
- 建立时间: 2008-06-07
- 更新时间: 2008-07-10
我的最新日志
-
一段可以运行的right-click
2008-7-10
找到一段有用的right-click.
require 'watir'
module Watir
class Element
def top_edge
assert_exists
assert_enabled
ole_object.getBoundingClientRect.top.to_i
enddef top_edge_absolute
top_edge + container.document.parentWindow.screenTop.to_i
enddef left_edge
assert_exists
assert_enabled
ole_object.getBoundingClientRect.left.to_i
enddef left_edge_absolute
left_edge + container.document.parentWindow.screenLeft.to_i
enddef right_click
x = left_edge_absolute
y = top_edge_absolute
#puts "x: #{x}, y: #{y}"
WindowsInput.move_mouse(x, y)
WindowsInput.right_click
end
end
endmodule WindowsInput
# Windows API functions
SetCursorPos = API.new('SetCursorPos', 'II', 'I', 'user32')
SendInput = API.new('SendInput', 'IPI', 'I', 'user32')
# Windows API constants
INPUT_MOUSE = 0
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010module_function
def send_input(inputs)
n = inputs.size
ptr = inputs.collect {|i| i.to_s}.join # flatten arrays into single string
SendInput.call(n, ptr, inputs[0].size)
enddef create_mouse_input(mouse_flag)
mi = Array.new(7, 0)
mi[0] = INPUT_MOUSE
mi[4] = mouse_flag
mi.pack('LLLLLLL') # Pack array into a binary sequence usable to SendInput
enddef move_mouse(x, y)
SetCursorPos.call(x, y)
enddef right_click
rightdown = create_mouse_input(MOUSEEVENTF_RIGHTDOWN)
rightup = create_mouse_input(MOUSEEVENTF_RIGHTUP)
send_input( [rightdown, rightup] )
end
end# Open google index page, and send a right click to the logo image
ie = Watir::IE.new
ie.goto('www.google.com')
image = ie.image(:index, 1)
image.right_click
# Then, bring up the properties menu (works with IE6, at least)
ie.send_keys("{UP}{UP}{ENTER}") -
解决AutoItX3不能用的方法
2008-7-02
regsvr32 "C:\ruby\lib\ruby\site_ruby\1.8\watir\AutoItX3.dll" -
win32ole接口调用word
2008-7-01
刚开始是用File类来操作的,后来改的word输出。给出的这个脚本我用了Ruby来操作WIN32OLE来生成word文件。require 'win32ole'var_a=Array['A1','A2','A3']var_b=Array['B1','B2','B3']var_c=Array['C1','C2','C3']var_d=Array['D1','D2','D3']var_e=Array['E1','E2','E3']var_f=Array['F1','F2','F3']var_total=Array.newfor a in (0..2)for b in (0..2)for c in (0..2)for d in (0..2)for e in (0..2)for f in (0..2)var_total.push(var_a[a]+' | '+var_b+' | '+var_c[c]+' | '+var_d[d]+' | '+var_e[e]+' | '+var_f[f])endendendendendendword = WIN32OLE.new('word.application')word.visible=trueword.Documents.Add()for i in(0..728)word.Selection.Font.Size=12word.Selection.Font.ColorIndex = 2word.Selection.TypeText(var_total[i]+"\n")endword.DefaultSaveFormatword.Documents.close()当然上面代码需要重新优化。。。word没有实现自动保存。。。以后再说。再次说明了Ruby是我们的好助手! -
也许可以支持键盘
2008-7-01
也许可以支持键盘,还没有试过。
步骤如下:
1.打开watir.rb
2.在class TextField中加入一个新的method:
def characters_in(value)
index = 0
while index < value.length
len = value[index] > 128 ? 2 : 1
yield value[index, len]
index += len
end
end
3.更改class TextField的doKeyPress( value )方法部分代码,将下面代码
-------------------------------------------
for i in 0 .. value.length-1
sleep @container.typingspeed
c = value[i,1]
@container.log " adding c.chr " + c
@o.value = @o.value.to_s + c
@o.fireEvent("onKeyDown")
@o.fireEvent("onKeyPress")
@o.fireEvent("onKeyUp")
end
替换为如下代码
characters_in(value) {|c|
sleep @container.typingspeed
@o.value = @o.value.to_s + c
@o.fireEvent("onKeyDown")
@o.fireEvent("onKeyPress")
@o.fireEvent("onKeyUp")
} -
运行Ruby时不显示browser方法
2008-7-01
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 -
watir中对table的操作
2008-6-23
在Watir中
对于实例化的$ie对象,我们可以通过以下的两种方式来访问他的Table元素:
t = $ie.table(:id,"data")
t = Table.new($ie,:id,"data")
同样,对于tr和td来说,分别也有以上两种方法:
tr = $ie.row(:id,"title")
tr = TableRow.new($ie,:id,"title")
td = $ie.cell(:id,"name")
td = TableCell.new($ie,:id,"name")
Watir中Table,TableBody,TableRow,TableCell这几个类,都提供了一个索引方法"[](index)"来定位其下一层的子元素对象,该方法为实例方法,"index"为传入的参数,索引值从1开始,而非从0开始。
用法如下:
以table的第一行,第一个元素为例:
tr1 = t.[](1)
td1 = tr1.[](1)
也可以连续访问:td1 = t.[](1).[](1)
如果td中还有其他元素,可以通过td的实例方法直接访问,以checkbox为例:
cb = td1.checkbox(:id,'navigate_id').click
对于以上所提到的对象,都是从Element继承而来,所以click,enabled?,exists?,fireEvent,flash,focus等方法都直接可以使用。
如果你的td元素定位准确了,且鼠标响应事件没有错误的话,那么应该能看到点击后的效果。
建议你先用flash方法定位一下要操作的元素。
建议多查一下Watir的API Reference http://wtr.rubyforge.org/rdoc/我写的代码如下:
def logOut()
t=@ie.table(:id,"CoolMenu2menutable")
@td_logout=t.[](1).[](16)
end先找到Table,再索引TR,再索引到TD
===================================================
t1 = t.[](1).[](1).to_s
puts t1 # print the cell of 1 row and 1 col
-
watir教程实例
2008-6-19
watir教程实例开发测试案例(Developing Test Cases)
1.打开编辑器
2.以.rb为你的文件扩展名
3.在测试文件的第一句写上“require 'watir'”,确保可以访问Watir工具。
4.打开浏览器并转到要测试的应用
5.与之交互并设计你的testcase
6.在测试脚本中使用Watir方法
7.验证结果
与网页交互(Interacting With a Web Page)
当使用Watir开发测试脚本的时候,通过给网页上的对象发送消息来与之交互。
ie.text_field(:name , "q").set("bluescorpio")
ie.button(:value , "Click Me").click
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 -
如何安装Ruby+Watir测试环境
2008-6-18
终于可以把环境搭起来了。。。现在总结一下。
1. 先到Ruby官方网站下Ruby安装程序。
2. Start->ruby->RubyGems Package Manager
3. 更新ruby到最新版本。打命令:gem update --system
4。安装watir. 命令:gem install watir
-
现在最想做的事
2008-6-07
刚从三亚回来,现在最想的就是快点快点把R&W变成自已的.


