Jump to content
Sign in to follow this  
vaish

Need AutoIT Code help in one of my script

Recommended Posts

vaish

Hi All,

I have following AutoIT script for creating IMacros Object and delete.

I need to implement following following things in the code:

There are ObjCreate and ObjGet functions. In current implementation, we always create new instance by invoking ObjCreate. This will eventually consume all memory.

To change this, we will always try to perform ObjGet to bind to current open process of Imacros Browser using Auto IT.

Please do the needful and explain your logics.

<snip>

Edited by Melba23
Code removed as it is reposted below

Share this post


Link to post
Share on other sites
vaish

Func CheckArguments()

;This first check is standard and is required in every scripts

If isDeclared ("usage") Then

consoleWrite($usageMsg)

Exit

EndIf

;Your checks start here

;Checking device under test Ip

If not isDeclared("deviceIp") Then

Global $deviceIp = EnvGet("encIp")

Else

$nPreArgs = $nPreArgs + 1

EndIf

If eval("deviceIp" = "") Then

KConsoleWrite("deviceIp is empty.",1)

EndIf

;Checking imacrosFile

If Not IsDeclared("imacrosFile") Then

KConsoleWrite("Parameter not found: imacrosFile",1)

Else

$nPreArgs = $nPreArgs + 1

EndIf

If StringRegExp(eval("imacrosFile"),'',0) = 0 Then

Assign("imacrosFile", $kleverTestScriptsDir & "iMacros" & eval("imacrosFile"), 2)

EndIf

If Not FileExists(eval("imacrosFile")) Then

KConsoleWrite("Imacro Script: "&eval("imacrosFile")&" is not existed."&@CRLF,1)

EndIf

;Checking Extract Options

if Not IsDeclared("outputFile") Then

Assign("outputFile", @ScriptName & ".out", 2)

Else

$nPreArgs = $nPreArgs + 1

EndIf

if IsDeclared("Comparison") Then

$nPreArgs = $nPreArgs + 2

If not IsDeclared("Pattern") Then

KConsoleWrite("Comparison option is given without Pattern option",1)

EndIf

EndIf

if Not IsDeclared("isVisible") Then

Global $isVisible="No"

Else

$nPreArgs = $nPreArgs + 1

EndIf

if Not IsDeclared("checkWarningMsg") Then

Global $checkWarningMsg="No"

Else

$nPreArgs = $nPreArgs + 1

EndIf

EndFunc



Func main()



;Creating IMacros Object



Global $iMacros = ObjCreate("IMacros")

Global $iRet

If Not IsObj($iMacros) Then

KConsoleWrite("IMacro Object not created successfully")

KConsoleWrite("Closing Program", 1)

Else

if StringInStr($isVisible, "no") Then

$iRet = $iMacros.iimInit("-tray")

Else

$iRet = $iMacros.iimInit()

EndIf

$count = 0

While $iRet = -1

$count = $count + 1

KConsoleWrite("IMacro not initialized successfully. Retrying")

if StringInStr($isVisible, "no") Then

$iRet = $iMacros.iimInit("-tray")

Else

$iRet = $iMacros.iimInit()

EndIf

Sleep(2000)

If $count = 6 Then

KConsoleWrite("IMacro not initialized successfully.")

KConsoleWrite("Exiting with error code " & $iRet, 1)

ExitLoop

EndIf

WEnd

EndIf



;the two routines that you need to run



Global $iMacroTorun = Eval("imacrosFile")

Global $iMacroStatus

$iMacros.iimDisplay("Testing is in Progress...")

IMacrosSetArguments()

$iMacroStatus = IMacrosPlay($iMacroTorun)

IMacrosCheckError($iMacroStatus)

;Delete IMacros Object

$iMacros.iimDisplay("Testing is finished...")

;$iMacros.iimExit()

;Handle Comparision

if IsDeclared("Comparison") Then



;Read output file into variable



local $text=KFileReadToArray(eval("outputFile"))

if $text[0] = 0 Then

KConsoleWrite("Imacros Ext gave empty output",1)

EndIf

;stripping leading and trailing white spaces

local $mytext = StringStripWS($text[1], 3)

;stripping any double quote char

$mytext = StringReplace($mytext,'"',"")

Switch eval("Comparison")

Case "equal"

if $mytext = eval("Pattern") Then

KConsoleWrite("Equal passed: "&eval("Pattern"))

Else

KConsoleWrite("Equal failed: " & $mytext & ", while looking for " & eval("Pattern"))

$programExitCode=1

EndIf

Case "lessthan"

if int($mytext) <= int(eval("Pattern")) Then

KConsoleWrite("Less Than passed: " &$mytext & " is less than " & eval("Pattern"))

