FOR loop in robot framework, how to exit if conditions are met

16,434 views
Skip to first unread message

Drewcasket

unread,
Nov 1, 2012, 3:57:27 PM11/1/12
to robotframe...@googlegroups.com
I wrote a for loop within robot framework, I need it to execute until certain conditions are met.  I can't find a built in key word that does this efficiently.  Any help would be appreciated.
 
:FOR | ${index} | IN RANGE | 100 
      VR button pressed 
      sleep | 1 sec  
      ${micState}= | Devaluate | signals[UISpeechService][micState][value][mode] 
      ${VoiceRecognition}= | Devaluate | signals[UISpeechService][voiceRecognition][value][state] 
      ${DialogActive}= | Devaluate | signals[UISpeechService][vrDialogActive][value][state] 
      sleep | 1 sec  
      should be equal | ${micState}  | "open"
      should be equal | ${VoiceRecognition} | "on"
      should be equal | ${DialogActive} | true
 
so basically this presses the VR button, evaluates signals from another program, then compares those values to what they should be.
 
This will continue until the values compared are all equal. 
 
The problem that is occuring is that if at any point the values are not equal it fails the test and ends the testcase. Or if they are all true after the first iteration, it will continue to do the loop 100 times.
 
How do I get it to exit the loop as soon as the three conditions are equal? While also continuing the loop if they are not equal.
 
Thank you

GAURAV DEORE

unread,
Nov 1, 2012, 10:52:49 PM11/1/12
to drewca...@gmail.com, robotframe...@googlegroups.com
Hi,

 You use simple keyword like  "Log    Exit For Loop" exact below the For keyword aligned.

In ur case like :-

:FOR | ${index} | IN RANGE | 100 
      VR button pressed 
      sleep | 1 sec  
      ${micState}= | Devaluate | signals[UISpeechService][micState][value][mode] 
      ${VoiceRecognition}= | Devaluate | signals[UISpeechService][voiceRecognition][value][state] 
      ${DialogActive}= | Devaluate | signals[UISpeechService][vrDialogActive][value][state] 
      sleep | 1 sec  
      should be equal | ${micState}  | "open"
      should be equal | ${VoiceRecognition} | "on"
      should be equal | ${DialogActive} | true
Log   Exit For Loop

---
Regards,
Gaurav

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/robotframework-users/-/UIQAP6vUDQUJ.
To post to this group, send email to robotframe...@googlegroups.com.
To unsubscribe from this group, send email to robotframework-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/robotframework-users?hl=en.



--
------------------------------
Warm Regards,
GAURAV S. DEORE.

Kevin O.

unread,
Nov 2, 2012, 9:11:56 AM11/2/12
to robotframe...@googlegroups.com
I would do something like this:

