Jump to content

Automated testing


hsteeman
 Share

Go to solution Solved by hsteeman,

Recommended Posts

Hello,

I try to use AutoIT for automated testing.

My first test is a recording of starting the Windows calc.exe, enter 2 * 3 and then I want to test the result. Of course it should be 6, but the result I get is 0, although calc,exe displays the correct result.

This is my first code:

#include <MsgBoxConstants.au3>
#region --- Au3Recorder generated code Start (v3.3.9.5 KeyboardLayout=00020409)  ---
 
#region --- Internal functions Au3Recorder Start ---
Func _Au3RecordSetup()
Opt('WinWaitDelay',100)
Opt('WinDetectHiddenText',1)
Opt('MouseCoordMode',0)
Local $aResult = DllCall('User32.dll', 'int', 'GetKeyboardLayoutNameW', 'wstr', '')
If $aResult[1] <> '00020409' Then
  MsgBox(64, 'Warning', 'Recording has been done under a different Keyboard layout' & @CRLF & '(00020409->' & $aResult[1] & ')')
EndIf
 
EndFunc
 
Func _WinWaitActivate($title,$text,$timeout=0)
WinWait($title,$text,$timeout)
If Not WinActive($title,$text) Then WinActivate($title,$text)
WinWaitActive($title,$text,$timeout)
EndFunc
 
_AU3RecordSetup()
#endregion --- Internal functions Au3Recorder End ---
 
_WinWaitActivate("Menu Start","")
MouseClick("left",42,500,1)
MouseClick("left",132,445,1)
Send("calc.exe{ENTER}")
_WinWaitActivate("Rekenmachine","")
Send("3*2{ENTER}")
#endregion --- Au3Recorder generated code End ---
WinWaitActive("[CLASS:'CalcFrame']", "", 1)
Local $sText = GUICtrlRead("[CLASS:'CalcFrame'];INSTANCE:1]")
MsgBox($MB_SYSTEMMODAL, "", "The result is: " & $sText)
 
What have I done wrong?
 
Link to comment
Share on other sites

You got the class of the frame and not the actual inside of the calculation window. Try this..

Local $sText = GUICtrlRead("[CLASS:#32770]")

Give that a try and tell me if I was wrong or not :)

Included is a screenshot of me doing this..

post-86910-0-27555500-1410800319_thumb.p

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Your example based on '?do=embed' frameborder='0' data-embedContent>>

;~ *** Standard code ***
#include "UIAWrappers.au3"
AutoItSetOption("MustDeclareVars", 1)

#cs
; Write below to uia.cfg file
; This is an inifile for UIA wrappers having the configuration defaults
; Debug=true        Turn debugging on of off with true/false value
; Highlight=true    Turn Highlighting rectangle to true / false
; TODO: AutoStartSUT=true AutoStartSUT is starting all SUT's automatically

[Global]
Debug=true
Highlight=true
AutoStartSUT=true

[Multiuser]
CFGPerUser=false

[Folders]
subfolders=false

;System under test settings
; Folder      = Location where exe can be found
; Workingdir  = Location of the working directory
; exe         = Name of the exe to start
; Fullname    = Path & name of exe
; Windowstate = minimized, maximized, normal


[SUT1]
Folder=%Windowsdir%\system32
Workingdir=%Windowsdir%\system32
exe=calc.exe
Fullname=%Windowsdir%\system32\calc.exe
Parameters=
Processname=calc.exe
Windowstate=normal

[SUT2]
Folder=%Windowsdir%\system32
Workingdir=%Windowsdir%\system32
exe=notepad.exe
Fullname=%Windowsdir%\system32\notepad.exe
Parameters=
Processname=notepad.exe
Windowstate=normal
#CE


;~ Start the system under test applications
;~ _UIA_StartSUT("SUT1") ;~Calculator
;~ _UIA_StartSUT("SUT2") ;~Notepad

shellexecute("calc.exe")
shellexecute("notepad.exe")

