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
}
RAutomation 在 Watir中的使用的更多相关文章
- Watir RAutomation VS AutoIt to deal with popup
Watir使用过程中,经常遇到的弹出框分为几类: 1. Alert 2. Confirm 3. Prompt 4. File select Watir 处理弹出框,有很多方法,可以应用 AutoIt来 ...
- watir学习系列--对话框处理(转)
1.下面是网上编写的类库,保存为libAutoit.rb #LibAutoit主要处理windows弹出的对话框,调用autoit类进行处理 #函数如下: #- ChooseFileDialog函数: ...
- Python开源框架
info:更多Django信息url:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC) ...
- 基于ruby的watir自动化测试 笔记二
基于ruby的watir自动化测试 笔记一的补充版,新增加了些特殊的控件捕获方法.还在更新中.... attribute_value 获取当前控件的属性 Value = ie.link(:id=> ...
- 基于ruby的watir自动化测试 笔记一
基于Ruby的watir-webdriver自动化测试方案与实施(五) 基于Ruby的watir-webdriver自动化测试方案与实施(四) 基于Ruby的watir-webdriver自动 ...
- watir学习系列--Watir API介绍
文本框: <INPUT id="email" name="_fmu.u._0.e" value="" /> ...
- Watir资源列表【转】
Watir简介 "Watir" (发音与 water相近) 全写是 "Web Application Testing in Ruby".Watir是一款用Rub ...
- Windows环境搭建Web自动化测试框架Watir
Windows环境搭建Web自动化测试框架Watir 一.前言 Web自动化测试一直是一个比较迫切的问题,对于现在web开发的敏捷开发,却没有相对应的敏捷测试,故开此主题,一边研究,一边将We ...
- selenium1,selenium2,watir的比较
接触web方面的自动化测试,会接触几个常用的工具,selenium1,selenium2,watir 有的时候总是混淆,那么他们的优缺点啥的呢,在让你给项目选自动化框架,会选择哪个??? 1,语言的支 ...
随机推荐
- 写一个dup2功能同样的函数,不能调用 fcntl 函数,而且要有出错处理
实现的时候用到系统原来的dup函数 // mydup2.c // 2015/08/17 Lucifer Zhang version1.0 // write my own dup2 function / ...
- sh_Spring整合Hibernate
分别介绍了Sping和Hibernate,以下是将它们整合到一块去了. 一.Hibernate内容 1.创建PO类. package cn.tgb.domain; //User实体 public ...
- Python学习笔记8:标准库之正則表達式
Python拥有强大的标准库.从如今起,開始学习标准库中提供的一些经常使用功能. 首先看正則表達式(regular expression),它的主要功能是从字符串(string)中通过特定的模式(pa ...
- mysql select last_insert_id()函数返回的值
mysql)); 创建表j 插入数据 mysql> insert into j(name) values('wanggiqpg'); Query OK, row affected (0.00 s ...
- java数据库连接池简单实现
package cn.lmj.utils; import java.io.PrintWriter; import java.lang.reflect.InvocationHandler; import ...
- 每天进步一点点——Linux中的线程局部存储(二)
转载请说明出处:http://blog.csdn.net/cywosp/article/details/26876231 在Linux中另一种更为高效的线程局部存储方法,就是使用keyword ...
- shell(3):文本处理、基本语法和脚本编写
一.awk.变量.运算符.if多分支 awk:shell编辑器的一种文本处理工具/命令,同grep.sed一样均可解释正则.具体运用下面awk文本处理有详细说明. 变量:分为系统变量和临时变量.变量一 ...
- Web开发者用什么编辑器?
写在前面的话:从事web前端开发也有一段时间了,今天主要想分享的是文字(代码)编辑器.对于编辑器每个人都有自己的偏爱,也分不同语言的编码者,这里我就拿我接触过的来说说吧! Web开发者用什么编辑器? ...
- PCB常用单位转换 mil 英尺
PCB常用单位转换 mil 英尺 相关常用单位 1mil = 0.0254mm 100mil = 2.54mm 1英寸 = 1000mil = 2.54cm 1英尺 = 12英寸 ...
- C++卷积神经网络实例:tiny_cnn代码具体解释(7)——fully_connected_layer层结构类分析
之前的博文中已经将卷积层.下採样层进行了分析.在这篇博文中我们对最后一个顶层层结构fully_connected_layer类(全连接层)进行分析: 一.卷积神经网路中的全连接层 在卷积神经网络中全连 ...