ruby in WinXP as an automation tool

R

ruby talk

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
 
B

Bill Guindon

=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)
 
J

James Britt

Bill said:
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
 
B

Bill Guindon

=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)
 
J

James Britt

Alexey said:
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
 
J

James Britt

Alexey said:
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
 
J

James Britt

G

Gavin Kistner

--Apple-Mail-1-645804600
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

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--
 
G

Gavin Kistner

--Apple-Mail-2-646401628
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

(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--
 
P

Pit Capitain

Bill said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top