我使用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)`高清startClicker(按鈕,等待時間= 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)`需要 '的Watir-經典\的contrib \ enabled_popup'
DEF startClicker(按鈕,WAITTIME = 9,USER_INPUT =無)
HWND = $ ie.enabled_popup(WAITTIME)
如果(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(:名稱, 「delete_action」)click_no_wait
startClicker( 「OK」,7)`
Get消息 - 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)
如果(HWND)
彈出= WinClicker.new
彈出。makeWindowActive(HWND)
popup.clickWindowsButton( 「從網頁信息」, 「OK」, 「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:在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
block中enabled_popup' from E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/wait.rb:18:在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' FR OM Task.rb:145:在<main>'
8)'需要 '的Watir經典\的contrib \ enabled_popup'
高清popupChecker(文本)
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`
Get消息:Task.rb:13:in
塊在popupChecker ':未定義的方法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(標題= 「從網頁信息」,按鈕= 「OK」)
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`
從出現的網頁信息後10碼 -
check_for_popups("Message from webpage", "OK")
沒有反應
謝謝browser.alert.ok不工作(我得到消息,該警報沒有被定義的方法),但代碼browser.execute_script(「window.confirm = function(){return true}「)可以工作 – khris 2012-07-05 12:21:30