d-b
d-b

Reputation: 971

Robot Framework: exit if-clause but not keyword?

Some RF pseudo code:

Run keyword if  X == 1
   Run keyword if  Y == a  [do something]
   Run keyword if  Y == b  [do something]
   Run keyword if  Y == c  [do something]

If Y == b, is there a way to exit the if-clause without exiting the whole keyword?

I am aware of run keyword and return but it exits the whole keyword, I just want to exit the if-clause because it is unnecessary to check if Y is c, d, e and so on if the condition already has been true.

(N.B. This is a helper class that translates between two set of values. Also, there are way more than three "sub-ifs".)

Upvotes: 1

Views: 2915

Answers (2)

Temizzi
Temizzi

Reputation: 412

In ROBOT Framework, you cannot do a nested loop.. like we do in programming language.. So, the approach that we do in ROBOT scripts is like below.

  1. Use RUN KEYWORD IF ${X} == '1'
  2. If true, then call another/new USER_DEFINED_KEYWORD (custom keyword) which contains your conditional statements.

So, you must create YOUR_NEW_KEYWORD which contains your conditional statement first beforehand. Then if (X == 1) is true then we will call this newly created keyword. So the approach that you need can be like below.. Besides, you must use the EXIT FOR LOOP IF keyword if you want to exit a loop after a condition is fulfilled.

RUN KEYWORD IF     ${X} == '1'    YOUR_NEW_KEYWORD # *this user-defined keyword contains the code below..*

# this is the content of YOUR_NEW_KEYWORD which contains conditional statements..
:FOR ${check}  IN  TRUE
  Run keyword if  Y == a  [do something]
  EXIT FOR LOOP IF Y == a

  Run keyword if  Y == b  [do something]
  EXIT FOR LOOP IF Y == b

  Run keyword if  Y == c  [do something]
  EXIT FOR LOOP IF Y == c

In ROBOT Framework, we normally create many user-defined keywords (custom keywords) and then import this as resource file. In ROBOT Framework, we can import LIBRARY(s) and RESOURCE FILE (our own defined keywords). This is a good approach (code refactoring) to make your code neat and easily readible as we don't want to write everthing in the main test script, instead we will prefer to call the USER KEYWORDS which we have written as RESOURCE FILE.

Upvotes: 0

Bob
Bob

Reputation: 150

I think you can wrap your IF statement into another keyword like this:

Do Switch
  Run Keyword If  Y == a  [do something]
  ...    ELSE IF  Y == b  [do something]
  ...    ELSE IF  Y == c  [do something]

Run keyword if  X == 1
    Do Switch

Upvotes: 2

Related Questions