ruby in WinXP as an automation tool

Discussion in 'Ruby' started by kevin.gc@gmail.com, May 14, 2005.

  1. Guest

    Can anyone tell me if it can be done?
     
    , May 14, 2005
    #1
    1. Advertising

  2. Adelle Hartley, May 14, 2005
    #2
    1. Advertising

  3. James Britt Guest

    wrote:
    > Can anyone tell me if it can be done?


    Can you give examples of some tasks?

    James
     
    James Britt, May 14, 2005
    #3
  4. ruby talk Guest

    Hello,
    AutoIT Does the job well. It has an application that will tell you
    window names and lot of nice tools. It uses a vb like lang to program.
    Becker

    On 5/14/05, Alexey Verkhovsky <> wrote:
    > wrote:
    >=20
    > >Can anyone tell me if it can be done?
    > >
    > >

    > Yes.
    > This is not a very informative answer, I suppose. What kinds of things
    > do you want to automate?
    >=20
    > --
    > Best regards,
    >=20
    > Alexey Verkhovsky
    >=20
    > Ruby Forum: http://ruby-forum.org (moderator)
    > RForum: http://rforum.andreas-s.net (co-author)
    > Instiki: http://instiki.org (maintainer)
    >=20
    >
     
    ruby talk, May 17, 2005
    #4
  5. Martin DeMello, May 17, 2005
    #5
  6. Pit Capitain Guest

    Martin DeMello schrieb:
    > ruby talk <> wrote:
    >
    >>Hello,
    >>AutoIT Does the job well. It has an application that will tell you
    >>window names and lot of nice tools. It uses a vb like lang to program.

    >
    > http://www.rubygarden.org/ruby?AutoIt_For_Windows


    I'm also using the AutoIT DLL driven by Ruby scripts. It really works
    great and already has saved me a lot of key-presses and mouse-clicks.

    Regards,
    Pit
     
    Pit Capitain, May 17, 2005
    #6
  7. Bill Guindon Guest

    On 5/17/05, Pit Capitain <> wrote:
    > Martin DeMello schrieb:
    > > ruby talk <> wrote:
    > >
    > >>Hello,
    > >>AutoIT Does the job well. It has an application that will tell you
    > >>window names and lot of nice tools. It uses a vb like lang to program.

    > >
    > > http://www.rubygarden.org/ruby?AutoIt_For_Windows

    >=20
    > I'm also using the AutoIT DLL driven by Ruby scripts. It really works
    > great and already has saved me a lot of key-presses and mouse-clicks.


    Anybody have any samples of ruby/au3 scripts that are a bit more in
    depth than the wiki example?
    =20
    > Regards,
    > Pit
    >=20
    >=20



    --=20
    Bill Guindon (aka aGorilla)
     
    Bill Guindon, May 17, 2005
    #7
  8. James Britt Guest

    Bill Guindon wrote:
    > Anybody have any samples of ruby/au3 scripts that are a bit more in
    > depth than the wiki example?


    No. But here's a script I just moved over to Ruby.

    On a whim I started wrapping methods so that the core script would be a
    bit tidier.

    # Launch Firefox and check gmail for that
    # special v1agr4 offer
    require 'win32ole'

    def set_up
    @au3 = WIN32OLE.new("AutoItX3.Control")
    @au3.opt("WinTextMatchMode", 2)
    end

    def browse_to( url )
    @au3.Run( "e:\\Firefox\\firefox.exe #{url}" )
    end

    def enter
    @au3.Send( "{ENTER}" )
    end

    def tab
    @au3.Send( "{TAB}" )
    end


    def wait( n )
    @au3.Sleep( n.to_i )
    end

    def send s
    @au3.Send( s.to_s )
    end

    def wait_for_title t
    @au3.WinWaitActive( t.to_s )
    end


    set_up
    name = 'my.name'
    password = 'fake-password'
    browse_to 'http://www.gmail.com'
    wait_for_title "Welcome to Gmail"
    # Make sure whole page has
    # load over flakey WiFi
    sleep 5
    send name
    tab
    send password
    enter
    # The end.



    James
     
    James Britt, May 18, 2005
    #8
  9. Bill Guindon Guest

    On 5/17/05, James Britt <> wrote:
    > Bill Guindon wrote:
    > > Anybody have any samples of ruby/au3 scripts that are a bit more in
    > > depth than the wiki example?

    >=20
    > No. But here's a script I just moved over to Ruby.
    >=20
    > On a whim I started wrapping methods so that the core script would be a
    > bit tidier.


    Thanks much for the example, gives me a good place to start.

    --=20
    Bill Guindon (aka aGorilla)
     
    Bill Guindon, May 18, 2005
    #9
  10. James Britt Guest

    Alexey Verkhovsky wrote:
    > On 5/17/05, James Britt <> wrote:
    >
    >>> No. But here's a script I just moved over to Ruby.
    >>>

    >
    > By the way, if I have a .dll (such as AutoIt) somewhere in
    > my_project/ext, and I want it to work in "download, start, No Step Three
    > (TM)" manner - no changes to Windows registry, no copying of files to
    > c:\WINNT, no other installation to speak of.
    >
    > In this case, what should this line be preceeded with:
    > @au3 = WIN32OLE.new("AutoItX3.Control")



    Good question. The DLL is going to get loaded, I think, by the WIN32OLE
    wrapper, no? So it needs to know where t look, or the dll has to be
    someplace it knows to look.

    Does the AutoIT.dll have to be registered (via regsvr32 or something)
    before win32ole can use it?

    Time to go experiment ...

    James
     
    James Britt, May 18, 2005
    #10
  11. James Britt Guest

    Alexey Verkhovsky wrote:
    > James Britt wrote:
    >
    >> Good question. The DLL is going to get loaded, I think, by the
    >> WIN32OLE wrapper, no? So it needs to know where t look, or the dll
    >> has to be someplace it knows to look.

    >
    >
    > That's my question, in a more pointed version: how do I tell win32ole to
    > look at my_project/ext?


    I believe win32ole is going to want to find the COM automation info in
    the registry, and you'll need to run

    regsvr32.exe AutoItX3.dll

    as part of the installation process. But the DLL can go wherever you
    like; the location is stored when it gets registered.


    James


    --

    http://www.ruby-doc.org
    http://www.rubyxml.com
    http://catapult.rubyforge.org
    http://orbjson.rubyforge.org
    http://ooo4r.rubyforge.org
    http://www.jamesbritt.com
     
    James Britt, May 18, 2005
    #11
  12. James Britt Guest

    Alexey Verkhovsky wrote:
    > James Britt wrote:
    >
    >> I believe win32ole is going to want to find the COM automation info in
    >> the registry, and you'll need to run
    >>
    >> regsvr32.exe AutoItX3.dll
    >>
    >> as part of the installation process. But the DLL can go wherever you
    >> like; the location is stored when it gets registered.

    >
    >
    > Sigh... as I feared.
    > How do I query/edit the registry from within Ruby?


    For what? If the goal is to register the DLL, just use, for example:

    # unreg
    puts `regsvr32 AutoItX3.dll /c /s /u`

    # reg
    puts `regsvr32 AutoItX3.dll /c /s `

    http://www.ss64.com/nt/regsvr32.html


    James


    --

    http://www.ruby-doc.org
    http://www.rubyxml.com
    http://catapult.rubyforge.org
    http://orbjson.rubyforge.org
    http://ooo4r.rubyforge.org
    http://www.jamesbritt.com
     
    James Britt, May 18, 2005
    #12
  13. --Apple-Mail-1-645804600
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    delsp=yes;
    format=flowed

    On May 17, 2005, at 3:28 PM, Bill Guindon wrote:
    > Anybody have any samples of ruby/au3 scripts that are a bit more in
    > depth than the wiki example?


    I'm not using AutoIt, but in case it helps, here's a script I wrote
    today (inspired by this thread) that repeatedly opens different files
    with an application, and measures how much RAM the app is using.

    I've simplified the code a bit from what I'm actually using. For
    example, I snipped out the bit which is testing a bunch of different
    builds of the app to check for variations between builds.

    (Suggestions on cleaner ways to do things are welcome; I really don't
    understand OLE at all, or the WMI service. Although I did find this
    list[1] of properties which you can access on the processes returned
    by the InstancesOf call.)

    class Fixnum
    def sample_average
    sum=0.0
    self.times{ sum += yield }
    sum/self
    end
    end

    class WIN32OLE
    def to_a
    a = []
    self.each{ |p| a<<p }
    a
    end
    end

    require 'win32ole'

    players = {
    '25139' => 'C:/Documents and Settings/gavin.kistner/Desktop/
    25139/AMPlayer.exe'
    }

    file_base = 'D:/QA/TestProjects/v30/Performance/
    MasterSlideAssetWithManySlides/'
    files = %w| 1x1_10Assets.am 100x1_10Assets.am 1x100_10Assets.am
    100x100_10Assets.am |

    filepaths = {}
    files.each{ |filename|
    filepaths[ filename ] = (file_base + filename)
    }

    stats = {}
    samples_per_launch = 5
    seconds_after_launch = 4
    seconds_after_terminate = 1

    players.each{ |player_name, exe|
    filepaths.each{ |filename, path|
    cmd = %Q|#{exe} "#{path}"|

    # Different launches use slightly different amounts of RAM
    # so launch it a few times and average the samples
    stats[ [player_name, filename ] ] =
    samples_per_launch.sample_average{

    # Run the command (open the file with the player)
    asynchronously
    IO.popen( cmd )

    # Wait to be sure it launched and initialized
    sleep seconds_after_launch

    # Find the process for the player
    mgmt = WIN32OLE.connect('winmgmts:\\\\.')
    player = mgmt.InstancesOf("win32_process").to_a.find{ |
    proc| proc.name =~ /AMPlayer.exe/ }

    # Figure out how much RAM it's using
    kbytes = player.workingSetSize.to_f / 1024

    # Kill it
    player.Terminate

    # Make sure it's really dead (not needed?)
    sleep seconds_after_terminate

    puts "#{player_name}:#{filename} :: #{kbytes}"

    # Return the measured RAM, for averaging
    kbytes
    }
    }
    }


    [1] http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_process.asp


    --
    "Despite the surge of power you feel upon learning Ruby,
    resist the urge to trip others or slap them in the bald head.
    DO NOT LORD YOUR RUBYNESS OVER OTHERS!"
    - Why the Lucky Stiff


    --Apple-Mail-1-645804600--
     
    Gavin Kistner, May 18, 2005
    #13
  14. --Apple-Mail-2-646401628
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    delsp=yes;
    format=flowed

    On May 17, 2005, at 10:53 PM, Gavin Kistner wrote:
    > (Suggestions on cleaner ways to do things are welcome; I really
    > don't understand OLE at all, or the WMI service. Although I did
    > find this list[1] of properties which you can access on the
    > processes returned by the InstancesOf call.)


    Ooh, and I just found this url[2] which links to some tasty scripts
    showing how to use WMI to automate all sorts of tasks on Windows.
    (The examples are in VBScript, but I think that it should be
    relatively easy to directly port them to Ruby, given the code I
    already supplied. And possibly this page[3])

    [2] http://msdn.microsoft.com/library/en-us/wmisdk/wmi/
    wmi_tasks_for_scripts_and_applications.asp
    [3] http://msdn.microsoft.com/library/en-us/wmisdk/wmi/swbemservices.asp

    --Apple-Mail-2-646401628--
     
    Gavin Kistner, May 18, 2005
    #14
  15. Pit Capitain Guest

    Bill Guindon schrieb:
    > Anybody have any samples of ruby/au3 scripts that are a bit more in
    > depth than the wiki example?


    Hi Bill,

    I doubt that it is of any use to you, but here's a script I use when
    developing stored procedures for an Oracle database. There are no
    comments, so feel free to ask if you need more information.

    def main
    start_automation
    if toad_running?
    activate_toad
    switch_away_from_output if output_showing?
    execute_code
    if package_state_changed?
    remove_error_window
    switch_to_output
    restart_output
    switch_away_from_output
    execute_code
    end
    switch_to_output unless error_window?
    end
    end

    TOAD = "FREE TOAD"
    OUT = "DBMS Output"
    ERR = "TOAD Error"

    def start_automation
    require "win32ole"
    @ai = WIN32OLE.new( "AutoItX3.Control" )
    end

    def toad_running?
    @ai.WinExists( TOAD ) > 0
    end

    def activate_toad
    @ai.WinActivate( TOAD )
    @ai.WinWaitActive( TOAD )
    end

    def output_showing?
    window_title =~ /#{OUT}/
    end

    def switch_away_from_output
    switch_with( "^{TAB}" )
    end

    def switch_to_output
    switch_with( "^+{TAB}" )
    end

    def restart_output
    switch_with( "^{F4}" )
    switch_with( "!DD" )
    end

    def execute_code
    @ai.Send( "^{ENTER}" )
    sleep 0.2 while @ai.MouseGetCursor == 0
    end

    def package_state_changed?
    sleep 0.2
    error_window? && window_text =~ /ORA-04068/
    end

    def error_window?
    window_title =~ /#{ERR}/
    end

    def remove_error_window
    switch_with( "{ESC}" )
    end

    def switch_with( cmd )
    title = window_title
    @ai.Send( cmd )
    sleep 0.2 while title == window_title
    end

    def window_title
    @ai.WinGetTitle( "" )
    end

    def window_text
    @ai.WinGetText( "" )
    end

    main


    Regards,
    Pit
     
    Pit Capitain, May 18, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D. Lane
    Replies:
    0
    Views:
    788
    D. Lane
    Jun 27, 2003
  2. Replies:
    0
    Views:
    439
  3. Replies:
    0
    Views:
    586
  4. apondu
    Replies:
    0
    Views:
    630
    apondu
    Jul 19, 2007
  5. Eduardo Schettino

    [ANN] doit - automation tool 0.8 released

    Eduardo Schettino, May 17, 2010, in forum: Python
    Replies:
    0
    Views:
    275
    Eduardo Schettino
    May 17, 2010
Loading...

Share This Page