RAutomation 在 Watir中的使用

RAutomation的用法

require "rautomation"

# 通过匹配部分标题来获取窗口
window = RAutomation::Window.new(:title => /part of the title/i)
window.exists? # => true
window.title # => "blah blah part Of the title blah"
window.text # => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ultricies..."

# 控件操作
window.text_field(:class => "Edit", :index => 0).set "hello, world!"
button = window.button(:text => "&Save")
button.exists? # => true
button.click

# 获取当前所有窗口的句柄
all_windows = RAutomation::Window.windows
all_windows.each {|window| puts window.hwnd}

# 根据标题匹配所有窗口
window = RAutomation::Window.new(:title => /part of the title/i)
windows = window.windows
puts windows.size # => 2
windows.map {|window| window.title } # => ["part of the title 1", "part of the title 2"]
window.windows(:title => /part of other title/i) # => all windows with matching specified title

# 遍历窗口上所有的button控件
window.buttons.each {|button| puts button.value}
window.buttons(:value => /some value/i).each {|button| puts button.value}

# 使用autoit adapter来定位和操作窗口
# 注意:需要注册AutoitX的DLL
window2 = RAutomation::Window.new(:title => "Other Title", :adapter => :autoit) # use AutoIt adapter
# 使用autoit的原生方法来操作控件
# use adapter's (in this case AutoIt's) internal methods not part of the public API directly
window2.WinClose("[TITLE:Other Title]")

下面我展示一个 RAutomation有用的地方:
需求:
我现在要到达百度页面,把百度图片保存到本地(用处在于:我可以应用脚本,到达百度图片页面,下载1000张1920*1080的图片,为我日常工作所用)
难点:
如果单纯应用Watir来操作,是不怎么方便的,一般是需要调用AutoIt的方法。
如果应用RAutomation来做的话,存在另外一个难点:image.save(path)方法并不能工作
处理方法:
1. Watir脚本:
require 'watir-classic'
require 'rautomation'
$ie = Watir::Browser.start("www.baidu.com")
$ie.goto("http://www.baidu.com")
$ie.image(:src,/baidu/).save("d:\\test3.jpg")

 2. 上述脚本并不能正常工作,需要修改Watir脚本库的Image.rb

 

def fill_save_image_dialog(path)
        command = "require 'rubygems';require 'rautomation';" <<
        		"window=::RAutomation::Window.new(:class => '#32770');" <<
        		"window.text_field(:class => 'Edit', :index => 0).set('#{path.gsub(File::SEPARATOR, File::ALT_SEPARATOR)}');" <<
        		"window.button(:class =>'Button', :index =>0).click"
        IO.popen("ruby -e \"#{command}\"")

	end

 

 

 

image.rb的路径为:lib\ruby\gems\1.9.1\gems\watir-classic-4.0.1\lib\watir-classic\image.rb

 

RAutomation 对中文的支持不是很好

如果你应用的是中文操作系统,你在使用下面的方法的时候

all_windows = RAutomation::Window.windows
all_windows.each {|window| puts window.title}
可能你会打印出来一堆的乱码。
如果你应用 RAutomation::Window.new :title=> window.title
你可能无法定位很多中文弹出窗口。
这时候该怎么办?
HWND永远不会骗你!
你可以通过弹出窗口上能够展示的英文字符,外加窗口的遍历,把你想要应用的窗口hwnd筛选出来,然后对该筛选出来的窗体进行操作。
下面的一个代码片段,就是根据弹出窗体上的文字“Firefox HTML Document”定位,然后在输入口中输入用户想要设置的字符。

#encoding=utf-8
require "rautomation"
require "watir-classic"
RAutomation::Window.windows.each {|window|
        if window.text.include?(text)
            puts window.button(:class =>"Button", :index =>0).exists?
            window.button(:class =>"Button", :index =>0).click
            #return window.hwnd
        end
    }
 
 

 

 
 
 
 


转载于:https://www.cnblogs.com/autotest/p/3599074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值