路漫漫其修远兮,吾将上下而求索

如何让Watir脚本在不同环境下运行 2

上一篇 / 下一篇  2010-03-03 15:59:34 / 个人分类:Ruby_Watir

 

(四)使用watir对来对JavaScript. Pop Ups进行处理

一直都是在半知半解的状态下使用autoit来对Javascript. pop ups进行处理,虽然可以处理成功,但是还是不太明确为什么要这样做才可以点击到“确定”按钮等。前阵子为了使脚本能够在不干扰本机用户的使用情况下正常运行,搜索资料查找到一个非常有用的方法。

(我想如果你仔细读懂了这  个链接中所给出的内容,你处理弹出对话框的问题应该也没有太多的问题了。http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups

该方法可以方便的让你的脚本正常运行,并且不影响本机用户的正常运行。并且这个方法不需要依靠autoit来运行脚本,仅仅使用win32ole方法来对弹出的对话框进行出来。

步骤为:

(1)              首先向库文件ie-class.rb添加如下代码,(库文件所在位置C:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.2\lib\watir\ie-class.rb)

#/////////////////////////////////////////////////////////////////////////////
#Name: clickprompt
#Description: Waits for the popup to appear and then clicks the ok or cancel button
#
#Arguments: button: The button to be pressed – “OK” or “CANCEL”
# txt: Text to be passed into the prompt dialog box
# waitTime: the waiting time caluclated as waitingtime/0.2 currently set to 50 seconds
#Returns: This returns the popup text if popup is found or ” if no popup is found
#/////////////////////////////////////////////////////////////////////////////
def clickprompt(but=”OK”,txt =”", waitTime = 10)
tim = 0
poptxt= ”
while tim < waitTime
sleep 0.2
pophwnd = Win32API.new(”user32″, “GetWindow”, ‘Li’, ‘L’).Call(@ie.hwnd.to_i, 6)
# the above returns any popup windows that are present for the specific window
tim += 0.2
tim += waitTime if pophwnd != 0
end
return ” if pophwnd == 0
button = but.upcase
outval = ‘ ‘ * 30
Win32API.new(”user32″, “GetWindowText”, ‘Lpi’, ‘L’).Call(pophwnd,outval,30)
popwndtitle = outval.rstrip.chomp(”00″) # window title stored here
outval = nil
#poptype = 0
#alert and confirm have ie6 – Microsoft Internet Explorer
#ie7 – Windows Internet Explorer
#ie8 – Message from webpage
if popwndtitle.include?(”Microsoft Internet Explorer”) ||
popwndtitle.include?(”Windows Internet Explorer”) ||
popwndtitle.include?(”Message from webpage”)
#confirm and alerts have the above 3 window titles
# poptype =1 means this is a javascript. alert tag
#poptype = 1
poptxt = handlepopup1(pophwnd,button)
elsif popwndtitle.include?(”Explorer User Prompt”)
#prompts have the above window title
#poptype = 2
poptxt = handlepopup2(pophwnd,button, txt)
elsif popwndtitle.include?(”Connect to”)
#authentication dialog
#also make sure the username and password text fields are present – if present we got the auth dialog
cntrlhwnd = 0
cntrlhwnd = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 1002)
#poptype = 3 if cntrlhwnd != 0 #verified the 2 textboxes are present to enter the values
return ” if cntrlhwnd == 0
poptxt = handlepopup3(pophwnd,button, prompt)
end
return poptxt
end

def handlepopup1(pophwnd, button)
# handles the alerts and confirm dialogs
#Yes there is a popupwindow… hence get the controlhandle for the text control – 65535
cntrlhwnd = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 65535)
#now get the text from the popup
outval = ‘ ‘ * 900
Win32API.new(”user32″, “GetWindowText”, ‘Lpi’, ‘L’).Call(cntrlhwnd,outval, 900)
poptext = outval.rstrip.chomp(”00″)
outval = nil

#confirm ok-1 and cancel-2, alert ok-2
cntrlhwndOK = 0
cntrlhwndCANCEL = 0
cntrlhwndOK = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 1)
if cntrlhwndOK == 0 # only 1 button alert
cntrlhwndOK = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 2)
clickWin32Button(cntrlhwndOK) # done clicking javascript. ok button
return poptext
else # this is a confirm with 2 buttons
cntrlhwndCANCEL = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 2)
end
button.include?(”OK”) ? clickWin32Button(cntrlhwndOK) : clickWin32Button(cntrlhwndCANCEL)
#clickWin32Button(cntrlhwndCANCEL)
return poptext
end
private :handlepopup1

def handlepopup2(pophwnd, button, prompt)
#handles prompt boxes which takes a value as input
cntrlhwndOK = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 1)
cntrlhwndCANCEL = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 2)

# get handle to the text control from the prompt box
cntrlpromptText = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 8132)
#now get the text from the popup
outval = ‘ ‘ * 200
Win32API.new(”user32″, “GetWindowText”, ‘Lpi’, ‘L’).Call(cntrlpromptText,outval, 200)
poptext = outval.rstrip.chomp(”00″)
outval = nil

cntrltextarea = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 8133)
if prompt.size != 0
sendmessage = Win32API.new(”user32″, “SendMessage”, ‘LLpp’, ‘L’)
sendmessage.Call(cntrltextarea, 0×000C, ”, prompt) # calling sendmessage with WM_SETTEXT
end
button.include?(”OK”) ? clickWin32Button(cntrlhwndOK) : clickWin32Button(cntrlhwndCANCEL)
return poptext
end
private :handlepopup2

def handlepopup3(pophwnd, button, prompt)
# handles the auth dialog box , 3 tries then the 401 page is shown
cntrlhwnd = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 1002)
cntrlusername = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(cntrlhwnd, 1003)
cntrlpassword = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(cntrlhwnd, 1005)
if prompt.size == 2
sendmessage = Win32API.new(”user32″, “SendMessage”, ‘LLpp’, ‘L’)
sendmessage.Call(cntrlusername, 0×000C, ”, prompt[0]) # calling sendmessage with WM_SETTEXT
sendmessage.Call(cntrlpassword, 0×000C, ”, prompt[1]) # calling sendmessage with WM_SETTEXT
end
cntrlhwndOK = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 1)
cntrlhwndCANCEL = Win32API.new(”user32″, “GetDlgItem”, ‘Li’, ‘L’).Call(pophwnd, 2)
button.include?(”OK”) ? clickWin32Button(cntrlhwndOK) : clickWin32Button(cntrlhwndCANCEL)

end
private :handlepopup3

def clickWin32Button(cntrlhwnd)
Win32API.new(”user32″, “SendMessage”,’LLLL’,'L’).Call(cntrlhwnd, 0×0006, 1,0)
Win32API.new(”user32″, “SendMessage”,’LLLL’,'L’).Call(cntrlhwnd, 0×00F5, 0,0)
end
private :clickWin32Button
(2)对于系统语言的不同,浏览器弹出来的对话框及对话框中的一些按钮也是不尽相同的,所以可能需要像你修改下刚刚所添加进来的clickprompt方法。

比如在中文vista操作系统下的ie7浏览器,弹出来的对话框的title值为中文,所以需要修改下 clickprompt方法下的if语句

if popwndtitle.include?(”Microsoft Internet Explorer”) ||
popwndtitle.include?(”Windows Internet Explorer”) ||
popwndtitle.include?(”Message from webpage”) ||popupwndtitle.include?(”来自网页的消息”)


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-13  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 40483
  • 日志数: 76
  • 图片数: 2
  • 建立时间: 2007-11-02
  • 更新时间: 2011-08-13

RSS订阅

Open Toolbar