博客主机
A-A+

ruby watir 整理

2009年12月28日 柴房 暂无评论

异常对象识别(watir应用解决方案)

watir基于处理,继承于web document的对象提供较好的支持方法,但相对于处理windows对象较弱. 就目前从各网站收集的对象来看, 基本上,对于在ie实例对象打开的web标准对象
能够成功识别与操作,其它 的脱离当前ie窗口,打开的所有弹出框都不能正常识别。其中,弹出框对象占有异常对象识别中很大一部分。
目前碰到的弹出框, 我把他们分为三大类型:
1, alerts 警告作用,如:sorry, 当前用户没有权限操作
2, confirm 需要操作按钮, 如:你确认要删除当前记录?
3, prompt+ select +confirm 需要用户输入,操作查询或点击, 最后确认, 如:download/upload (浏览+选择文件)

下面给出,上面几种弹出框watir实现识别与操作的方法,
5.1. 弹出框基于autoit + 线程实现方式

此种方法,对于第一、二种弹出框操作较有效,
因点击某个link/button等对象,而弹出的窗口(大部分弹出框可应用此种方式来实现。)

1, 定义方法

def check_for_popups
autoit = win32ole.new('autoitx3.control')
#

# do forever - assumes popups could occur anywhere/anytime in your application.
loop do
# look for window with given title. give up after 1 second.

ret = autoit.winwait('消息 -- 网页对话框', '', 1)
#ret = winactivate("microsoft internet explorer", "")
#autoit.controlclick("microsoft internet explorer", "", "[class:button; instance:1]", 2)

puts(ret)
#
# if window found, send appropriate keystroke (e.g. {enter}, {y}, {n}).

if (ret==1) then autoit.send("{enter}") end

#
# take a rest to avoid chewing up cycles and give another thread a go.
# then resume the loop.
sleep(3)
end
end

2, 程序体代码

ie.button(:name, "signin").click_no_wait

sleep(20)

$popup = thread.new { check_for_popups } # start popup handler

at_exit { thread.kill($popup) }

5.2. call 另一ruby 文件

对于第二种弹出框,像安全警告点击, 并不通过操作与点击任何对象, 即呈现弹出窗口。
我尝试用3.1方法来实现,不成功。用下面方法替代:

1, 在watir/windowhelper.rb文件中增加方法

def push_security_alert_yes_cn
@autoit.winwait "安全警报", ""
@autoit.send "{tab}"
@autoit.send "{tab}"
@autoit.send "{space}"
end

2, 定义另一调用文件 tmp.rb
require 'watir/windowhelper'
helper = windowhelper.new
helper.push_security_alert_cn_yes

3, 在打开安全url之前,启动调用ruby文件

require 'rubygems'
require 'watir' # the watir controller
require 'win32ole'
require 'watir/windowhelper'

thread.new{system('ruby c:\tmp.rb')} #你定义tmp文件存放路径
ie = watir::ie.new
ie.goto("http://www.alipay.com.cn/ ")
5.3. 修改框架底层

此种方法,针对弹出框3.

举例一, 上传下载文件
1, 修改底层代码input_elements.rb文件,
类 filefield中, 方法set.
为了支持中文,有一句替换修改为:

system("rubyw -e \"require 'win32ole'; @autoit=win32ole.new('autoitx3.control');waitresult=@autoit.winwait ' 选择文件', '', 15; sleep 1; if waitresult == 1\" -e \"@autoit.controlsettext '选择文件', '', 'edit1', '#{setpath}'; @autoit.controlsend '选择文件', '', 'button2', '{enter}';\" -e \"end\"")

2, 定义方法
def save_file(filepath)
ai = win32ole.new("autoitx3.control")
ai.winwait("文件下载", "", 5)
ai.controlfocus("文件下载", "", "保存(&s)")
sleep 1
ai.controlclick("文件下载", "", "保存(&s)", "left")
ai.winwait("另存为", "", 5)
sleep 1
ai.controlsend("另存为", "", "edit1",filepath)
ai.controlclick("另存为", "", "保存(&s)", "left")
ai.winwait("下载完毕", "", 5)
ai.controlclick("下载完毕", "", "关闭")
end

3, 程序体代码:
ie.file_field(:name, "xlsfile").set("c:\\samenameobject.html")
#上传你所指定的文件


ie.span(:text, "导出excel表").click_no_wait
save_file("c:\\abc.xls")
#下载到你所指定的路径

针对弹出框3, 需要用户介入查询并操作:
举例二, 中供crm中类目与客户选择的弹出框,与第一种实现方式不同在于,
用到autoit中winactivate与controlclick方法,

require 'rubygems'
require 'watir' # the watir controller
require 'watir/windowhelper'

require 'win32ole'

def check_for_popups

autoit = win32ole.new('autoitx3.control')

loop do
ret = autoit.winwait("选择 -- 网页对话框", "", 1)
puts ret
if (ret==1) then
autoit.winactivate("[class:internet explorer_tridentdlgframe]", "")
autoit.send("{tab}")
autoit.send("{tab}")

#autoit.send("湖州")
#autoit.send("{asc 2709}")
#autoit.sendunicode("a")
#autoit.clipput ("杭")
#autoit.clipget
#autoit.tooltip "this is a tooltip杭州", 0, 0
#autoit.sleep 2000

autoit.send("{tab}")
autoit.send("{tab}")
autoit.send("{enter}")

autoit.winactivate("[class:internet explorer_tridentdlgframe]", "")
autoit.controlclick("选择 -- 网页对话框", "", "[class:internet explorer_tridentlstbox; instance:2]", 2)