Else

KConsoleWrite("Less Than passed: " &$mytext & " is not less than " & eval("Pattern"))

$programExitCode=1

EndIf

Case "lessthanequal"

if int($mytext) < int(eval("Pattern")) Then

KConsoleWrite("Less Than passed: " &$mytext & " is less than or equal " & eval("Pattern"))

Else

KConsoleWrite("Less Than passed: " &$mytext & " is not less than or equal " & eval("Pattern"))

$programExitCode=1

EndIf

Case "greater"

if int($mytext) > int(eval("Pattern")) Then

KConsoleWrite("Less Than passed: " &$mytext & " is greater than " & eval("Pattern"))

Else

KConsoleWrite("Less Than passed: " &$mytext & " is not greater than " & eval("Pattern"))

$programExitCode=1

EndIf

Case "greaterequal"

if int($mytext) >= int(eval("Pattern")) Then

KConsoleWrite("Less Than passed: " &$mytext & " is greater than " & eval("Pattern"))

Else

KConsoleWrite("Less Than passed: " &$mytext & " is not greater than " & eval("Pattern"))

$programExitCode=1

EndIf

Case Else

KConsoleWrite("Invalid Compare option: "&eval("Compare"),1)

EndSwitch

EndIf

EndFunc





;Set all the IMacros Arguments.

;encIP is deviceIp

;we only need to provide other values

Func IMacrosSetArguments()

;We can set the encIp automatically from the envget("encIP")

$iMacros.iimSet("encIP", eval("deviceIp"))

;extraact dir and filename separately if outputfile is a fullpath

Global $workingDir = @WorkingDir

if (IsDeclared("outputFile") And StringInStr(Eval("outputFile"), "")) Then

$fileArray = StringSplit($outputFile, "")

if IsArray($fileArray) Then

$workingDir = ""

For $arrayIndex = 1 To $fileArray[0] - 1

$workingDirTemp = $fileArray[$arrayIndex]

$workingDir = $workingDir & $workingDirTemp & ""

Next

Global $outputFile = $fileArray[$fileArray[0]]

EndIf

EndIf

;make sure workingdir path is created

KDirCreate($workingDir)

$iMacros.iimSet("outputDir", $workingDir)

$iMacros.iimSet("outputFile", eval("outputFile"))

;extracting imacros arguments

If(isarray($CmdLine)) Then

For $arrayIndex = 1 to $CmdLine[0]

Local $linesplit = StringSplit($CmdLine[$arrayIndex], "=", 1)

If ($linesplit[1] = "imacrosFile") or ($linesplit[1] = "deviceIp") or ($linesplit[1] = "outputFile") or ($linesplit[1] = "isVisible") or ($linesplit[1] = "checkWarningMsg") or ($linesplit[1] = "logFile") or ($linesplit[1] = "imacrosFile") Then

;$iMacros.iimExit()

;KConsoleWrite("Arguments are entered in wrong order.", 1)

Else

$iMacros.iimSet($linesplit[1], $linesplit[2])

EndIf

Next

EndIf

EndFunc

func IMacrosPlay($iMacroTorun)

$iMacroTorun = Chr(34) & $iMacroTorun & Chr(34)

Local $iRet = $iMacros.iimPlay($iMacroTorun)

if StringInStr($checkWarningMsg,"yes") Then

$iMacros.iimPlayCode("TAG POS=1 TYPE=DIV ATTR=CLASS:warnbottomrule&&TXT:* EXTRACT=TXT")

$extractValue = $iMacros.iimGetLastExtract()

KConsoleWrite("The warning message is :" & $extractValue)

EndIf

Return $iRet

EndFunc

func IMacrosCheckError($error)

If $error > 0 Then

KConsoleWrite("IMacros: "&$imacrosFile& " ran successfully.", 0, 2)

Else

KConsoleWrite("IMacros: "&$imacrosFile& " errored: " & $iMacros.iimGetLastError(), 1)

EndIf

EndFunc

Share this post


Link to post
Share on other sites
vaish

I have posted code again with AutoIT feature for the referenece.

There are ObjCreate and ObjGet functions. we always create new instance by invoking ObjCreate. This will eventually consume all memory.

To change this, we will always try to perform ObjGet to bind to current open process of Imacros Browser using Auto IT.

Please do the needful and explain your logics.

Share this post


Link to post
Share on other sites
dany

Maybe something like this?

Global $iMacros = 0 ; Global object pointer.
;...
Func main()
    ; Only create an object when first initialized, otherwise re-use object pointer.
    If $iMacros = 0 Then
        $iMacros = ObjCreate("IMacros")
        If @error Then
            ; No object, error handling here.
        EndIf
    EndIf
    ; IMacros object is available now.
    ; ...
