Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Else-if in Robot Framework

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
like image 430
Natchaya TingTang Avatar asked Jan 30 '15 05:01

Natchaya TingTang


People also ask

How do you write if else condition in Robot Framework?

END Use Run Keyword If in Robot Framework Run Keyword If ${True} Log This line IS executed. Run Keyword If ${False} Log This line is NOT executed. Use Run Keyword Unless in Robot Framework Run Keyword Unless ${True} Log This line is NOT executed. Run Keyword Unless ${False} Log This line IS executed.

How do you run multiple keywords in if condition in Robot Framework?

You can do a couple of things. The first is to create a new keyword that calls all the other keywords, and then call that from Run keyword if . This might be the most readable solution, but at the expense of having to write and document another keyword. Show activity on this post.

How do you know if an element is present in Robot Framework?

By locating the element using xpath, I assume that you're using Sselenium2Library . In that lib there is a keyword named: Page Should Contain Element which requires an argument, which is a selector , for example the xpath that defines your element. The keyword failes, if the page does not contain the specified element.


2 Answers

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}
like image 160
Minh Nguyen-Phuong-Hoang Avatar answered Sep 23 '22 04:09

Minh Nguyen-Phuong-Hoang


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.

like image 29
Brandon Olson Avatar answered Sep 22 '22 04:09

Brandon Olson