Search code examples
messagewatir

how click "ok" in window "Massage from webpage"


I use watir for testing in RadRails IDE.After clicking button Delete appears window "Message from webpage" which confirms if I really want delete record. How can I simulate clicking OK? Here are methods i try: 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}")

Get error - 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}")

Have no effect

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`

And put

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

   startClicker("OK" , 3)

   ie.button("Submit").click`

after message from webpage appears. Get error:

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) Code in the begining of test

`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) } `

No reaction.

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`

Code after message from webpage appears

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

startClicker( "OK ", 7)`

Get message - Task.rb:14:instartClicker': undefined method enabled_popup' for nil:NilClass (NoMethodError) from Task.rb:157:in'`

7)require 'watir/contrib/enabled_popup'

Code after message from webpage appears -

`hwnd = browser.enabled_popup(5)

if (hwnd)

popup = WinClicker.new

popup.makeWindowActive(hwnd)

popup.clickWindowsButton("Message from webpage", "OK", "30")

end`

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's DL API not compatible with 1.8, see http://www.ruby-forum.com/topic/138277 (NotImplementedError) from 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:inblock in enabled_popup' from 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' from Task.rb:145:in <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`

Code after message from webpage appears -

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

popupChecker('OK')

ie1.wait`

Get message:Task.rb:13:inblock in popupChecker': undefined method enabled_popup' for nil:NilClass (NoMethodError) from E:/Ruby193/lib/ruby/1.9.1/timeout.rb:68:intimeout' from Task.rb:11:in popupChecker' from Task.rb:158:in'`

9)`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)

        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`

Code after message from webpage appears -

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

No reaction


Solution

  • Try this:

    browser.alert.ok
    

    More information: http://watir.github.io/docs/javascript-dialogs/