is there a switch statements in Robot?

6,395 views
Skip to first unread message

ElizaLeong

unread,
Apr 23, 2012, 5:37:07 PM4/23/12
to robotframe...@googlegroups.com
Hi,

I used the Collections library to Create Dictionary (a hashtable of key-value pairs).
I'd like to unpack my hashtable and use a switch-statement construct if RF provides such a construct.
Is there something like that?

So, for example, I would like to do this (pseudo code here).
#unpack dictionary
switch key
  'a'  Run Keyword A-test
  'b'  Run Keyword B-test
  'c'  Run Keywrod C-test
  'default' Run Keyword default-test

Right now I'm using something like the following.  The script unnecessarily checks the key against each possible value in sequential order. 
Even if I add e a Exit For Loop after a match, there are some unnecessary steps.

#unpack dictionary
#for each key
  Run Keyword If   ${key}=='a'  A-test
  Run Keyword If   ${key}=='b'  B-test
  Run Keyword If   ${key}=='c'  C-test

Pekka Klärck

unread,
Apr 25, 2012, 8:23:54 AM4/25/12
to elc....@gmail.com, robotframe...@googlegroups.com
> Hi,
>
> I used the Collections library to Create Dictionary (a hashtable of
> key-value pairs).
> I'd like to unpack my hashtable and use a switch-statement construct if RF
> provides such a construct.
> Is there something like that?

No, there are no switch statements in Robot syntax. When you need
complex logic like that, it is often a good idea to move the logic
into a test library where you can use real programming code.

> Right now I'm using something like the following.  The script unnecessarily
> checks the key against each possible value in sequential order.
> Even if I add e a Exit For Loop after a match, there are some unnecessary
> steps.
>
> #unpack dictionary
> #for each key
>   Run Keyword If   ${key}=='a'  A-test
>   Run Keyword If   ${key}=='b'  B-test
>   Run Keyword If   ${key}=='c'  C-test

Instead of the above, you could possibly use this:

#for each key
  Run Keyword ${key}-test


Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Reply all
Reply to author
Forward
0 new messages