:FOR | ${index} | IN RANGE | 100 
      VR button pressed 
      sleep | 1 sec  
      ${micState}= | Devaluate | signals[UISpeechService][micState][value][mode] 
      ${VoiceRecognition}= | Devaluate | signals[UISpeechService][voiceRecognition][value][state] 
      ${DialogActive}= | Devaluate | signals[UISpeechService][vrDialogActive][value][state] 
      sleep | 1 sec  
      Run Keyword If | ("${micState}"=="open") and ("${VoiceRecognition}=="on") and ("${DialogActive}"=="true") | Exit For Loop
Run Keyword If | ${index} == 99 | Fail | Conditions not met - micState:${micState} VoiceRecognition:${VoiceRecognition} DialogActive:${DialogActive}

Note that the index in the index check must be one less than the range. This would only allow 99 iterations to meet condidtion not 100.
Checking the value of a loop counter outside of the loop is a no-no in some languages but I think its OK here.

Kevin

Kevin O.

unread,
Nov 2, 2012, 9:15:24 AM11/2/12
to robotframe...@googlegroups.com
If ${DialogActive} is a true boolean, then you should do
... and ${DialogActive}
  or
... and ("${DialogActive}"=="True")

Kevin O.

unread,
Nov 2, 2012, 9:19:31 AM11/2/12
to robotframe...@googlegroups.com
Actually I think I like this better:

:FOR | ${index} | IN RANGE | 100 
      VR button pressed 
      sleep | 1 sec  
      ${micState}= | Devaluate | signals[UISpeechService][micState][value][mode] 
      ${VoiceRecognition}= | Devaluate | signals[UISpeechService][voiceRecognition][value][state] 
      ${DialogActive}= | Devaluate | signals[UISpeechService][vrDialogActive][value][state] 
      sleep | 1 sec  
      ${signals ok} = | Evaluate | ("${micState}"=="open") and ("${VoiceRecognition}=="on") and ("${DialogActive}"=="true")
      Run Keyword If | ${signals ok} | Exit For Loop
Run Keyword Unless | ${signals ok} | Fail | Conditions not met - micState:${micState} VoiceRecognition:${VoiceRecognition} DialogActive:${DialogActive}

Drewcasket

unread,
Nov 2, 2012, 9:24:19 AM11/2/12
to robotframe...@googlegroups.com
Thanks for the help!! I was unaware that you could use the "and" "or" conjunctions within robot framework.  I suppose reading the included userguide would put a stop to my postings lol. Once again thanks for the help!!
 
Drew

Trey Duskin

unread,
Nov 2, 2012, 12:18:45 PM11/2/12
to robotframe...@googlegroups.com
You might also want to consider the Wait Until Keyword Succeeds (see Builtin Library) to handle this.  That would give you time-based control over how long to wait and how often to re-check, which could be more feasible for what you are testing.

Also, the 'and' and 'or' support comes from the Evaluate keyword, which simply uses the Python/Jython eval() function.  So, the support for those conjunctions comes from Python, not RF.

Drewcasket

unread,
Nov 2, 2012, 12:34:12 PM11/2/12
to robotframe...@googlegroups.com
I'm getting this error saying that true is undefined.
KEYWORD: ${micState} = OnTheDLib.D Evaluate signals[UISpeechService][micState][value][mode] Expand All
Documentation:

DEvaluate(parameter) takes a string parameter (e.g. signals[Media][nowPlaying][value][title]) and returns the looked-up value of that parameter.

Start / End / Elapsed: 20121102 12:22:02.870 / 20121102 12:22:03.073 / 00:00:00.203
12:22:03.073 INFO ${micState} = "open"
-
KEYWORD: ${VoiceRecognition} = OnTheDLib.D Evaluate signals[UISpeechService][voiceRecognition][value][state] Expand All
Documentation:

DEvaluate(parameter) takes a string parameter (e.g. signals[Media][nowPlaying][value][title]) and returns the looked-up value of that parameter.

Start / End / Elapsed: 20121102 12:22:03.073 / 20121102 12:22:03.073 / 00:00:00.000
12:22:03.073 INFO ${VoiceRecognition} = "on"
-
KEYWORD: ${DialogActive} = OnTheDLib.D Evaluate signals[UISpeechService][vrDialogActive][value][state] Expand All
Documentation:

DEvaluate(parameter) takes a string parameter (e.g. signals[Media][nowPlaying][value][title]) and returns the looked-up value of that parameter.

Start / End / Elapsed: 20121102 12:22:03.073 / 20121102 12:22:03.089 / 00:00:00.016
12:22:03.089 INFO ${DialogActive} = true
+
KEYWORD: BuiltIn.Sleep 1 sec Expand All
Documentation:

Pauses the test executed for the given time.

Start / End / Elapsed: 20121102 12:22:03.089 / 20121102 12:22:04.089 / 00:00:01.000
-
KEYWORD: ${signals ok} = BuiltIn.Evaluate (${micState}=="open") and (${VoiceRecognition}=="on") and ${DialogActive}==true Expand All
Documentation:

Evaluates the given expression in Python and returns the results.

Start / End / Elapsed: 20121102 12:22:04.089 / 20121102 12:22:04.104 / 00:00:00.015
12:22:04.104 FAIL Evaluating expression '("open"=="open") and ("on"=="on") and true==true' failed: NameError: name 'true' is not defined

 

Everything is evaluating correctly so ${signals ok} should come back as True, instead I get the above error.  What am I doing wrong?
 

On Thursday, November 1, 2012 3:57:27 PM UTC-4, Drewcasket wrote:

Drewcasket

unread,
Nov 2, 2012, 12:58:09 PM11/2/12
to robotframe...@googlegroups.com
I got it figured out.  The program that I'm testing is returning a boolean value of true, but python needs to recognize it as True(with a capital T).  Now I have to incorporate some code to receive true and change it to True before checking the ${signals ok}.

On Thursday, November 1, 2012 3:57:27 PM UTC-4, Drewcasket wrote:

Trey Duskin

unread,
Nov 2, 2012, 2:40:07 PM11/2/12
to robotframe...@googlegroups.com
I don't think you need to incorporate code, you just need to properly quote the RF variables in your Evaluate keyword call.  E.g:

Evaluate ("${micState}"=="open") and ("${VoiceRecognition}"=="on") and "${DialogActive}"=="true"

That way python will just compare the strings.  The quotes do not escape RF variables.

Kevin O.

unread,
Nov 2, 2012, 3:29:53 PM11/2/12
to robotframe...@googlegroups.com
Or like I said in my earlier post, you can do (this is a possibility not a recommendation)
Evaluate ("${micState}"=="open") and ("${VoiceRecognition}"=="on") and ${DialogActive}

Boolean values in RF are a bit problematic and the core developers have recommended avoiding them when possible.
FYI there is a keyword Convert To Boolean in BuiltIn that was probably created for the specific problem of handling True and true and TRUE.
Glad you got it working.

Kevin

Edison Quisiguina

unread,
Jan 15, 2016, 3:28:33 PM1/15/16
to robotframework-users, drewca...@gmail.com
Thanks Gaurav Deore,  'Exit For Loop' keyword works for me :)
Gaurav
To unsubscribe from this group, send email to robotframework-users+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/robotframework-users?hl=en.
Reply all
Reply to author
Forward
0 new messages