EndFunc

Alternately, if you don't need the object anymore, assign 0 to the variable to release it from memory.

Func main()
    $iMacros = 0 ; Delete object and free memory.
    $iMacros = ObjCreate("IMacros") ; Create a new obejct.
    ; ...
EndFunc

http://www.autoitscript.com/autoit3/docs/intro/ComRef.htm


[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Share this post


Link to post
Share on other sites
vaish

Hi dany,

Thank you very much.

I have modified the code according to the ur logic... just check the code and let me know... if any mistakes, correct me.

;This argument checks for the parameters which should not be a part of iimSet command.
Global $nPreArgs = 0

;This CheckArguments functions is where you determine if those parsed arguments satisfy your input and output requirements
CheckArguments()

;Program Start
ProgramStart()

;Your main program
main()

;Program End
ProgramEnd()


Func CheckArguments()
;This first check is standard and is required in every scripts
If isDeclared ("usage") Then
consoleWrite($usageMsg)
Exit
EndIf

;Your checks start here

;Checking device under test Ip
If not isDeclared("deviceIp") Then
Global $deviceIp = EnvGet("encIp")
Else
$nPreArgs = $nPreArgs + 1
EndIf
If eval("deviceIp" = "") Then
KConsoleWrite("deviceIp is empty.",1)
EndIf

;Checking imacrosFile
If Not IsDeclared("imacrosFile") Then
KConsoleWrite("Parameter not found: imacrosFile",1)
Else
$nPreArgs = $nPreArgs + 1
EndIf

If StringRegExp(eval("imacrosFile"),'',0) = 0 Then
Assign("imacrosFile", $kleverTestScriptsDir & "iMacros" & eval("imacrosFile"), 2)
EndIf

If Not FileExists(eval("imacrosFile")) Then
KConsoleWrite("Imacro Script: "&eval("imacrosFile")&" is not existed."&@CRLF,1)
EndIf

;Checking Extract Options
if Not IsDeclared("outputFile") Then
Assign("outputFile", @ScriptName & ".out", 2)
Else
$nPreArgs = $nPreArgs + 1
EndIf

if IsDeclared("Comparison") Then
$nPreArgs = $nPreArgs + 2
If not IsDeclared("Pattern") Then
KConsoleWrite("Comparison option is given without Pattern option",1)
EndIf
EndIf

if Not IsDeclared("isVisible") Then
Global $isVisible="No"
Else
$nPreArgs = $nPreArgs + 1
EndIf

if Not IsDeclared("checkWarningMsg") Then
Global $checkWarningMsg="No"
Else
$nPreArgs = $nPreArgs + 1
EndIf

EndFunc

;Modified code

; Global object pointer.

Global $iMacros = 0

Func main()
; Only create an object when first initialized, otherwise re-use object pointer.
If $iMacros = 0 Then
$iMacros = ObjCreate("IMacros")
Global $iRet
If Not IsObj($iMacros) Then
KConsoleWrite("IMacro Object not created successfully")
KConsoleWrite("Closing Program", 1)
Else
if StringInStr($isVisible, "no") Then
$iRet = $iMacros.iimInit("-tray")
Else
$iRet = $iMacros.iimInit()
EndIf
$count = 0
While $iRet = -1
$count = $count + 1
KConsoleWrite("IMacro not initialized successfully. Retrying")
if StringInStr($isVisible, "no") Then
$iRet = $iMacros.iimInit("-tray")
Else
$iRet = $iMacros.iimInit()
EndIf
Sleep(2000)
If $count = 6 Then
KConsoleWrite("IMacro not initialized successfully.")
KConsoleWrite("Exiting with error code " & $iRet, 1)
ExitLoop
EndIf
WEnd
EndIf

;the two routines that you need to run
Global $iMacroTorun = Eval("imacrosFile")
Global $iMacroStatus

$iMacros.iimDisplay("Testing is in Progress...")
IMacrosSetArguments()
$iMacroStatus = IMacrosPlay($iMacroTorun)
IMacrosCheckError($iMacroStatus)

;Delete IMacros Object
$iMacros.iimDisplay("Testing is finished...")
;$iMacros.iimExit()

;Handle Comparision
if IsDeclared("Comparison") Then
;Read output file into variable
local $text=KFileReadToArray(eval("outputFile"))
if $text[0] = 0 Then
KConsoleWrite("Imacros Ext gave empty output",1)
EndIf

;stripping leading and trailing white spaces
local $mytext = StringStripWS($text[1], 3)
;stripping any double quote char
$mytext = StringReplace($mytext,'"',"")

Switch eval("Comparison")
Case "equal"
if $mytext = eval("Pattern") Then
KConsoleWrite("Equal passed: "&eval("Pattern"))
Else
KConsoleWrite("Equal failed: " & $mytext & ", while looking for " & eval("Pattern"))
$programExitCode=1
EndIf
Case "lessthan"
if int($mytext) <= int(eval("Pattern")) Then
KConsoleWrite("Less Than passed: " &$mytext & " is less than " & eval("Pattern"))
Else
KConsoleWrite("Less Than passed: " &$mytext & " is not less than " & eval("Pattern"))
$programExitCode=1
EndIf
Case "lessthanequal"
if int($mytext) < int(eval("Pattern")) Then
KConsoleWrite("Less Than passed: " &$mytext & " is less than or equal " & eval("Pattern"))
Else
KConsoleWrite("Less Than passed: " &$mytext & " is not less than or equal " & eval("Pattern"))
$programExitCode=1
EndIf
Case "greater"
if int($mytext) > int(eval("Pattern")) Then
KConsoleWrite("Less Than passed: " &$mytext & " is greater than " & eval("Pattern"))
Else
KConsoleWrite("Less Than passed: " &$mytext & " is not greater than " & eval("Pattern"))
$programExitCode=1
EndIf
Case "greaterequal"
if int($mytext) >= int(eval("Pattern")) Then
KConsoleWrite("Less Than passed: " &$mytext & " is greater than " & eval("Pattern"))
Else
KConsoleWrite("Less Than passed: " &$mytext & " is not greater than " & eval("Pattern"))
$programExitCode=1
EndIf
Case Else
KConsoleWrite("Invalid Compare option: "&eval("Compare"),1)
EndSwitch
EndIf
EndFunc

;Set all the IMacros Arguments.
;encIP is deviceIp
;we only need to provide other values

Func IMacrosSetArguments()
;We can set the encIp automatically from the envget("encIP")
$iMacros.iimSet("encIP", eval("deviceIp"))

;extraact dir and filename separately if outputfile is a fullpath
Global $workingDir = @WorkingDir
if (IsDeclared("outputFile") And StringInStr(Eval("outputFile"), "")) Then
$fileArray = StringSplit($outputFile, "")
if IsArray($fileArray) Then
$workingDir = ""
For $arrayIndex = 1 To $fileArray[0] - 1
$workingDirTemp = $fileArray[$arrayIndex]
$workingDir = $workingDir & $workingDirTemp & ""
Next
Global $outputFile = $fileArray[$fileArray[0]]
EndIf
EndIf

;make sure workingdir path is created
KDirCreate($workingDir)

$iMacros.iimSet("outputDir", $workingDir)
$iMacros.iimSet("outputFile", eval("outputFile"))

;extracting imacros arguments
If(isarray($CmdLine)) Then
For $arrayIndex = 1 to $CmdLine[0]
Local $linesplit = StringSplit($CmdLine[$arrayIndex], "=", 1)
If ($linesplit[1] = "imacrosFile") or ($linesplit[1] = "deviceIp") or ($linesplit[1] = "outputFile") or ($linesplit[1] = "isVisible") or ($linesplit[1] = "checkWarningMsg") or ($linesplit[1] = "logFile") or ($linesplit[1] = "imacrosFile") Then
;$iMacros.iimExit()
;KConsoleWrite("Arguments are entered in wrong order.", 1)
Else
$iMacros.iimSet($linesplit[1], $linesplit[2])
EndIf
Next
EndIf
EndFunc

func IMacrosPlay($iMacroTorun)
$iMacroTorun = Chr(34) & $iMacroTorun & Chr(34)
Local $iRet = $iMacros.iimPlay($iMacroTorun)
if StringInStr($checkWarningMsg,"yes") Then
$iMacros.iimPlayCode("TAG POS=1 TYPE=DIV ATTR=CLASS:warnbottomrule&&TXT:* EXTRACT=TXT")
$extractValue = $iMacros.iimGetLastExtract()
KConsoleWrite("The warning message is :" & $extractValue)
EndIf
Return $iRet
EndFunc

func IMacrosCheckError($error)
If $error > 0 Then
KConsoleWrite("IMacros: "&$imacrosFile& " ran successfully.", 0, 2)
Else
KConsoleWrite("IMacros: "&$imacrosFile& " errored: " & $iMacros.iimGetLastError(), 1)
EndIf
EndFunc
  • Like 1

Share this post


Link to post
Share on other sites
dany

Well, I 'checked' your code and I'd say just try it. it looks good as far as I can tell. I'm missing lots of code (KConsoleWrite etc.) and I don't have IMacros software installed so I couldn't test anyway.

So just try it and see if it still gives you problems.


[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Share this post


Link to post
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
Sign in to follow this  

×