Monday, June 15, 2015

AutoHotkey - Improved Script

Here's an automated GUI test script using objects, coordinates, values, for each loop and if condition.

Numpad1 & Numpad2::

IfWinExist TradingApp
{

;;Activate window
WinActivate

;Create coordinates data
CoList :={}
CoListCount = 6

Loop, % CoListCount 
   CoList[A_Index] := {} 

CoList.1 := {X: "268", Y: "189", Val: ""}
CoList.2 := {X: "255", Y: "111", Val: ""}
CoList.3 := {X: "441", Y: "155", Val: ""}
CoList.4 := {X: "146", Y: "396", Val: "Risky Accounts"}
CoList.5 := {X: "500", Y: "249", Val: "27029445{Enter}"}
CoList.6 := {X: "500", Y: "249", Val: "27029000-28029000{Enter}"}


for each, co in CoList 


    Sleep 1000

    val := co.Val
    MouseClick,Left,co.X, co.Y
    if(val)
    {
      Send, %val%
    }
}
}

Return

Esc::ExitApp
Return

AutoHotkey - Some simple tips.


Say you want to perform series of sequential clicks. The best way to do this with repetition is to store all the co-ordinates in an object, then use a for loop to perform those clicks.

The example below, moves the mouse pointer to the co-ordinates I defined in the Point object (you ca n name it anything you wish).

Point :=   Object(138,168,485,690,733,240)

For x, y in Point
{
  Sleep 2000 ;its too fast
  MouseMove, %x%, %y%
}

MsgBox, The script ended...thanks.

I think the code is self explainatory.

Using objects and properties.

ProfileList := {}
 ProfileCount = 2 

 Loop, % ProfileCount 
   ProfileList[A_Index] := {} 

 ProfileList.1.Name := "MyName"
 ProfileList.1.Password := "MyPassword"
 ProfileList.1.Server := "TheServer" 

 ProfileList.2.Name := "AnotherName" 
ProfileList.2.Password := "AnotherPassword" 
ProfileList.2.Server := "AnotherServer" 

 for each, item in ProfileList 
    MsgBox, % "Name: " item.name "`nPassword: " item.Password "`nServer: " item.server
 }

Sunday, June 14, 2015

AutoHotkey - Magical GUI testing

I've been smitten by AutoHotkey, which is an open source scripting framework which allows you to define to automate your inputs and mouse clicks simply assigning them to some key strokes (it's actually much more than that, but enough introduction)

Here's what i wish to do. I'll simply open up my app. Then I want to type inputs in all of my text boxes and finally click the button.

I'll write the auto hot key script for this with the input values and its done.

Here's what my app looks like:

Here's my .ahk script (explanations later)

Numpad0 & Numpad2:: ;starts when these 2 are pressed

IfWinExist Trading Application
{
    WinActivate

    ;Enter for MF symbol
Sleep 2000 ;sleep for 2 seconds
Click, 81, 98 ;now clicks
Send, MFC02

;Action
Sleep 2000 ;sleep for 2 seconds
Click, 214, 96 ;now clicks
Send, BUY

;Amount
Sleep 2000 ;sleep for 2 seconds
Click, 79, 151 ;now clicks
Send, 5000

;Trade Date
Sleep 2000 ;sleep for 2 seconds
Click, 239, 150 ;now clicks
Send, 13/06/2015

;Verify Trade
Sleep 2000 ;sleep for 2 seconds
Click, 257, 199 ;now clicks
}

return ;end

Escape::
ExitApp
Return

When you run the script and press Numpad 0 followed by 2, the Trading Application window (if open in background) shows up in the front (WinActivate command). Then after 2 seconds (Sleep command), the mouse pointer moves and clicks (using Click command with location) at the MF symbol textbox, and "MFC02" is typed (Send command). This happens for all other textboxes. Then the button is clicked and a message box appears (from the program).

I got all the co-ordinates using the AutoIt3 window spy (which gets installed when you install AutoHotkey). I used the 'Default' mouse co-ordinates.



Return command signals the end of the program. On pressing Esc key the app will exit (it's useful if something nasty happens because of your script)

So, i think you must have got the hang of it more or less. There is certainly a great way to improve this example (eg. use variables for data and read the values from a textfile in a loop). Just try it out and have fun!