Watir使用过程中,经常遇到的弹出框分为几类:

1. Alert

2. Confirm

3. Prompt

4. File select

Watir 处理弹出框,有很多方法,可以应用 AutoIt来处理,也可以通过 RAutomation来处理。

AutoIt的局限性是无法跨平台,所以在Watir 4.0中被摒弃了;

RAutomation 对中文的处理存在一定的局限性,对于新手来说,常常无法找到问题的解决方案。

Watir-classic 中的 Watir::Alert 方法,是无法起作用的。

我对AutoIt,Rautomation处理弹出框做了一个总结:

AutoIt处理方法如下:

#encoding:utf-8
require 'watir-classic'
class PopupWindow
def initialize( )
@autoit = WIN32OLE.new('AutoItX3.Control')
end #popup_title: 弹出窗口的标题
def push_alert_button(popup_title)
@autoit.WinWaitActive(popup_title, "")
@autoit.ControlClick(popup_title, "", "Button1")
@autoit.WinWaitClose(popup_title)
end #popup_title: 弹出窗口的标题
#button_name: 弹出窗口上要点击的按钮名称
def push_confirm_button(popup_title, button_name)
@autoit.WinWaitActive(popup_title, "")
@autoit.ControlClick(popup_title, "", "[text:#{button_name}]")
@autoit.WinWaitClose(popup_title)
end #popup_title: 弹出窗口的标题
#input: 在弹出窗口的输入口中要输入的值
#button_name: 弹出窗口上要点击的按钮名称
def push_prompt_button(popup_title, input, button_name)
@autoit.WinWaitActive(popup_title, "")
@autoit.ControlSetText(popup_title, "", "Edit1", input)
@autoit.ControlClick(popup_title, "", "[text:#{button_name}]")
@autoit.WinWaitClose(popup_title)
end #popup_title: 弹出窗口的标题
#file_path: 在弹出窗口的输入口中要输入的文件路径
#button_name: 弹出窗口上要点击的按钮名称
def select_file(popup_title, file_path, button_name="打开(&O)")
@autoit.WinWaitActive(popup_title, "")
@autoit.ControlSetText(popup_title, "", "Edit1", file_path)
@autoit.ControlClick(popup_title, "", "[text:#{button_name}]")
@autoit.WinWaitClose(popup_title)
end
end

如何应用 RAutomation 处理 Watir IE 弹出框?

