Watir Ruby 对弹出框进行处理的方法总结

最近想用Watir+Ruby写几个简单的脚本来提高工作效率,功能很简单,但是因为页面有多个security popup和弹出子窗口,还是头疼了几天,网上查了很多解决方法,综合总结如下:

1. AutoItX:

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 

 
(转载自:http://blog.youkuaiyun.com/Testing_is_believing/archive/2010/01/24/5250785.aspx

2. 直接用win32api处理(还没试过不知道是不是能工作,先贴下备忘)

require 'win32API' 

SendMessage = Win32API.new('user32.dll','SendMessageA',['l','l','l','l'],'v') 

FindWindowEx = Win32API.new('user32.dll','FindWindowExA',['l','l','p','p'],'l') 

GetWindowText = Win32API.new('user32.dll','GetWindowTextA',['l','p','l'],'l') 

我们先找到ie的弹出框。 

hwnd =ie.hwnd #根据ie对象获取他的hwnd值 

popup_hwnd = FindWindowEx.call(hwnd,0,"#32770",nil) #'#32770'是ie弹出框的类名 

#找到弹出框后,遍历弹出框上的button控件 

button_hwnds =Array.new 

button_hwnd = FindWindowEx.call(popup_hwnd,0,"button",nil) 

while button_hwnd!=0 

button_hwnds << button_hwnd 

button_hwnd = FindWindowEx.call(popup_hwnd,0,"button",nil) 

end 

#找到button的集合后,要点第i个button,直接使用button_hwnd[i] 

#调用sendmessage点击按钮 

SendMessage.call(button_hwnd[i], WM_CLICK,IDC_BUTTON1,0) 

 
(转载自:http://www.51testing.com/?uid-170805-action-viewspace-itemid-96033)

 

3. 贴上我自己写的一个function,针对弹出窗口有certificate error需要点击的:

###################################################
#Secutiry Popup Page Handling
#############################################
  def handle_popup(pop_type)
    #pop_type = 1: certificate popup page, need to click continue link
    #pop_type = 2: popup sub-window, need to get handler of sub-window and continue actions in new window
    begin
      if pop_type==1 and $ie.title()=~/Certificate Error: Navigation Blocked/i #Certificate popup page
      puts '-----------Certificate Error Page Displayed------------------'
      click_link('id','overridelink')
        
      elsif pop_type==2 #popup sub-window 
         puts '--------Find The Popup Window----------'
         autoit = WIN32OLE.new('AutoItX3.Control')
         ret = autoit.WinWait('Certificate Error: Navigation Blocked', '', 1)
         #puts ret
         if (ret==1) then 
           while $ie2.title()=~/Certificate Error: Navigation Blocked/i do
             #puts $ie2.title()
              i = 9
              while i>0 do
                 autoit.Send('{TAB}')
                 i=i-1
               end
               autoit.Send('{ENTER}')
               sleep(10)
            end
        end
     end     
    end
  end

 以上是用autoit写的方法,之所以要循环,因为我发现用autoit模拟tab和enter键来点击continue的link,经常第一次点击页面没有转换,必须再点击一次,并且要特别注意sleep的时间要足够长,否则点击页面转换就会失败

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值