Sign in to follow this  
Followers 0
agreiner

For...In Loop Help needed

7 posts in this topic

For an automated software testing suite I have, I am reading test steps from an XML file. I have a For...In loop which works well, executing each step sequentially. I am trying to add looping to my program.

What I have:

For $oStep In $oSteps
$i = $i +1
ConsoleWrite($i & @TAB)
;ConsoleWrite("5" & @CRLF)
$action = _NodeGetValue($oStep.attributes.getNamedItem("action"))
;ConsoleWrite($action & @TAB)
$exitonerror = _NodeGetValue($oStep.attributes.getNamedItem("exitonerror"))
;ConsoleWrite($exitonerror & @TAB)
;MsgBox(0,"Function",$action)
$check = Call($action,"True")
;ConsoleWrite($check & @CRLF)
If $check < 1 AND $exitonerror == "true" Then
  ExitOnError($i,$check)
EndIf
Next

Example XML:

<steps>
  <step action="BrowserStartup" exitonerror="True"/>
  <step action="Pause" millis="2500" exitonerror="false" />
  <step action="CheckUsername" expected="admin" exitonerror="True" />
  <step action="ClickMainMenu" menuitem="Logout" exitonerror="True"/>
<step action="CloseBrowser" exitonerror="True"/>
</steps>

What I would like to do is add 2 actions: LoopStart and LoopStop. I would then record the line number (in the xml file) of "LoopStart". When "LoopStop" is reached, I would jump back to "LoopStart" and repeat until some specified condition is met. The part I am having trouble with is how do I go back to a specific line using a For...In loop? Do I have to re-write the loop to use $i, making it:

For $i = 1 To <Number of Steps in XML File>
...
Next

and then resetting $i to the line containing LoopStart (or the line after, more specificly)?

Share this post


Link to post
Share on other sites



You have not embraced the Tao of XML. Just add a loop element:

Test1.xml:

<?xml version="1.0"?>
<steps>
<step action="BrowserStartup" exitonerror="True"/>
<loop count="2">
  <step action="Pause" millis="2500" exitonerror="false" />
  <loop count="3">
   <step action="CheckUsername" expected="admin" exitonerror="True" />
  </loop>
  <step action="ClickMainMenu" menuitem="Logout" exitonerror="True"/>
</loop>
<step action="CloseBrowser" exitonerror="True"/>
</steps>

Test1.au3:

#include <_XMLDomWrapper.au3>
#include <Array.au3>
Global $iRET, $iNodeCnt, $sXPath = "/steps"
$iRET = _XMLFileOpen(@ScriptDir & "Test1.xml")
$aNodes = _XMLSelectNodes($sXPath & "/*")
If IsArray($aNodes) Then
For $n = 1 To $aNodes[0]
  _RunStep($sXPath & "/*[" & $n & "]", $aNodes[$n])
Next
Else
Exit
EndIf
Func _RunStep($sStepPath, $sStep)
Local $aSteps, $iLoopCnt
Switch $sStep
  Case "step"
   $sAction = _XMLGetAttrib($sStepPath, "action")
   ConsoleWrite("Debug: $sAction = " & $sAction & @LF)
  Case "loop"
   $aSteps = _XMLSelectNodes($sStepPath & "/*")
   If IsArray($aSteps) Then
    $iLoopCnt = Number(_XMLGetAttrib($sStepPath, "count"))
    If $iLoopCnt > 0 Then
     For $iLoop = 1 To $iLoopCnt
      For $s = 1 To $aSteps[0]
       _RunStep($sStepPath & "/*[" & $s & "]", $aSteps[$s])
      Next
     Next
    EndIf
   EndIf
  Case Else
   ConsoleWrite("Debug:  Unhandled case: $sStepPath = " & $sStepPath & "; $sStep = " & $sStep & @LF)
EndSwitch
EndFunc   ;==>_RunStep

Now go sit in the corner and meditate on the sound of one hand clapping...

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

You have not embraced the Tao of XML. Just add a loop element:...

Now go sit in the corner and meditate on the sound of one hand clapping... :)

Holy crap! It took me a few hours of meditating, but I think I got it! You are the master!

Previously I had created an Object from the XML file directly, but using the XML DOM wrapper is much better. Now, to modify all of my old code to use this. Joy.

Share this post


Link to post
Share on other sites

OK, Now I have another odd issue. This did not occur before the use of _XMLDomWrapper.au3.

When using _IECreate (from ie.au3) I am getting an error. Searching is not turning up any helpful results. Error:

COM Error with DOM!

err.description is:

err.windescription is: Unspecified error

err.number is: 80020009

err.lastdllerror is: 0

err.scriptline is: 3677

err.source is:

err.helpfile is:

err.helpcontext is: 0

Also, in the output console of Scite, I get:

IE.au3 V2.4-0 Warningfrom function internal function __IEIsObjType, Cannot register internal error handler, cannot trap COM errors (Use _IEErrorHandlerRegister() to register a user error handler)

IE.au3 V2.4-0 Warningfrom function internal function __IEIsObjType, Cannot register internal error handler, cannot trap COM errors (Use _IEErrorHandlerRegister() to register a user error handler)

IE.au3 V2.4-0 Warning from function _IELoadWait, Cannot register internal error handler, cannot trap COM errors (Use _IEErrorHandlerRegister() to register a user error handler)

Is this due to having conflicting COM error handlers? I have never seen anything like this before.

Share this post


Link to post
Share on other sites

Well, if you open the _XMLDomWrapper.au3 UDF, it is of course the same COM methods, just put in convenient wrappers.

That UDF is old though, and hasn't been updated since the COM Error handling was changed in version 3.3.7.19.

Use _IEErrorHandlerRegister() to apply Dale's handler before you call any _IE* functions.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

_IEErrorHandlerRegister() with custom function, so inside it you will seperate error from domwrapper and from Dale's one.In this function, don't miss that :

"Important: When using your own error handler, the Error Object variable used MUST be named $oIEErrorHandler (see example).

see autoit help file for more information. Com event is not so simple when dealing with multiple com objects. You have to handle each error, and play with global error variable ( see above )


-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

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  
Followers 0