#encoding=utf-8
require "rautomation"
require "watir-classic"
#text means the English text which only displayed on the Alert popup window
def click_alert(text)
window = RAutomation::Window.new(:text => /#{text}/i)
window.wait_until_present
window.activate
puts window.button(:class =>"Button", :index =>0).exists?
window.button(:class =>"Button", :index =>0).click
end #text means the English text which only displayed on the Confirm popup window
#value means the value you want to input on Prompt window
def set_prompt(text, value)
window = RAutomation::Window.new(:text => /#{text}/i)
window.wait_until_present
window.activate
text_field = window.text_field(:class =>"Edit", :index=>0)
text_field.set(value)
window.button(:class =>"Button", :index =>0).click
end #text means the English text which only displayed on the Confirm popup window
def click_confirm(text)
window = RAutomation::Window.new(:text => /#{text}/i)
window.wait_until_present
window.activate
puts window.button(:class =>"Button", :index =>0).exists?
window.button(:class =>"Button", :index =>0).click
end
#text means the English text which only displayed on the Select file popup window
#path means the file path you want to select
def select_file(text, path)
window = RAutomation::Window.new(:text => /#{text}/i)
window.wait_until_present
window.activate
window.text_field(:class =>"Edit", :index =>0).set(path)
window.button(:class =>"Button", :index =>0).click
end

Watir RAutomation VS AutoIt to deal with popup的更多相关文章

  1. Watir: 应用Watir,调用AutoIT清空IE浏览器的Cookies

    require 'win32ole'ai = WIN32OLE.new("AutoItX3.Control")ai.RunWait("RunDll32.exe InetC ...

  2. watir学习系列--对话框处理(转)

    1.下面是网上编写的类库,保存为libAutoit.rb #LibAutoit主要处理windows弹出的对话框,调用autoit类进行处理 #函数如下: #- ChooseFileDialog函数: ...

  3. 【ruby】ruby基础知识

    Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...

  4. RAutomation 在 Watir中的使用

    RAutomation的用法 require "rautomation" # 通过匹配部分标题来获取窗口 window = RAutomation::Window.new(:tit ...

  5. AutoIT: 开发界面结合GUI automation和Watir Automation

    可以应用AutoIT开发出界面,从而把AutoIT对GUI的自动化测试与Watir对web的自动化测结合在一起.以下代码是我学习GUI界面开发的实例代码.1. 当点击Watir_Test_Button ...

  6. Watir: 右键点击实例(某些如果应用AutoIt来做会更加简单高效)

    require 'watir' module Watir class Element def top_edge assert_exists assert_enabled ole_object.getB ...

  7. Watir: 对浏览器的保存文件操作, 其实应用的是AutoIt脚本

    def save_file(filepath) ai =WIN32OLE.new("AutoItX3.Control") ai.WinWait("FileDownload ...

  8. ruby + watir 自动化上传图片文件解决方案

    watir自动化捕获上传图片元素: require 'watir' include Watir require 'test/unit' class TC_recorded < Test::Uni ...

  9. Watir资源列表【转】

    Watir简介 "Watir" (发音与 water相近) 全写是 "Web Application Testing in Ruby".Watir是一款用Rub ...

随机推荐

  1. hdu1827之强联通

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 爬虫基本操作、requests和BeautifulSoup

    1. 爬虫基本操作 例如舆情系统: 获取汽车之家新闻放到自己数据库里,创建自己的app,发布内容,注明来源,自己创业. URL指定内容获取到 - 发送Http请求:http://www.autohom ...

  3. mysql: Data source rejected establishment of connection, message from server: "Too many connections"

    http://www.oschina.net/question/558677_66703 com.mysql.jdbc.exceptions.MySQLNonTransientConnectionEx ...

  4. Mysql导出大量数据

    outfile 导出文件   select name from t1 into outfile "/tmp/test.txt"   infile 导入文件 导入到表t1中的name ...

  5. git 操作远程仓库地址

    查看所有远程仓库地址: git remote -v 更改远程仓库地址: git remote set-url origin newUrl 查看某一个远程仓库地址: git remote get-url ...

  6. linux下apache https 虚拟主机配置

    如果单纯仅仅想在数据传输时加密传输,那么ssl证书是不须要认证的,可是浏览器打开时会有警告信息.如果我们做的不是一个公众产品那么也还好啦. 例如以下是今天学习时的一个笔记,事实上我用的是真实环境. 环 ...

  7. IDEA搭建Android wear开发环境,Android wear,I&#39;m comming!

    随着google公布了android wear这个东西.然后又有了三星的gear,LG的G watch以及moto 360,苹果由公布了apple watch.未来可能在智能手表行业又有一场战争. 当 ...

  8. D3js-API介绍【英】

    Everything in D3 is scoped under the d3 namespace. D3 uses semantic versioning. You can find the cur ...

  9. Lombok简介

    Lombok简介 和其他语言相比,Java经常因为不必要的冗长被批评.Lombok提供了一系列注解用以在后台生成模板代码,将其从你的类中删除,从而有助于保持你的代码整洁.较少的模板意味着更简洁的代码, ...

  10. sublime 快捷键 汇总--长期

    Ctrl+P 输入当前项目中的文件名,快速搜索文件 Ctrl+G 输入数字跳转到该行代码 Ctrl+R 输入关键字,查找文件中的函数名 Ctrl+: 输入关键字,查找文件中的变量名.属性名等 Ctrl ...