;~ Low level setting of references
_UIA_SETVAR("calc.mainwindow","Title:=((Rekenmachine.*)|(calc.*));controltype:=UIA_WindowControlTypeId;class:=CalcFrame")
_UIA_SETVAR("btn1","title:=1;classname:=Button")
_UIA_SETVAR("btn2","title:=1;classname:=Button")
_UIA_SETVAR("btn3","title:=1;classname:=Button")
_UIA_SETVAR("btn4","title:=1;classname:=Button")
_UIA_SETVAR("btn5","title:=1;classname:=Button")
_UIA_SETVAR("btn6","title:=1;classname:=Button")
_UIA_SETVAR("btn7","title:=1;classname:=Button")
_UIA_SETVAR("btn8","title:=1;classname:=Button")
_UIA_SETVAR("btn9","title:=1;classname:=Button")

_UIA_SETVAR("btn+","automationid:=93;classname:=Button")
_UIA_SETVAR("btn=","automationid:=121;classname:=Button")

_UIA_SETVAR("mnuEdit","title:=((Edit)|(Bewerken));ControlType:=MenuItem")
_UIA_SETVAR("mnuCopy","title:=((Copy.*)|(Kopi.*)); controltype:=MenuItem")

;~ Alternative way of setting the predefined references
;- They are all prefixed with the reference Notepad. when loaded
local $UID_NOTEPAD[4][2] = [ _
["mainwindow","classname:=Notepad"], _
["title","controltype:=50037"], _
["mnuEdit","name:=((Edit)|(Bewerken))"], _
["mnuPaste","name:=((Paste.*)|(Plak.*))"] _
]
_UIA_setVarsFromArray($UID_NOTEPAD,"Notepad.")

;- Above could also be stored in configuration files

;- Actual simple script
;- The bookkeeper has a calculator to make a calculation of 1 + 1 and he expects to see the answer 2 in notepad

_UIA_Action("calc.mainwindow","setfocus")
_UIA_Action("btn1","click")
_UIA_Action("btn+","click")
_UIA_Action("btn1","click")
_UIA_Action("btn=","click")

_UIA_Action("mnuEdit","click")
_UIA_Action("mnuCopy","click")

_UIA_Action("Notepad.mainwindow","setfocus")
_UIA_Action("Notepad.mnuEdit","click")

_uia_getvars2array()

_UIA_Action("Notepad.mnuPaste","click")
Link to comment
Share on other sites

 

Your example based on '?do=embed' frameborder='0' data-embedContent>>

;~ *** Standard code ***
#include "UIAWrappers.au3"
AutoItSetOption("MustDeclareVars", 1)

#cs
; Write below to uia.cfg file
; This is an inifile for UIA wrappers having the configuration defaults
; Debug=true        Turn debugging on of off with true/false value
; Highlight=true    Turn Highlighting rectangle to true / false
; TODO: AutoStartSUT=true AutoStartSUT is starting all SUT's automatically

[Global]
Debug=true
Highlight=true
AutoStartSUT=true

[Multiuser]
CFGPerUser=false

[Folders]
subfolders=false

;System under test settings
; Folder      = Location where exe can be found
; Workingdir  = Location of the working directory
; exe         = Name of the exe to start
; Fullname    = Path & name of exe
; Windowstate = minimized, maximized, normal


[SUT1]
Folder=%Windowsdir%\system32
Workingdir=%Windowsdir%\system32
exe=calc.exe
Fullname=%Windowsdir%\system32\calc.exe
Parameters=
Processname=calc.exe
Windowstate=normal

[SUT2]
Folder=%Windowsdir%\system32
Workingdir=%Windowsdir%\system32
exe=notepad.exe
Fullname=%Windowsdir%\system32\notepad.exe
Parameters=
Processname=notepad.exe
Windowstate=normal
#CE


;~ Start the system under test applications
;~ _UIA_StartSUT("SUT1") ;~Calculator
;~ _UIA_StartSUT("SUT2") ;~Notepad

shellexecute("calc.exe")
shellexecute("notepad.exe")

