quv
quv

Reputation: 23

Watir Help: click-no-wait and execute_script not working as expected

Using ruby's Watir gem, I'm trying to click a button that opens a javascript popup, then click 'ok' on the popup and I can't seem to do that. The button is in a frame in a frame. I have searched extensively and read the Watir wiki entry about javascript popups but nothing works.
I think this is because execute_script and click_no_wait are not working as expected.
For example, the following commands:

    @browser.execute_script('window.alert = function() {}')
    @browser.execute_script('alert("hello")')

create a javascript popup that says "hello" instead of the expected behavior of not doing anything.
The command:

    @browser.button(:value, 'Submit').click

Clicks the button which creates a javascript popup. The script then hangs until I manually click 'ok' on the popup.
The command:

    @browser.button(:value, 'Submit').click_no_wait

Does not click the button (or it does click the button and doesn't trigger the onclick event).
I have also tried:

    Timeout::timeout(1) { @browser.button(:value, 'Submit').click }

and

    Thread.new { @browser.button(:value, 'Submit').click }

But Timeout never throws an exception, causing ruby to hang until I manually click 'ok' on the popup, and Watir doesn't click when I run it in a new thread.

Would anyone be able to help or offer a solution?

Edit: The problem is that after @browser.button.click, the script hangs until the popup closes. @browser.alert.ok successfully closes the popup, but the script cannot execute @browser.alert.ok after the button is clicked because it hangs until the popup is closed. This catch-22 is supposed to be solved by click_no_wait, but click_no_wait is not clicking.

System information:
Windows 7 Enterprise 64 Bit
Internet Explorer Version 9.0.12
watir-4.0.2-x86-mingw32
ruby 1.9.3p286 (2012-10-12) [i386-mingw32]

Upvotes: 2

Views: 2091

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

There is an alert api (watir-classic and watir-webdriver) that you can use to interact with the javascript popup.

Try closing the popup using:

@browser.alert.ok

Update - Workaround:

This appears to be a bug with using Element#click_no_wait for elements in a frame. I have updated Issue 45 and will try to submit a patch.

In the mean time, adding the following monkey patch at the start of your script should solve the issue (ie the click_no_wait should not wait):

require 'watir-classic'

Watir::IE.new(true)
module Watir
    class Frame
        def attach_command
            tag_name = @specifiers[:tag_name].join("' << '")
            @container.page_container.attach_command + ".frame(:tag_name => Array.new << '#{tag_name}', :unique_number => #{unique_number})"
        end
    end
end

Upvotes: 1

Related Questions