autoit.send("{tab}")
autoit.send("{enter}")

autoit.send("{tab}")
autoit.send("{tab}")
autoit.send("{tab}")
autoit.send("{tab}")
autoit.send("{enter}")
end
sleep(3)
end

end

ie=watir::ie.new
ie.goto("http://10.2.6.4:5100/bin/member/signin ")
#ie.goto("http://10.2.6.4:5100/ ")

#ie.element_by_xpath("//span[contains(text(), 'main road nijmegen')]").click

ie.text_field(:name, "id").set("mhl1805")
ie.text_field(:name, "password").set("zxcvbnm")

ie.button(:name, "signin").click

ie.frame(:name, "left").link(:url, "http://10.2.6.4:5100/v3/help_cust.htm").click

ie.frame(:name, "left").link(:text, "新签中供客户").click

# start popup handler

ie.frame(:name, "right").frame(:name, "rtop").button(:value, "选择客户").click_no_wait
sleep(20)

$popup = thread.new { check_for_popups }

at_exit { thread.kill($popup) }

针对第三种popup window, 如果需要与用户交互,且需要输入中文时,若用autoit sendkey对待中文支持(但官方文档说支持中文输入, 网上有不少人和我遇到同样问题),尚没有找到有效方案,有待进一步跟进。

除上述弹出框需要特殊处理外,watir中也有一些扩展tag,或第三方控件需要特殊处理的,
像:
5.4. web html编辑器
中文站的html编辑器提供: 操作id或name操作起来较方便直接

require 'rubygems'
require 'watir' # the watir controller
require 'watir/windowhelper'
require 'win32ole'
ie=watir::ie.new

ie=watir::ie.attach(:title, /阿里助手/)

ie.text_field(:name, "_fmo.a._0.s").set("mhl1805")
ie.document.getelementbyid("_editor").contentwindow.focus()
ie.send_keys("abcsadfasfd")

但也碰到有些web页面,不提供任何id,只能用autoit方法来send tab实现

require 'rubygems'
require 'watir' # the watir controller
require 'watir/windowhelper'
require 'win32ole'

ie=watir::ie.new

#ie=watir::ie.attach(:title, /alibaba/)

ie.goto('http://us.my.alibaba.com/product/buyoffer/post_buying_lead.htm')

ie.text_field(:name, "_fmp.bu._0.su").set("mhl1805")
ie.text_field(:name, "_fmp.bu._0.su").set("中国人")

ie.checkbox(:id, "detaildesc").set
ie.checkbox(:id, "detaildesc").focus()
ie.send_keys("\t"*9)
ie.send_keys('hello worldabcdef')

5.5. 对象识别其它常用tag内置方法引用

如:ie.div , ie.span, ie.cell, ie.table方法,可以实现点击操作,取值操作等.
另外提供qtp 类似描述性编程,同类型对象统计:

ie.buttons.each { |b| puts b.to_s }
puts ie.checkboxes.length
puts ie.links.length
puts ie.buttons.length等等

对于常用的innertext属性, 在watir中已经封装到方法,可以直接引用。如:
ruby在对象识别底层,封装了innertext
实现,调用方法text即可:
如:puts ie.div(:id, "intelligentword").link(:id, index.to_s).text

最后:返回文本与源代码,用下面方法:
puts ie.text()
puts ie.html()

5.6. 识别对象正则表达式支持

当然,ruby提供强大的正则表达式支持,如:属性标识正则
ie=watir::ie.attach(:title, /alibaba/)

ruby的正则表达式以"//"作为基本框架,表达式内容位于"/"之间。表达式返回一个regexp的对象。
表达式的一般规则:
/a/匹配字符a。
/\?/匹配特殊字符?。特殊字符包括^, $, ? , ., /, \, [, ], {, }, (, ), +, *.
.匹配任意字符,例如/a./匹配ab和ac。
/[ab]c/匹配ac和bc,[]之间代表范围。例如:/[a-z]/ , /[a-za-z0-9]/。
/[^a-za-z0-9]/匹配不在该范围内的字符串。
/[\d]/代表任意数字,/[\w]/代表任意字母,数字或者_,/[\s]/代表空白字符,包括空格,tab和换行。
/[\d]/,/[\w]/,/[\s]/均为上述的否定情况。

关于正则其它用法,请参照《watir技术集锦》
5.7. 最后攻略

总之,对于对象识别与操作,要借助于上述文档中的, 灵活运用autoit, xpath与异常对象操作方法。对于watir不支持的windows控件,想到第一个方法,就是采用第三方autoit技术,来模拟键盘或鼠标操作。
对于操作web对象,watir不支持特殊tag,除了扩展其底层代码外,只能深深研究一下xpath了.
最后,再搞不定,就只能到watir group里咨询高人了,呵呵。

尚没有碰到其它更好方法。。。

一些错误

如果报错 autoitx3, it helps to make automation happen

那就在windows 下运行

regsvr32 "c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.2\lib\watir\autoitx3.dll"

unknown ole server: `autoitx3.control' (wi2oleruntimeerror)

that might mean you have not resisted the autoitx3.dll in your computer,
in current watir version, autoitx3.dll has been included in the watir folder,
so run the command in "run":

regsvr32 "c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.2\lib\watir\autoitx3.dll"
then you will not get the errors :)

learn autoitx3, please go to [url]http://www.autoitscript.com/autoit3/index.shtml[/url]
i just want to use it to handle the popup windows when writing watir scripts

标签:

给我留言

Copyright © 小小的数据技术梦想 保留所有权利.   Theme  Ality 浙ICP备12043346号-1

用户登录

分享到: