0

我在 RadRails IDE 中使用 watir 进行测试。单击“删除”按钮后会出现“来自网页的消息”窗口,确认我是否真的要删除记录。如何模拟单击确定?以下是我尝试的方法: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(按钮,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) `需要'watir-classic\contrib\enabled_popup'

def startClicker(按钮,waitTime=9,user_input=nil)

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

结束`

出现网页消息后的代码

`ie1.button(:name, "delete_action").click_no_wait

startClicker(“确定”,7)`

获取消息 - Task.rb:14:instartClicker':未定义的方法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

popup.makeWindowActive(hwnd)

popup.clickWindowsButton("来自网页的消息", "OK", "30")

结束`

Get error - E:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/win32.rb:19:inblock in ': 1.9 的 DL API 与 1.8 不兼容,请参阅来自 E:/Ruby193/lib/ruby/gems/1.9 的http://www.ruby-forum.com/topic/138277 (NotImplementedError)。 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:inblock 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:inenabled_popup' 来自 Task.rb:145:in<main>'

8)`需要'watir-classic\contrib\enabled_popup'

def 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

结束`

出现网页消息后的代码 -

`ie1.button(:name, "delete_action").click_no_wait

popupChecker('OK')

即1.等待`

获取消息:Task.rb:13:inpopupChecker 中的块':未定义的方法enabled_popup' for nil:NilClass (NoMethodError) from E:/Ruby193/lib/ruby/1.9.1/timeout.rb:68:in超时'来自 Task.rb:11:in popupChecker' from Task.rb:158:in'`

9)`def check_for_popups(title="来自网页的消息", button="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) }

结束`

出现网页消息后的代码 -

check_for_popups("Message from webpage", "OK")

没有反应

4

1 回答 1

1

试试这个:

browser.alert.ok

更多信息:http ://watir.github.io/docs/javascript-dialogs/

于 2012-07-05T09:18:19.423 回答