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