module Autoit class AutoItApi def initialize require 'win32ole' require 'watir/windowhelper' WindowHelper.check_autoit_installed @autoit = WIN32OLE.new("AutoItX3.Control") end #def initialize end def DealAlterDialog() win_title = 'Windows Internet Explorer' puts '----------deal with alter dialog begin----------' if (WinExists(win_title,'') == 1) @autoit.WinActivate(win_title,'') alter_content = ControlGetText(win_title,'','Static2') SendKey('{ENTER}') #ControlClick('Windows Internet Explorer','','Button1') #puts "alter message:\n #{alter_content}" puts "the content of alter dialog: #{alter_content} " puts '-----------deal with alter dialog end-----------' return alter_content else puts "windows is not exist!" end end def DealConfirmDialog(type = 'Y') win_title = 'Windows Internet Explorer' puts '----------deal with confirm dialog begin----------' if (WinExists(win_title,'') == 1) @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 puts "the content of confirm dialog: #{confirm_content} " puts '-----------deal with confirm dialog end-----------' return confirm_content #puts "窗口提示信息:#{confirm_content}" end end def DealPromptDialog(string = '',type = 'Y') win_title = 'Explorer 用户提示' puts '----------deal with prompt dialog begin----------' if (WinExists(win_title,'') == 1) @autoit.WinActivate(win_title,'') ControlSetText(win_title,'','Edit1',string) case type when 'Y' ControlClick(win_title,'','Button1') when 'N' ControlClick(win_title,'','Button2') end end puts '-----------deal with prompt dialog end-----------' end def DealSecurity(win_title) @autoit.ControlClick(win_title,'','Button1') sleep(1) SendKey('{DOWN}') SendKey('{ENTER}') sleep(1) clearSecurityAlert('Y') #清除安全框窗口 end def clearSecurityAlert(win_title = '安全警告',type = 'Y') puts '----------deal with Security dialog begin----------' #处理安全对话框 if (WinExists('安全警告','') == 1) alter_content = ControlGetText('安全警告','','Static2') case type when 'Y' ControlClick('安全警告','','Button1') when 'N' ControlClick('安全警告','','Button2') end #puts "安全警告信息:\n#{alter_content}" puts "the content of Security dialog: #{alter_content} " puts '-----------deal with Security dialog end-----------' return alter_content else puts "当前不存在需要处理的安全窗口!" end if (WinExists('Security Alert','') == 1) alter_content = ControlGetText('Security Alert','','Static2') case type when 'Y' ControlClick('Security Alert','','Button1') when 'N' ControlClick('Security Alert','','Button2') end #puts "Security Alert message:\n#{alter_content}" puts "the content of Security dialog: #{alter_content} " puts '-----------deal with Security dialog end-----------' return alter_content else puts "The Security Alert Windows is not exist!" end end def SendKey(string = '{ENTER}') # Send("#r") 将发送 Win+r,这将打开“运行”对话框. #Send("^!a") 发送按键 "CTRL+ALT+a". #Send(" !a") 按下"ALT+a". @autoit.Send(string) end def ControlSetText(win_title,win_text,id,string = '',flag = 1) #修改指定控件的文本 if (WinExists(win_title,win_text) == 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 end def ControlClick(win_title,win_text,id,button_type =1,click_time = 1) #向指定控件发送鼠标点击命令 #win_title:目标窗口标题. #win_text:目标窗口文本. #id:目标控件ID #button_type:按键 [可选参数] 要点击的按钮, 可以是"left", "right", "middle", "main", "menu", "primary", "secondary". 默认为left(左键). #click_time :要点击鼠标按钮的次数. 默认值为 1. @autoit.AutoItSetOption("WinTitleMatchMode", 3) if (WinExists(win_title,win_text) == 1) @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) else puts "窗口(标题:#{win_title})不存在" end end 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 def ControlGetHandle(win_title,win_text,id) ret = @autoit.ControlGetHandle(win_title,win_text,id) return ret end def ControlFocus(win_title,win_text,id) #设置输入焦点到指定窗口的某个控件上 ret = @autoit.ControlFocus(win_title,win_text,id) return ret end def WinExists(win_title,win_text = '') #检查指定的窗口是否存在 ret = @autoit.WinExists(win_title,win_text = '') return ret end end end
|