# How to capture text from a popup window..in watir

**URL:** <https://www.ruby-forum.com/t/how-to-capture-text-from-a-popup-window-in-watir/92804>\
**Category:** Rails\
**Created:** [April 24, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/how-to-capture-text-from-a-popup-window-in-watir/92804 "2007-04-24T12:39:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SHALINI\_GUPTA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@SHALINI\_GUPTA](https://www.ruby-forum.com/u/SHALINI_GUPTA)\
**Post date:** [April 24, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/how-to-capture-text-from-a-popup-window-in-watir/92804/1 "2007-04-24T12:39:11Z")

</div>

Hi all,

## I got the answer how to click on ‘ok’ button. But still i have two problems

that code is not working when im trying to run that code through the exe  
file of that ruby file which is using that code.

and second one is i want to capure the text of that popup…

i tried this with no success

#----- js\_dialog.rb -----

require ‘win32ole’  
require ‘test/unit’  
require ‘test/unit/ui/console/testrunner’

# 

# Use AutoIt to read and close Javascript dialog windows

# 

module JavascriptDialog

```
#
# Target javascript dialogs with this window title
#
WINDOW_TITLE = "Microsoft Internet Explorer"

class << self

    #
    # Return the text contained in a javascript dialog (e.g. an 

```

“alert()”)  
# if such a dialog is present or appears within +wait\_seconds+.  
#

```
    def text(wait_seconds = 1)
        # sleep 0.3
        autoit.WinWait(WINDOW_TITLE, nil, wait_seconds) if 

```

wait\_seconds  
s = autoit.WinGetText(WINDOW\_TITLE)  
s unless s == “1”

```
    end

    #
    # Close any active javascript dialog
    #
    def close
        autoit.WinClose WINDOW_TITLE
    end

    #
    # Press the "OK" button on a javascript dialog

    #
    def ok
        autoit.ControlClick(WINDOW_TITLE, "", "OK")
    end
    #
    # Press the "Cancel" button on a javascript dialog
    #

    def cancel
        autoit.ControlClick(WINDOW_TITLE, "", "Cancel")
    end
    #
    # Press the "Yes" button on a javascript dialog
    #
    def yes

        autoit.ControlClick(WINDOW_TITLE, "", "Yes")
    end
    #
    # Press the "No" button on a javascript dialog
    #
    def no

```

autoit.ControlClick(WINDOW\_TITLE, “”, “No”)  
end

```
    private

        #
        # Return an AutoIt COM object, creating it if it doesn't 

```

already  
# exist

```
        #
        def autoit
            unless defined? @@autoit
                @@autoit = WIN32OLE.new("AutoItX3.Control")
            end
            @@autoit
        end

end

```

end

module Test::Unit::Assertions  
#  
# Passes if a Javascript dialog appears within +wait\_seconds+ and  
its  
# text matches the given (optional) pattern.  
#  
# Use like this:

```
# assert_js_dialog do
# watir_command_to_make_dialog_appear
# end
# Or like this:
# assert_js_dialog /Text to find in the dialog/, "Cancel" do
# watir_command_to_make_dialog_appear

# end
#
def assert_js_dialog(pattern = //, action = "close", message = nil)
    _wrap_assertion do
        begin
            pipe = IO.popen("ruby exetry.rb #{action}")

            yield
            window_text = pipe.read rescue ""
            pipe.close
            unless window_text.empty?
                assert_block(build_message(message,

                    "<?> not found in JavaScript dialog.", pattern)) 

```

do  
window\_text.match(pattern)  
end  
else  
raise Test::Unit::  
AssertionFailedError.new(  
build\_message(message, “No JavaScript window  
found.”))  
end  
ensure  
pipe.close if pipe && !pipe.closed?

```
        end
    end
end

```

end

# test - will close and print the text of an opened javascript dialog if

# run as “ruby js\_dialog.rb”

if $0 == **FILE**  
action = ARGV.shift  
|| “close”  
print JavascriptDialog.text || “”  
JavascriptDialog.send action  
end
