If Else-if in Robot Framework

28,939

Solution 1

Just add THREE DOTS (...) in first cell before ELSE IF keyword

${txt}    Set Variable
${txt}=    Run Keyword If    ${lenght} > 5    Some Keyword
...    ELSE IF    ${lenght} < 5    Some Keyword
...    ELSE    Some Keyword
Log       ${txt}

Solution 2

An alternate way to code this with Robot Framework's version of a switch statement is:

*** Variables ***    
String  ${text} =  ""

*** Keywords ***
${text} =  Set Variable If
...  ${variable} > 5  one
...  ${variable} < 5  two
...  ${variable} = 5  three

There may be other ways using Run Keyword If and Run Keyword Unless.

Share:
28,939
Natchaya TingTang
Author by

Natchaya TingTang

Updated on July 09, 2022

Comments

  • Natchaya TingTang
    Natchaya TingTang almost 2 years

    I want get value from a Keyword by using else if.

    Example:

    String text = ""  
    If variable > 5
       text = "one";
    else if variable <5
       text = "two";
    else
       text = "three";
    

    In Robot Framework

    I use the code

    ${txt}    Set Variable
    ${txt}=    Run Keyword If    ${length} > 5    Some Keyword
    \    ELSE IF    ${length} < 5    Some Keyword
    \    ELSE    Some Keyword
    Log       ${txt}
    

    ERROR !!!

    In Keyword ELSE IF  ;  Keyword name cannot be empty
    
  • Ds Arjun
    Ds Arjun over 5 years
    The Solution working for running keywords but when a variable is declared inside ELSE IF or ELSE then it is returning error. Ex: If a variable like ${a}= 1 is declared inside ELSE IF and on doing log to console ${a} returning "Variable '${a}' not found."