;~ Low level setting of references
_UIA_SETVAR("calc.mainwindow","Title:=((Rekenmachine.*)|(calc.*));controltype:=UIA_WindowControlTypeId;class:=CalcFrame")
_UIA_SETVAR("btn1","title:=1;classname:=Button")
_UIA_SETVAR("btn2","title:=1;classname:=Button")
_UIA_SETVAR("btn3","title:=1;classname:=Button")
_UIA_SETVAR("btn4","title:=1;classname:=Button")
_UIA_SETVAR("btn5","title:=1;classname:=Button")
_UIA_SETVAR("btn6","title:=1;classname:=Button")
_UIA_SETVAR("btn7","title:=1;classname:=Button")
_UIA_SETVAR("btn8","title:=1;classname:=Button")
_UIA_SETVAR("btn9","title:=1;classname:=Button")

_UIA_SETVAR("btn+","automationid:=93;classname:=Button")
_UIA_SETVAR("btn=","automationid:=121;classname:=Button")

_UIA_SETVAR("mnuEdit","title:=((Edit)|(Bewerken));ControlType:=MenuItem")
_UIA_SETVAR("mnuCopy","title:=((Copy.*)|(Kopi.*)); controltype:=MenuItem")

;~ Alternative way of setting the predefined references
;- They are all prefixed with the reference Notepad. when loaded
local $UID_NOTEPAD[4][2] = [ _
["mainwindow","classname:=Notepad"], _
["title","controltype:=50037"], _
["mnuEdit","name:=((Edit)|(Bewerken))"], _
["mnuPaste","name:=((Paste.*)|(Plak.*))"] _
]
_UIA_setVarsFromArray($UID_NOTEPAD,"Notepad.")

;- Above could also be stored in configuration files

;- Actual simple script
;- The bookkeeper has a calculator to make a calculation of 1 + 1 and he expects to see the answer 2 in notepad

_UIA_Action("calc.mainwindow","setfocus")
_UIA_Action("btn1","click")
_UIA_Action("btn+","click")
_UIA_Action("btn1","click")
_UIA_Action("btn=","click")

_UIA_Action("mnuEdit","click")
_UIA_Action("mnuCopy","click")

_UIA_Action("Notepad.mainwindow","setfocus")
_UIA_Action("Notepad.mnuEdit","click")

_uia_getvars2array()

_UIA_Action("Notepad.mnuPaste","click")

 

Nice!

How to disable red marking?

How to make it faster? (took 6 seconds)

How to get same result without moving a mouse cursor?

Link to comment
Share on other sites

Read uia.cfg for turning off highlight or debugging which slows down. Use invoke instead of click.

;~  Some settings to use as a default
_UIA_setVar("Global.Debug", True)
_UIA_setVar("Global.Debug.File", True)
_UIA_setVar("Global.Highlight", True)
Edited by junkew
Link to comment
Share on other sites

I tried these:
 
; get the result of the calculation
; wait a second to get the result
WinWaitActive("[CLASS:'CalcFrame']", "", 1)
; test 1
Local $sText = GUICtrlRead("[CLASS:CalcFrame; INSTANCE:1]")
MsgBox($MB_SYSTEMMODAL, "", "The result #1 is: " & $sText)
; test 2
Local $sText = GUICtrlRead("[CLASS:'CalcFrame'; INSTANCE:1]")
MsgBox($MB_SYSTEMMODAL, "", "The result #2 is: " & $sText)
; test 3
Local $sText = GUICtrlRead("[CLASS:#32770; INSTANCE:1]")
MsgBox($MB_SYSTEMMODAL, "", "The result #3 is: " & $sText)
; test 4
Local $sText = GUICtrlRead("[CLASS:#327701; INSTANCE:1]")
MsgBox($MB_SYSTEMMODAL, "", "The result #4 is: " & $sText)
; test 5
Local $sText = GUICtrlRead("[CLASS:#32770]")
MsgBox($MB_SYSTEMMODAL, "", "The result #5 is: " & $sText)
; test 6
Local $sText = GUICtrlRead("[CLASS:#327701]")
MsgBox($MB_SYSTEMMODAL, "", "The result #6 is: " & $sText)

Every test returns 0.

Link to comment
Share on other sites

Class of #32770 means it's like a sub window, with it's own controls.

You can get the handle to that control, and input it into my signature to get all the controls within it...your control is 150:

Func=[GetAllWindowsControls]: ControlCounter=[  1] ControlID=[  404] Handle=[0x000A1016] ClassNN=[            Static1] XPos=[  15] YPos=[  13] Width=[ 182] Height=[  13] IsVisible=[1] Text=[].
Func=[GetAllWindowsControls]: ControlCounter=[  2] ControlID=[  401] Handle=[0x0006101A] ClassNN=[            Static2] XPos=[  11] YPos=[  35] Width=[  14] Height=[  18] IsVisible=[1] Text=[].
Func=[GetAllWindowsControls]: ControlCounter=[  3] ControlID=[  150] Handle=[0x001D0FAC] ClassNN=[            Static3] XPos=[  29] YPos=[  25] Width=[ 167] Height=[  28] IsVisible=[1] Text=[0].
Func=[GetAllWindowsControls]: ControlCounter=[  4] ControlID=[  158] Handle=[0x000D06F0] ClassNN=[            Static4] XPos=[  29] YPos=[  28] Width=[ 167] Height=[  30] IsVisible=[1] Text=[].

Or, you can use the actual window's handle, and output all the controls...(not going to paste all that in)...so you would do this to get the text:

ConsoleWrite(ControlGetText("Calculator","",150) & @CRLF)

Use the ControlID to grab the data.  It's reliable.

;~ Click 3 * 9 =
WinActivate("Calculator")
ControlFocus("Calculator","",133)
ControlClick("Calculator","",133)

ControlFocus("Calculator","",92)
ControlClick("Calculator","",92)

ControlFocus("Calculator","",121)
; resists click, unless done twice
ControlClick("Calculator","",121,"left",2)

$string = ControlGetText("Calculator","",150)
If Number($string) = 27 Then
    MsgBox(1,1, $string & " matches 27")
Else
    MsgBox(1,1, $string & "does not match 27")
EndIf
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Solution

Thank you very much. The problem is solved now.

I use WinSpy++ to get the controlID. It displays a controlID with value 00000096, which is hexadecimal. The decimal value is 150.

I display the correct value via:

#include <MsgBoxConstants.au3>
#region --- Au3Recorder generated code Start (v3.3.9.5 KeyboardLayout=00020409)  ---

#region --- Internal functions Au3Recorder Start ---
Func _Au3RecordSetup()
Opt('WinWaitDelay',100)
Opt('WinDetectHiddenText',1)
Opt('MouseCoordMode',0)
Local $aResult = DllCall('User32.dll', 'int', 'GetKeyboardLayoutNameW', 'wstr', '')
If $aResult[1] <> '00020409' Then
  MsgBox(64, 'Warning', 'Recording has been done under a different Keyboard layout' & @CRLF & '(00020409->' & $aResult[1] & ')')
EndIf

EndFunc

Func _WinWaitActivate($title,$text,$timeout=0)
    WinWait($title,$text,$timeout)
    If Not WinActive($title,$text) Then WinActivate($title,$text)
    WinWaitActive($title,$text,$timeout)
EndFunc

_AU3RecordSetup()
#endregion --- Internal functions Au3Recorder End ---

_WinWaitActivate("AutoIT","")
Send("{LWINDOWN}{LWINUP}")
_WinWaitActivate("Menu Start","")
Send("calc.exe{ENTER}")
_WinWaitActivate("Rekenmachine","")
Send("2*3{ENTER}")
#endregion --- Au3Recorder generated code End ---
; Display result of calc
Local $result_calc = ControlGetText("Rekenmachine","",dec("96"))
MsgBox($MB_SYSTEMMODAL, "", "The result of calc is: " & $result_calc)
WinClose("Rekenmachine")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...