watir学习系列--对话框处理(转)
1.下面是网上编写的类库,保存为libAutoit.rb
#LibAutoit主要处理windows弹出的对话框,调用autoit类进行处理 #函数如下: #- ChooseFileDialog函数:处理选择文件对话框窗口 #- clearSecurityAlert函数:处理安全警告对话框 #- ControlSetText函数:给对话框中的某个控件设置值 #- ControlClick函数:向指定控件发送鼠标点击命令 #- ControlGetText函数:获取指定控件值 #- ControlGetHandle函数:获取指定控件值的句柄 #- ControlFocus函数:设置输入焦点到指定窗口的某个控件上 #- DealDownloadDialog函数:处理文件下载对话框 #- DealPathDialog函数:设置下载文件路径及操作下载,如果文件已存在则覆盖处理 #- DealAlterDialog函数:处理Alter对话框 #- DealConfirmDialog函数:处理Confirm对话框 #- DealPromptDialog函数:处理Prompt对话框 #- DealSecurity函数:点击页面链接,使弹出安全警告对话框 #- GetDialogTitle函数:根据类型获取弹出的窗口标题 #- SendKey函数:模拟键盘输入字符 #- WinExists函数:判断窗口是否存在 module LibAutoit class AutoItApi def initialize require 'win32ole' require 'watir/windowhelper' WindowHelper.check_autoit_installed @autoit = WIN32OLE.new("AutoItX3.Control") @DealDownloadDialogTimeOut = 40 @DealPathDialogTimeOut = 40 @ChooseFileDialogTimeOut = 40 @DealAlterDialogTimeOut = 40 @DealConfirmDialogTimeOut = 40 @DealPromptDialogTimeOut = 40 @DealSecurityTimeOut = 40 @clearSecurityAlertTimeOut = 40 end #def initialize end #功能说明:根据类型获取弹出的窗口标题,因为IE各版本弹出的对话框标题有差异,需要进行特殊处理 # #参数说明: #type:窗口类型,具体值如下: #- type=1:选择文件窗口标题 #- type=2:Alter窗口标题 #- type=3:Prompt窗口标题 #- type=4:安全警告窗口标题 #- type=5:文件下载窗口标题 #- type=6:文件另存为窗口标题 # #调用示例: GetDialogTitle(2) # #返回值说明: #- 成功:返回获取的标题 #- 失败:返回false def GetDialogTitle(type = 2) #$logger.log("调用函数:LibAutoit.rb文件中的GetDialogTitle(#{type})" ) case type when 1 #选择文件窗口标题 dialog_title = ['选择文件','Choose file'] when 2 #Alter窗口标题 dialog_title = ['Microsoft Internet Explorer','Windows Internet Explorer'] when 3 #Prompt窗口标题 dialog_title = ['Explorer 用户提示','Explorer User Prompt'] when 4 #安全警告窗口标题 dialog_title = ['安全警告','Security Alert'] when 5 #文件下载窗口标题 dialog_title = ['文件下载 - 安全警告','文件下载','File Download'] when 6 #文件另存为窗口标题 dialog_title = ['另存为'] end dialog_title.each do |title| if (WinExists(title,'') == 1) $logger.log("获取的窗口:#{title}",'N') $logger.log("调用函数结束:LibAutoit.rb文件中的GetDialogTitle(#{type})" ,'N') return title end end return false #$logger.log("调用函数结束:LibAutoit.rb文件中的GetDialogTitle(#{type})" ) end #功能说明:处理文件下载对话框,调用DealPathDialog函数设置路径及下载文件 # #参数说明: #- file_path:文件下载后存放的路径,格式如:c:\\test #- file_name:文件名,如:test.txt #- timeout:对话框处理的超时时间,默认为20秒 # #调用示例: DealDownloadDialog(“c:\\test\test.txt”,15) # #返回值说明: #- 成功:返回ture #- 失败:返回false def DealDownloadDialog(file_path,file_name,timeout = @DealDownloadDialogTimeOut ) $logger.log("调用函数:LibAutoit.rb文件中的DealDownloadDialog(#{file_path},#{file_name},#{timeout})",'N' ) $logger.log('----------deal with download File dialog begin----------') start_time = Time.now.to_i while 1 if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 win_title = GetDialogTitle(5) #获取窗口标题 if (win_title) @autoit.WinActivate(win_title,'') sleep(1) ControlClick(win_title,'','Button2') DealPathDialog(file_path,file_name) $logger.log('----------deal with download File dialog end----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealDownloadDialog(),返回结果:true",'N' ) return true end else $logger.log("Deal Download File Dialog Fail!") $logger.log('----------deal with download File dialog end----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealDownloadDialog(),返回结果:false",'N' ) return false end end end #功能说明:设置下载文件路径及操作下载,如果文件已存在则覆盖处理 # #参数说明: #- file_path:文件下载后存放的路径,格式如:c:\\test #- file_name:文件名,如:test.txt #- timeout:对话框处理的超时时间,默认为20秒 # #调用示例: #- DealPathDialog(“c:\\test”,"test.txt") # #返回值说明: #- 成功:返回true #- 失败:返回false def DealPathDialog(file_path,file_name = '',timeout = @DealPathDialogTimeOut ) $logger.log("调用函数:LibAutoit.rb文件中的DealPathDialog(#{file_path},#{file_name},#{timeout})",'N' ) start_time = Time.now.to_i while 1 if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 win_title = GetDialogTitle(6) #获取窗口标题 if (win_title) @autoit.WinActivate(win_title,'') #if (file_name == '') #file_name = ControlGetText(win_title,'','Edit1') #end if (!FileTest::exist?(file_path)) FileUtils.makedirs(file_path) end file_full_path = "#{file_path}\\#{file_name}" real_file_path = $TxtClass.GetRealPath(file_full_path,'N') real_full_path = "#{real_file_path}\\#{file_name}" ControlSetText(win_title,'','Edit1',real_full_path) SendKey("!S") #ControlClick(win_title,'','Button2') if (WinExists(win_title,'替换') == 1) @autoit.WinActivate(win_title,'替换') ControlClick(win_title,'替换','Button1') end $logger.log("调用函数结束:LibAutoit.rb文件中的DealPathDialog()" ,'N') return true end else $logger.log("Deal File Path Dialog Fail!") $logger.log("调用函数结束:LibAutoit.rb文件中的DealPathDialog()" ,'N') return false end end end #功能说明:处理选择文件对话框窗口 # #参数说明:无 #- file_path:文件下载后存放的路径,格式:目录+文件名,如:c:\\test\\test.txt #- timeout:对话框处理的超时时间,默认为20秒 # #调用示例: ChooseFileDialog(“c:\\test\\test.txt”,15) # #返回值说明: #- 成功:返回true #- 失败:返回false def ChooseFileDialog(file_path,timeout = @ChooseFileDialogTimeOut) $logger.log("调用函数:LibAutoit.rb文件中的ChooseFileDialog(#{file_path},#{timeout})" ,'N') $logger.log('----------deal with Choose File dialog begin----------') start_time = Time.now.to_i while 1 if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 win_title = GetDialogTitle(1) #获取窗口标题 if (win_title) @autoit.WinActivate(win_title,'') if (FileTest::exist?(file_path)) real_file_path = $TxtClass.GetRealPath(file_path) #获取真实路径 ControlSetText(win_title,'','Edit1',real_file_path) ControlClick(win_title,'','Button2') $logger.log('----------deal with Choose File dialog end----------') $logger.log("调用函数结束:LibAutoit.rb文件中的ChooseFileDialog(#{file_path},#{timeout})",'N' ) return true else $logger.log('----------deal with Choose File dialog end----------') $logger.log("调用函数结束:LibAutoit.rb文件中的ChooseFileDialog(#{file_path},#{timeout})",'N' ) return false end end else $logger.log("Deal Choose File Dialog Fail!") $logger.log('----------deal with Choose File dialog end----------') $logger.log("调用函数结束:LibAutoit.rb文件中的ChooseFileDialog(#{file_path},#{timeout})" ,'N') return false end end end #功能说明:处理Alter对话框 # #参数说明: #- timeout:对话框处理的超时时间,默认为20秒 # #调用示例: DealAlterDialog(15) # #返回值说明: #- 成功:返回对话框中的文本提示内容 #- 失败:返回false def DealAlterDialog(timeout = @DealAlterDialogTimeOut ) $logger.log("调用函数:LibAutoit.rb文件中的DealAlterDialog(#{timeout})" ,'N') $logger.log('----------deal with alter dialog begin----------') start_time = Time.now.to_i while 1 if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 win_title = GetDialogTitle(2) #获取窗口标题 if (win_title) @autoit.WinActivate(win_title,'') alter_content = ControlGetText(win_title,'','Static2') SendKey('{ENTER}') #ControlClick('Windows Internet Explorer','','Button1') #puts "alter message:\n #{alter_content}" $logger.log("the content of alter dialog: #{alter_content} ",'N') $logger.log('-----------deal with alter dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealAlterDialog(#{timeout})" ,'N') return alter_content end else $logger.log("deal with alter dialog fail!") $logger.log('-----------deal with alter dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealAlterDialog(#{timeout})" ,'N') return false end end end #功能说明:处理Confirm对话框 # #参数说明: #- type:点击确定或取消按钮,Y:确定 N:取消 #- timeout:对话框处理的超时时间,默认为20秒 # #调用示例: DealConfirmDialog() # #返回值说明: #- 成功:返回对话框中的文本提示内容 #- 失败:返回false def DealConfirmDialog(type = 'Y',timeout = @DealConfirmDialogTimeOut) $logger.log("调用函数:LibAutoit.rb文件中的DealConfirmDialog()" ,'N') $logger.log('----------deal with confirm dialog begin----------') start_time = Time.now.to_i while 1 if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 win_title = GetDialogTitle(2) #获取窗口标题' if (win_title) @autoit.WinActivate(win_title,'') confirm_content = ControlGetText(win_title,'','Static2') case type when 'Y' ControlClick(win_title,'','Button1') when 'N' ControlClick(win_title,'','Button2') end $logger.log("the content of confirm dialog: #{confirm_content} ",'N') $logger.log('-----------deal with confirm dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealConfirmDialog()" ,'N') return confirm_content #puts "窗口提示信息:#{confirm_content}" end else $logger.log("deal with confirm dialog fail!") $logger.log('-----------deal with confirm dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealConfirmDialog()" ,'N') return false end end end #功能说明:处理Prompt对话框 # #参数说明: #- string:输入的文本内容 #- type:点击确定或取消按钮,Y:确定 N:取消 #- timeout:对话框处理的超时时间,默认为20秒 # #调用示例: DealPromptDialog('test','Y',15) # #返回值说明: #- 成功:返回true #- 失败:返回false def DealPromptDialog(string = '',type = 'Y',timeout = @DealPromptDialogTimeOut ) $logger.log("调用函数:LibAutoit.rb文件中的DealPromptDialog(#{string},#{type},#{timeout})" ,'N') puts '----------deal with prompt dialog begin----------' start_time = Time.now.to_i while 1 if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 win_title = GetDialogTitle(3) #获取窗口标题 if (win_title) @autoit.WinActivate(win_title,'') ControlSetText(win_title,'','Edit1',string) case type when 'Y' ControlClick(win_title,'','Button1') when 'N' ControlClick(win_title,'','Button2') end $logger.log('-----------deal with prompt dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealPromptDialog(#{string},#{type},#{timeout})" ,'N') return true end else $logger.log("deal with prompt dialog fail!") $logger.log('-----------deal with prompt dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的DealPromptDialog(#{string},#{type},#{timeout})" ,'N') return false end end end #功能说明:点击页面链接,使弹出安全警告对话框 # #参数说明: #- win_title:IE页面的标题 #- timeout:对话框处理的超时时间 # #调用示例: DealSecurity("ie标题",15) # #返回值说明: #- 成功:返回true #- 失败:返回false def DealSecurity(win_title,timeout = @DealSecurityTimeOut) $logger.log("调用函数:LibAutoit.rb文件中的DealSecurity(#{win_title},#{timeout})" ,'N') start_time = Time.now.to_i if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 while 1 @autoit.ControlClick(win_title,'','Button1') sleep(1) SendKey('{DOWN}') SendKey('{ENTER}') sleep(1) clearSecurityAlert('Y') #清除安全框窗口 return true end else $logger.log('Deal Security Fail!') $logger.log("调用函数结束:LibAutoit.rb文件中的DealSecurity(#{win_title},#{timeout})" ,'N') return false end end #功能说明:处理安全警告对话框 # #参数说明: #- type:选择点击哪个按钮,Y:确定 N:取消 #- timeout:对话框处理的超时时间 # #调用示例: DealSecurity("ie标题",15) # #返回值说明: #- 成功:返回安全警告窗口中的提示信息 #- 失败:返回false def clearSecurityAlert(type = 'Y',timeout = @clearSecurityAlertTimeOut) $logger.log("调用函数:LibAutoit.rb文件中的clearSecurityAlert(#{type},#{timeout})" ,'N') $logger.log('----------deal with Security dialog begin----------') #处理安全对话框 start_time = Time.now.to_i while 1 if (Time.now.to_i-start_time < timeout) #判断处理时间是否超时 win_title = GetDialogTitle(4) #获取窗口标题 if (win_title) alter_content = ControlGetText(win_title,'','Static2') case type when 'Y' ControlClick(win_title,'','Button1') when 'N' ControlClick(win_title,'','Button2') end #puts "安全警告信息:\n#{alter_content}" $logger.log("the content of Security dialog: #{alter_content} ") $logger.log('-----------deal with Security dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的clearSecurityAlert(#{type},#{timeout})" ,'N') return alter_content end else $logger.log("The Security Alert Windows is not exist!") $logger.log('-----------deal with Security dialog end-----------') $logger.log("调用函数结束:LibAutoit.rb文件中的clearSecurityAlert(#{type},#{timeout})",'N' ) return false end end end #功能说明:模拟键盘输入字符 # #参数说明: #- string:输入的字符串信息, #- timeout:对话框处理的超时时间 # #调用示例: #- Send("#r") 将发送 Win+r,这将打开“运行”对话框. #- Send("^!a") 发送按键 "CTRL+ALT+a". #- Send(" !a") 按下"ALT+a". # #返回值说明:无 def SendKey(string = '{ENTER}') @autoit.Send(string) end #功能说明:给对话框中的某个控件设置值 # #参数说明: #- win_title:对话框窗口的标题 #- win_text:对话框窗口中显示的文本 #- id:对话框窗口中某个控件的ID #- string:控件设置的值 # #调用示例: 无 # #返回值说明:无 def ControlSetText(win_title,win_text,id,string = '',flag = 1) #修改指定控件的文本 @autoit.WinActivate(win_title,win_text) if (ControlFocus(win_title,win_text,id) == 1) @autoit.ControlSetText(win_title,win_text,id,string) end end #功能说明:向指定控件发送鼠标点击命令 # #参数说明: #- win_title:目标窗口标题. #- win_text:目标窗口文本. #- id:目标控件ID #- button_type:按键 [可选参数] 要点击的按钮, 可以是"left", "right", "middle", "main", "menu", "primary", "secondary". 默认为left(左键). #- click_time :要点击鼠标按钮的次数. 默认值为 1. # #调用示例: 无 # #返回值说明:无 def ControlClick(win_title,win_text,id,button_type =1,click_time = 1) @autoit.AutoItSetOption("WinTitleMatchMode", 3) @autoit.WinActivate(win_title,win_text) case button_type when 1 #点击鼠标左键 button_type = 'left' when 2 #点击鼠标右键 button_type = 'right' when 3 #点击鼠标中间键 button_type = 'middle' end @autoit.ControlClick(win_title,win_text,id,button_type,click_time) end #功能说明:获取指定控件值 # #参数说明: #- win_title:目标窗口标题. #- win_text:目标窗口文本. #- id:目标控件ID # #调用示例: 无 # #返回值说明: #- 返回获取的文本内容 def ControlGetText(win_title,win_text,id) if (ControlGetHandle(win_title,win_text,id) != "") control_text = @autoit.ControlGetText(win_title,win_text,id) return control_text end end #功能说明:获取指定控件值的句柄 # #参数说明: #- win_title:目标窗口标题. #- win_text:目标窗口文本. #- id:目标控件ID # #调用示例: 无 # #返回值说明: #- 返回获取的控件句柄 def ControlGetHandle(win_title,win_text,id) ret = @autoit.ControlGetHandle(win_title,win_text,id) return ret end #功能说明:设置输入焦点到指定窗口的某个控件上 # #参数说明: #- win_title:目标窗口标题. #- win_text:目标窗口文本. #- id:目标控件ID # #调用示例: 无 # #返回值说明:无 def ControlFocus(win_title,win_text,id) #设置输入焦点到指定窗口的某个控件上 ret = @autoit.ControlFocus(win_title,win_text,id) return ret end #功能说明:判断窗口是否存在 # #参数说明: #- win_title:目标窗口标题. #- win_text:目标窗口文本,默认为空 # #调用示例: 无 # #返回值说明:返回窗口对象 def WinExists(win_title,win_text = '') #检查指定的窗口是否存在 ret = @autoit.WinExists(win_title,win_text = '') return ret end end end
HTML示例,保存到d:\test.html
<html> <head> <title>watir处理对话框</title> <script language= "javascript" type="text/javascript"> function clickbutton(flag) { if (flag == 1) alert("测试alter对话框"); if (flag == 2) prompt("测试prompt对话框"); if (flag == 3) confirm('测试confirm对话框', '测试confirm对话框?',"测试结果:"); } </script> </head> <body> <center> <H1>watir处理对话框<H1> <hr> <table border ="2"> <tr> <th bgcolor = "#aaaaaa">测试内容</th> <th bgcolor = "#aaaaaa">操作</th> </tr> <tr> <td>文件上传</td> <td><input type="file" name="attach[]" /></td> </tr> <tr> <td>alter对话框</td> <td> <input type="button" name="alterbutton" value="测试alter对话框" onclick = "clickbutton(1);" /> </td> </tr> <tr> <td>prompt对话框</td> <td> <input type="button" name="promptbutton" value="测试prompt对话框" onclick = "clickbutton(2);" /> </td> </tr> <tr> <td>confirm对话框</td> <td> <input type="button" name="confirmbutton" value="测试confirm对话框" onclick = "clickbutton(3);" /> </td> </tr> </table> </center> </body> </html>
处理对话框示例:
require 'watir' require 'libAutoit' require 'jcode' $KCODE = 'e' at = Autoit::AutoItApi.new winClicker = WinClicker.new url = "D:\\test.html" ie =Watir::IE.new ie.goto(url) ie.maximize() sleep(1) at.DealSecurity("watir处理对话框 - Windows Internet Explorer") puts "\n" sleep(1) #test alter dialog ie.button(:name,"alterbutton").click_no_wait sleep(10) at.DealAlterDialog puts "\n" #test prompt dialog ie.button(:name,"promptbutton").click_no_wait sleep(10) at.DealPromptDialog('test') puts "\n" #test confirm dialog ie.button(:name,"confirmbutton").click_no_wait sleep(10) at.DealConfirmDialog('Y')
2.
require 'watir\winClicker' #require 'watir\contrib\enabled_popup' ie = Watir::IE.new ie.goto("http://mail.163.com/") # 如果 IE 阻止了活动内容(JavaScript脚本)的运行, # 需要利用这 5 秒时间手工点击IE信息栏,允许阻止的内容运行, # 不然点击 Button 不会弹出窗口 sleep 5 ie.button(:id, 'loginBtn').click_no_wait # 查找弹出窗口,设置查找超时时间为10秒 #如果窗口存在,则获取他的句柄,(10)指的是等待时间; hwnd = ie.enabled_popup(10) print hwnd w = WinClicker.new # 点击弹出窗口中的“确定”按钮 w.clickWindowsButton_hwnd(hwnd,"确定")
3.
require 'watir' #require 'win32ole' # already included if you use 'require watir' # # Function to look for popups 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('Microsoft Internet Explorer', '', 1) # # 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 # # MAIN APPLICATION CODE # Setup popup handler $popup = Thread.new { check_for_popups } # start popup handler at_exit { Thread.kill($popup) } # kill thread on exit of main application # # Main application code follows ie = Watir::IE.new ie.goto("http://localhost:88/Test3.html") ie.link(:text,"Click Here").click
4.
autoit = WIN32OLE.new('AutoItX3.Control') autoit.Send('{enter}')
5.在Watir中整合AutoIt处理JavaScript弹出窗口的方法
[摘要:以下例子整开了 AutoIt ,正在独自的线程中监控战处置惩罚 JavaScript 弹出窗心: require 'watir' #require 'win32ole' # already included if you use 'require watir' # # Function to look for popups def check_for_popups autoit =]
以下例子整合了AutoIt,在单独的线程中监控和处理JavaScript弹出窗口:
require 'watir' #require 'win32ole' # already included if you use 'require watir' # # Function to look for popups 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('Microsoft Internet Explorer', '', 1) # # 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 # # MAIN APPLICATION CODE # Setup popup handler $popup = Thread.new { check_for_popups } # start popup handler at_exit { Thread.kill($popup) } # kill thread on exit of main application # # Main application code follows ie = Watir::IE.new ie.goto("http://localhost:88/Test3.html") ie.link(:text,"Click Here").click
6.Alert标题:
在火狐上 用 alert("你好");
弹出框的标题栏文字是(即时弹出框的上方) “来自xxx的页面说:” 这个可以更改吗?
ie下是 Microsoft Internet Explorer