我使用watir在RadRails IDE中进行测试。单击按钮后,出现“窗口消息”窗口,确认我是否真的要删除记录。如何模拟单击确定?
以下是我尝试的方法:
1)ie=Watir::IE.new
ie.execute_script("window.alert = function() {}") - have no effect
2)ie1.button(:name, "delete_action").click
ie4.execute_script("window.confirm = function() {return true}")
获取错误 - Task.rb:140:in '<main>: undefined local variable or method 'ie4' for main:Object (NameError)
3)ie1.button(:name, "delete_action").click
ie1.execute_script("window.confirm = function() {return true}")
无效
4)`def startClicker(button,waitTime = 3)
w = WinClicker.new
longName = ie.dir.gsub("/" , "\\" )
shortName = w.getShortFileName(longName)
c = "start rubyw #{shortName }\\watir\\clickJSDialog.rb #{button }
#{ waitTime} "
puts "Starting #{c}"
w.winsystem(c)
w=nil
end`
然后把
`ie1.button(:name, "delete_action").click
startClicker("OK" , 3)
ie.button("Submit").click`
显示来自网页的消息后。 得到错误:
E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/modal_dialog.rb:3:in 'initialize': Watir no longer supports WinClicker. Please use click_no_wait and the javascript_dialog method. (NotImplementedError)
from Task.rb:12:in 'new'
from Task.rb:12:in 'startClicker'
from Task.rb:162:in '<main>'
5)测试开始时的代码
`def check_for_popups
autoit = WIN32OLE.new('AutoItX3.Control')
loop do
ret = autoit.WinWait('Message from webpage', '', 1)
if (ret==1) then autoit.Send('{enter}') end
sleep(3)
end end`
Code after message from webpage appears
`ie1.button(:name, "delete_action").click
$popup = Thread.new { check_for_popups }
at_exit { Thread.kill($popup) } `
没有反应。
6)`require'watir-classic \ contrib \ enabled_popup'
def startClicker(button,waitTime = 9,user_input = nil)
hwnd = $ ie.enabled_popup(waitTime)
if(hwnd)
w = WinClicker.new
if ( user_input )
w.setTextValueForFileNameField( hwnd, "#{user_input}" )
end
sleep 3
w.clickWindowsButton_hwnd( hwnd, "#{button}" )
w=nil
end
end`
显示来自网页的消息后的代码
`ie1.button(:name,“delete_action”)。click_no_wait
startClicker(“OK”,7)`
获取消息 -
Task.rb:14:in
startClicker':未定义的方法enabled_popup' for nil:NilClass (NoMethodError)
from Task.rb:157:in
'`
7)require 'watir/contrib/enabled_popup'
显示来自网页的消息后的代码 -
`hwnd = browser.enabled_popup(5)
if(hwnd)
popup = WinClicker.new
popup.makeWindowActive(HWND)
popup.clickWindowsButton(“来自网页的消息”,“确定”,“30”)
end`
获取错误 - E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/win32.rb:19:in
阻止':1.9的DL API与1.8不兼容,请参阅http://www.ruby-forum.com/topic/138277(NotImplementedError)
来自E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/contrib/enabled_popup.rb:11:in call'
from E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/contrib/enabled_popup.rb:11:in
块中的enabled_popup'
来自E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/wait.rb:18:in until'
from E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/contrib/enabled_popup.rb:10:in
enabled_popup'
来自Task.rb:145:<main>'
8)`require'watir-classic \ contrib \ enabled_popup'
def popupChecker(text)
Timeout::timeout(2)do
begin
if $ie.enabled_popup
hwnd = ie.enabled_popup(5)
w = WinClicker.new
w.makeWindowActive(hwnd)
w.clickWindowsButton_hWnd(hwnd,text)
end
rescue Timeout::Error
puts 'No popup existed'
end
end
end`
显示来自网页的消息后的代码 -
`ie1.button(:name,“delete_action”)。click_no_wait
popupChecker( 'OK')
ie1.wait`
获取消息:popupChecker中的Task.rb:13:in
阻止':未定义的方法enabled_popup' for nil:NilClass (NoMethodError)
from E:/Ruby193/lib/ruby/1.9.1/timeout.rb:68:in
超时'
来自Task.rb:11:在popupChecker'
from Task.rb:158:in
'`
9)`def check_for_popups(title =“来自网页的消息”,按钮=“确定”)
popup=Thread.new {
autoit=WIN32OLE.new('AutoItX3.Control')
ret=autoit.WinWait(title,"",60)
if (ret==1)
puts "There is popup."
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`
显示来自网页的消息后的代码 -
check_for_popups("Message from webpage", "OK")
没有反应