Tuesday, May 11, 2010

javascript popup clicker

Ruby watir script  for clicking javascript popup.
require 'fileutils'
require 'win32ole'

def check_for_popups(title="Message from webpage", button="OK")
  popup=Thread.new {
    autoit=WIN32OLE.new('AutoItX3.Control')
    ret=autoit.WinWait(title,"",60)
    if (ret==1)
      autoit.WinActivate(title)
      button.downcase!
      if button.eql?("ok") || button.eql?("yes") || button.eql?("continue")
        autoit.Send("{Enter}")
      else
        autoit.Send("{tab}")
        autoit.Send("{Enter}")
      end
    elsif (ret==0)
        puts "No popup, please check your code."
    end
    }
  at_exit { Thread.kill(popup) 
  }
end 
and below for Internet Explorer authetication popup
def login(vusername,vpassword,vtest_site)
    @user = vusername
    @pass = vpassword
    @url = vtest_site
    a = Thread.new {
      FileUtils.rm_rf vcookiedir
      $ie = Watir::Browser.new
      $ie.goto(@url)
      sleep 15  
    }
    sleep 15   
    autoit=WIN32OLE.new('AutoItX3.Control')
    autoit.Send(@user)
    autoit.Send('{TAB}')
    autoit.Send(@pass)    
    autoit.Send('{ENTER}')
    a.join
end 

No comments:

Post a Comment