2
votes

How to test if a string variable in Robot Framework is empty?

My first naïve attempt looked like this:

Run Keyword If  ${myVar}!=${EMPTY}

but it failed:

Evaluating expression '!=' failed: SyntaxError: unexpected EOF while parsing (, line 1)

I then found this issue at Github but it didn't suggest a solution, just that the error message was unclear. An alternative solution was presented here:

${length}=    Get Length    ${Portfolio_ste}
Run Keyword If    ${length}    Go To Edit Portfolio

but is this really the best practice?

(The context is that I use a variable argument list and if a certain variable contains a value something should be done, otherwise just ignore it)

2
Run Keyword If '${myVar}'!='${EMPTY}' wrap your variables in apostrophe - Dev

2 Answers

6
votes

The expression needs to be a valid python expression after variable substitution. Assuming for the moment that myVar might be something like the number 42, your expression would end up looking like this after substitution:

Run Keyword if  42!=

When comparing against the empty string you need to add quotes to guarantee that the expression is a proper python expression after substitution. For example:

Run Keyword If  "${myVar}"!="${EMPTY}"
0
votes

Try Get Variable Value. It solved my problem.